user28451447
user28451447

Reputation:

Problem Communication Bluetooth Communication with Arduino UNO

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

Upvotes: 0

Views: 60

Answers (2)

AmirABD
AmirABD

Reputation: 297

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

Upvotes: 0

Atsushi Yokoyama
Atsushi Yokoyama

Reputation: 81

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

Upvotes: 0

Related Questions