I am working on a project using two HC-05 bluetooth modules to communicate with one another as master and slave. I have configured both units to have a baud rate of 38400, the master has the address of the slave, and they both have the same password. I have them communicating with each other, but when I was testing it out with a minimal working example code, I had the master sending over just one number constantly, and the slave reading it and printing it to the serial monitor. This works, and most of the time the slave reads the correct value, but occasionally the slave will read in a random value. Any idea what might be causing this and how to fix it? Below is code for Master and Slave. Both HC 05 modules are connected properly with a voltage divider shifitng the logic level to 3.3 V for the RX of the HC 05 as they are supposed to.
Master
#include<SoftwareSerial.h>
SoftwareSerial Master(10,11);
void setup() {
// put your setup code here, to run once:
Master.begin(38400);
}
void loop() {
// put your main code here, to run repeatedly:
Master.write(212);
}
Slave
#include<SoftwareSerial.h>
SoftwareSerial Slave(10,11);
void setup() {
// put your setup code here, to run once:
Slave.begin(38400);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (Slave.available()){
Serial.println(Slave.read());
}
}
I'm guessing that "most of the time" is actually less than you think, and you would never know anyway. Your loop is pointless in practice and is just tearing around at amazing speed spewing out the same signal, so you can't tell which is which, or what is really going on.
Put some delay in the master loop, and have it send some real data. Like a count.
Another thing is that you are taking something of a risk using software serial at 38400. I bet you have no good reason to do so, so cease and desist. It may be that this is your only problem.
38400 is the default speed for the HC 05, which is why I am using it. I included a delay in both, and confirmed that it is indeed most of the time. I'm well aware there's no actual point to this, I was just confirming the two are actually connected to each other, and can at least send and receive data. I will try with a count
okay I see. I will reconfigure the HC 05's for a slower speed and see if that helps. Unfortunately the larger project I am using requires use the use of the default Rx and Tx pins so I am stuck with the software serial
38400 is the required speed for AT mode, and is not the default speed for anything. If you really need a higher speed you might be better off with a Mega, which has extra ports, and you can then run at 115200.
Here are a couple of demo sketches that use 2 Bluetooth modules to acquire a range from a HCSR04 rangefinder connected to an Uno and HC05 and communicate that range to another Uno with a connected HC05 using concepts from the serial input basics tutorial. Maybe you can get some use from these sketches.
Acquire range and send:
//Bluetooth Master
//Sends the ultrasonic distance to the Slave
#include <SoftwareSerial.h>
SoftwareSerial BTserial(5, 6); // RX | TX
// defines pins numbers
const int trigPin = 7;
const int echoPin = 8;
// defines variables
long duration;
int distance;
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
BTserial.begin(9600);
Serial.println("Sonar sender starting");
BTserial.println("Sonar sender starting");
}
void loop()
{
static unsigned long timer = 0;
unsigned long interval = 1000;
if (millis() - timer >= interval)
{
timer = millis();
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
// Send the distance via Bluetooth
BTserial.println(distance);
}
}