I have a HC-05 Bluetooth module that works fine with an Arduino Uno board (I was able to send and receive data using a phone app and even enter the AT mode and make sure the settings are all right).
The problem starts when I try to connect the module to my Arduino Leonardo board. The only difference I know is the fact that I must use Serial1 instead of Serial, but that is pretty much it.
Data doesn't send from my phone to the module, and when I try to send data from the module, on my phone appears a single "?" character. And of course, I can't enter the AT mode either.
After this, I've tried the SoftwareSerial library and it has the same behavior as described above.
Can someone please explain what I'm doing wrong and how to make it work,
it would be much appreciated!
The following is the code that I've used with the SoftwareSerial lib:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3,2); // RX, TX
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
void loop() { // run over and over
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
This is the wiring that I've done except for the Tx and Rx pins that I've changed in regards to the SoftwareSerial library usage.
Do you have the USB cable connected? While (!Serial) causes the code to wait for a valid USB connection, if that is not connected it will wait forever.
On a Leonardo, Serial1 is the Rx/Tx pins, Serial is the serial over USB to the computer. Commonly Serial1 would be used instead of SoftwareSerial, I don't see from your code how it would be used as a replacement for Serial.
I have the USB connected to print on my pc what I receive from my phone.
Bellow I have the code with Serial1:
void setup() {
// Open serial communications for USB connection
Serial.begin(9600);
while (!Serial) { // !Serial.available()
; // wait for Serial port to connect.
}
// set the data rate for the Serial1 (bluetooth module)
Serial1.begin(9600);
}
void loop() { // run over and over
if (Serial1) { // Serial1.available()
Serial.write(Serial1.read());
}
if (Serial) { // Serial.available()
Serial1.write(Serial.read());
}
}
void loop() { // run over and over
if (Serial1.available()) { // Serial1.available()
Serial.write(Serial1.read());
}
if (Serial.available()) { // Serial.available()
Serial1.write(Serial.read());
}
}
If (Serial1) checks to see if the Serial1 port exists, if (Serial1.available()) checks to see if any characters have been received.
I've tried with your modification and I receive "?" over and over again without sending anything. The large number of question marks you see in the photo I provide at the end is sent to the phone as well, but whatever I send from the phone does not get sent to the PC.