Hi, everyone, I recently tried using the HC-05, and the HC-06 Bluetooth mod to send messages between them. But I can just let them connect, I can't send any news.
This is my code:
master(HC-05)↓↓
#include <SoftwareSerial.h>
SoftwareSerial BT(10, 11);
char val;
void setup()
{
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
Serial.begin(9600);
Serial.println("Enter AT commands:");
BT.begin(38400);
}
void loop()
{
if (Serial. available())
{
val = Serial.read();
BT.print(val);
}
if (BT.available())
{
val = BT.read();
Serial.print(val);
}
}
slave(HC-06)↓↓
#include <SoftwareSerial.h>
SoftwareSerial BT(10, 11);
char val;
void setup()
{
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
pinMode(12, OUTPUT);
Serial.begin(9600);
BT.begin(9600);
}
void loop()
{
if (BT.available())
{
val = BT.read();
if (val == '1')
{
digitalWrite(12, HIGH);
BT.println("LED ON");
}
else if (val == '0')
{
digitalWrite(12, LOW);
BT.println("LED OFF");
}
}
}
Any suggestions for improvement? Thanks!
1 Like
J-M-L
December 8, 2022, 1:16pm
2
how did you configure the HC-05 and HC-06? have they been paired? is 38400 bauds what they use? (seems you have 38400 for one and 9600 for the other BTW)
1 Like
They maybe had connected, and the HC-06 was constant light. But how to check that they had connected?
1 Like
J-M-L
December 8, 2022, 1:26pm
4
follow a tutorial on configuring them
Detailed and step-by-step instructions for HC-05 and HC-06 Bluetooth modules. AT commands, circuits, pairing and testing of the Bluetooth connection.
Est. reading time: 15 minutes
1 Like
The introduction is very detailed! Thank you!
1 Like
I switched the HC-06 baud rate to 38400, and they look like can send messages.
But currently, when I key "1" in HC-05, the HC-06 will print "⸮".
Master code:
#include <SoftwareSerial.h>
SoftwareSerial BT(10, 11);
String val;
void setup()
{
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
Serial.begin(9600);
Serial.println("Enter AT commands:");
BT.begin(38400);
}
void loop()
{
if (Serial.available())
{
val = Serial.readString();
Serial.println(val);
BT.print(val);
}
}
Slave code:
#include <SoftwareSerial.h>
SoftwareSerial BT(10, 11);
String val;
void setup()
{
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
pinMode(12, OUTPUT);
Serial.begin(9600);
BT.begin(38400);
}
void loop()
{
if (BT.available())
{
val = BT.readString();
Serial.print(val);
if (val == '1')
{
digitalWrite(12, HIGH);
BT.println("LED ON");
}
else if (val == '0')
{
digitalWrite(12, LOW);
BT.println("LED OFF");
}
}
}
Where was wrong?
1 Like
J-M-L
December 9, 2022, 2:54pm
7
modify your call to readString()
into readStringUntil('\n')
change the comparison if (val == '1')
into if (val.startsWith("1"))
can you confirm what Arduino you are using and how the modules are wired ?
1 Like
Your code is for configuring your Bluetooth module's but your heading implies you want communication between two blueteeth. The game is not the same but you need to configure an HC-05 as master first. See the Martyn Currey website referred to above.
You must use 38400 for AT mode.
You do not need to change the baud rate for communications.
Baud rate applies only to comms between Bluetooth and Arduino, and must match. It has nothing to do with Bluetooth /Bluetooth communications, or what might be going on at the other end.
1 Like
I use the UNO board.
HC-05
RX-->11
TX-->10
GND-->GND
VCC-->5V
HC-06
RX-->11
TX-->10
GND-->GND
VCC-->5V
1 Like
Thank's your reply, I now knew it.
1 Like
I have changed some places, and then it can talk between two Bluetooth!
Master code:
#include <SoftwareSerial.h>
SoftwareSerial BT(10, 11);
String val;
void setup()
{
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
Serial.begin(9600);
Serial.println("Enter AT commands:");
BT.begin(9600);
}
void loop()
{
if (Serial.available())
{
val = Serial.readStringUntil('\n');
Serial.println(val);
BT.print(val);
}
}
elisa20110908:
BT.begin(38400);
I changed 38400 to 9600 because I found out that the baud rate needs to be the same.
BT.begin(9600);
Slave code:
#include <SoftwareSerial.h>
SoftwareSerial BT(10, 11);
String val;
void setup()
{
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
pinMode(12, OUTPUT);
Serial.begin(9600);
BT.begin(9600);
}
void loop()
{
if (BT.available())
{
val = BT.readStringUntil('\n');
Serial.print(val);
if (val.startsWith("1"))
{
digitalWrite(12, HIGH);
BT.println("LED ON");
}
else if (val.startsWith("0"))
{
digitalWrite(12, LOW);
BT.println("LED OFF");
}
}
}
I also change the baud rate in the slave code.
And here is my test:
If someone have a similar problem, you can try my code, and maybe it will work!
1 Like
Good, but your code above is redundant and or misleading. It might as well be
Serial.println(" Hello, Bluetooth here!");
1 Like
Oh, I forgot to change it to "Hello".Thank you for your point, now I had changed.
Master code:
#include <SoftwareSerial.h>
SoftwareSerial BT(10, 11);
String val;
void setup()
{
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
pinMode(12, OUTPUT);
Serial.begin(9600);
BT.begin(9600);
Serial.println(" Hello, Bluetooth here!");
}
void loop()
{
if (BT.available())
{
val = BT.readStringUntil('\n');
Serial.print(val);
if (val.startsWith("1"))
{
digitalWrite(12, HIGH);
BT.println("LED ON");
}
else if (val.startsWith("0"))
{
digitalWrite(12, LOW);
BT.println("LED OFF");
}
}
}
Master code:
#include <SoftwareSerial.h>
SoftwareSerial BT(10, 11);
String val;
void setup()
{
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
Serial.begin(9600);
Serial.println(" Hello, Bluetooth here!");
BT.begin(9600);
}
void loop()
{
if (Serial.available())
{
val = Serial.readStringUntil('\n');
Serial.println(val);
BT.print(val);
}
}
1 Like
system
Closed
June 12, 2023, 12:42pm
15
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.