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 badges2 Answers
Reset to default 0First, 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());
}
}