最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c - Problem Communication Bluetooth Communication with Arduino UNO - Stack Overflow

programmeradmin1浏览0评论

I have a school project in Internet of Things where I have to create a bluetooth connection on an Arduino UNO. Here is the code in C I use for that :

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  mySerial.begin(9600);
  Serial.println("Start Communication");
}

void loop() { // run over and over
  if (mySerial.available()) {
  Serial.write(mySerial.read());
  }
  if (Serial.available()) {
  mySerial.write(Serial.read());
  }
}

And the montage is the following : Montage

The issue is when I upload the code on the Arduino to check if the connection is made, I enter the "AT" command that should return me "OK" to confirm that the connection is properly set. However nothing happens. Here is a picture :Serial Monitor I am stranded and I do not know what to do, could someone help me

Thank you very much

I have a school project in Internet of Things where I have to create a bluetooth connection on an Arduino UNO. Here is the code in C I use for that :

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  mySerial.begin(9600);
  Serial.println("Start Communication");
}

void loop() { // run over and over
  if (mySerial.available()) {
  Serial.write(mySerial.read());
  }
  if (Serial.available()) {
  mySerial.write(Serial.read());
  }
}

And the montage is the following : Montage

The issue is when I upload the code on the Arduino to check if the connection is made, I enter the "AT" command that should return me "OK" to confirm that the connection is properly set. However nothing happens. Here is a picture :Serial Monitor I am stranded and I do not know what to do, could someone help me

Thank you very much

Share Improve this question edited 2 days ago Hun43d asked 2 days ago Hun43dHun43d 91 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

First, please check the voltage level.

Before you start debugging, please check if the HC-06 module's RXD pin is 5V-tolerant. As far as I know, the HC-06 works with 3.3V logic levels, and its RXD pin does not support 5V.

Second, you can isolate the problem by splitting the issue.

I think you should check simple communication with HC-06.

void loop() {
  delay(1000); // Adding a delay may help stabilize communication
  
  mySerial.write("AT\r"); // Send AT command with carriage return

  // Print the number of available bytes every second
  Serial.println(mySerial.available());
}

I think there are two problems with your code. First you're using Serial.read() function. This function returns bytes from serial buffer in int data type. It means you get "AT" like 'A' then 'T'. Serial.read() article. You should use String or Arrays to read full string on data available. Serial.readString() article. Second use if() else if() instead of if() if().

void loop() { // run over and over
  if (mySerial.available()) {
  Serial.write(mySerial.readString());
  }
  else if (Serial.available()) {
  mySerial.write(Serial.readString());
  }
}
发布评论

评论列表(0)

  1. 暂无评论