-1

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

1
  • What makes you think it's a software problem? If you don't know if it's software or hardware, then electronics.stackexchange.com is more suitable for these kind of questions. Or if you have questions about Arduino libs or boards specifically: arduino.stackexchange.com Commented Feb 11 at 10:13

2 Answers 2

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());
}
Sign up to request clarification or add additional context in comments.

Comments

0

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());
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.