Hello! I'm trying to send an integer value (the number I chose for testing is 12) between 2 arduino nanos using HC-05 Bluetooth modules, one configured to master and one to servant. They connect fine, but when I try to send the number 12 between the modules I don't get anything showing up on the other end. I need to be able to send a value and then store it in a variable on he receiving end.
void setup()
{
Serial.begin(9600);
Serial.println("Enter AT commands:");
// HC-06 default baud rate is 9600
BTSerial.begin(9600);
}
uint8_t ID = 12;
void loop()
{
BTSerial.write(ID); //send ID value to other Bluetooth module?
Serial.print(ID);
}
Receiving module (servant) // Receive
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX
// Connect the HC-05 TX to Arduino pin 2 RX.
// Connect the HC-05 RX to Arduino pin 3 TX through a voltage divider.
//
char c = ' ';
uint8_t ID;
void setup()
{
Serial.begin(9600);
Serial.println("Arduino is ready");
Serial.println("Remember to select Both NL & CR in the serial monitor");
// HC-05 default serial speed for AT mode is 38400
BTserial.begin(38400);
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTserial.available())
{
ID = BTserial.read();
Serial.write(ID);
}
Thank you! Tried out the new baud rate and unfortunately it didn't work. Nothing appeared on the serial monitor on the servant side.
The circuit diagrams have a lot more going on than the code I showed cause it's for a project. I just needed to figure out Bluetooth part so I wanted to start from a blank script. I just double checked the pins, so these should be accurate.
The connections to SoftwareSerial look reversed on the transmitter.
The constructor is from the point of view of the Arduino. pin2 is rx and pin 3 is tx. The module tx connects to the arduino rx and the module rx connects to the arduino tx.
I would change all the .write() statements to .print() just to be sure that what is being sent is printable.
I don't have two modules set up, but I tested the servant (default state for HC05) program with a phone as a master, and it worked as planned when sent 12 from the phone.
When I place the sketches on two different Arduino unos without bluetooth and jumper/cross connect pins 2 and 3 on both units, the two codes communicate as planned.
I would put your focus on the master program. Are you certain the conversion from the default state was done correctly? How does the master connect to the servant module? How do you know the two modules are connected?
The LED on the HC05 modules have specific flashes to tell what mode that it is in.
AT mode alternating ≈3 seconds on and ≈3 seconds off.
Not connected rapid flashing ≈2 Hz.
Connected 2 rapid flashes followed by off for ≈3 seconds.
If they are connected, both modules should show the 2 rapid flashes with the longer off time. Not necessarily in sync.
I set up 2 more HC-05's and tried them on the circuit too, but the outcome was the same.
The LEDs switch to the long pause followed by 2 rapid blinks pattern shortly after they're turned on. I have them on CMODE=0, so that should mean they're pairing with each other. I set them up while they were in AT mode (long blinks, long pauses).
Could it be an issue with the HC-05's? I've ordered some more from a different brand, so I'll try those out once they arrive
Thank you so much for verifying the code! I appreciate it
I used this page to set my 2 HC05 modules up to talk to each other. I set the master to role=1 and the slave to role=0. Using the instructions from the linked page, my HC05s automatically paired and connected right away.
Idk how pings are used here, but the thread was dead for a while due to finals and graduation and now I'm back! I bought 2 more modules and set them up using Fungus's link. Everything was the same as in my initial setup except now I know how to check the baud rate in AT command mode! No matter how I held the reset button while powering the module on, they would not enter AT command mode so I resorted to setting their baud rate to 38400 manually, then resetting it to 9600.
The result was the same issue as before: 12 is printed on the master module's serial monitor, servant receives nothing.
I noticed that the example in Fungus's link used 38400 baud rate all the time (and also did not use softwareserial)... but since I've got RF using our serial port too I don't think I can just do that. So everything was reset to 9600 before my tests.
I tried serial.write, then switched to serial.print
Here's the screenshot of the serial monitor for the servant module. I would use AT+UART=9600,0,0 to set it back before testing, cause the codes are still set to 9600
Can you explain more about this. The changing of baud rate to say you are in AT mode sounds strange. What does the light blink patterns say about whether or not you are really in AT mode?
The setup screens look correct, and you say that the module flash pattern looks like they are connected.
Can you get the slave to respond if you send something to it with an Android phone terminal program. I use Kai Morich's Serial Bluetooth Terminal.
Are you testing the bluetooth piece of this project in isolation from the "alarm/activation system"? There may be power issues with trying to run two transmitters.
I think that the setting of the baud rate is AT+UART=baud,1,0. That is baud, 1 stop bit, no parity. Needs one stop bit cause Arduino serial is 8n1 (8 bit, no parity, 1 stop bit.
I hold the reset button while plugging in the uno I use for the HC-05 setup. This results in long, slow blinks.
I just used AT+NAME? to check if it's actually in full AT command mode, and it looks like I'm in full AT command regardless of the baud rate I set the devices too
Ok, so I set the baud rate on both modules to 9600,1,0, and I'm receiving values on the receiver end!!!! I get 49, 50, 49, 50, etc etc etc. Not correct, but so so close
edit: Ok so the modules are sending the digits one at a time and adding 48 to each digit. I'm not sure why the digits get separated.