I bought HC-06 bluetooth module to connect it with Arduino Mega2560, i found this code and try to run it, it is compiling well and uploaded to the board, i try to send the command to the module via serial monitor, when i sent the command there is no answer, what is the problem ?
#include <SoftwareSerial.h>
SoftwareSerial mySerial(18, 19); // RX, TX
String command = ""; // Stores response of the HC-06 Bluetooth device
void setup() {
// Open serial communications:
Serial.begin(9600);
Serial.println("Type AT commands!");
// The HC-06 defaults to 9600 according to the datasheet.
mySerial.begin(9600);
}
void loop() {
// Read device output if available.
if (mySerial.available()) {
while(mySerial.available()) { // While there is more to be read, keep reading.
command += (char)mySerial.read();
}
Serial.println(command);
command = ""; // No repeats
}
// Read user input if available.
if (Serial.available()){
delay(10); // The delay is necessary to get this working!
mySerial.write(Serial.read());
}
}
Modify the second part of your code so it receives the whole command from the PC before sending it to the Bluteooth device. Then you can use Serial.println() to check that the Arduino has received the correct command from the Serial Monitor. The first or second example in serial input basics should be suitable.
What are you sending to the Bluetooth device? I think for some of the programming commands it works at a higher baud rate.
I only have the slave version of Bluetooth and as I write I can't remember if that is the HC05 or HC06. In any case I was frustrated for a while until I realized that mine only responds to a few commands.
I won't ask why you are using software serial at all, but you are using it on hardware serial ports, which is a really bad idea.
Here is the programmer I used for HC-06
/*
Time to stop fartarsing around with Uno...
This is for MEGA
It seems internally HC-06 only responds to 9600 anyhow.
Even if you set the baud rate to 115200 you can "reprogram"
the sketch again with 9600.
JY-MCU board pins
RX - 18 Tx1 orange
TX - 19 Rx1 white
GND - GND black
VCC - 5v red
Kudos to marguskohv - he sowed the seed....
*/
String command = ""; // Stores response from HC-06
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //monitor
Serial1.begin(9600); //bluetooth
Serial.println("AT");
Serial1.print("AT"); //PING
if (Serial1.available()) {
while(Serial1.available()) { // While there is more to be read, keep reading.
delay(3);
char c = Serial1.read();
command += c;
}
}
delay(2000);
Serial.println(command);
command = ""; // No repeats
Serial.println("AT+NAMEFosters");
Serial1.print("AT+NAMEFosters"); //CHANGE NAME
if (Serial1.available()) {
while(Serial1.available()) { // While there is more to be read, keep reading.
delay(3);
command += (char)Serial1.read();
}
}
delay(2000);
Serial.println(command);
command = ""; // No repeats
Serial.println("AT+PIN1234");
Serial1.print("AT+PIN1234"); //CHANGE PASSWORD
if (Serial1.available()) {
while(Serial1.available()) { // While there is more to be read, keep reading.
delay(3);
command += (char)Serial1.read();
}
}
delay(2000);
Serial.println(command);
command = ""; // No repeats
Serial.println("AT+BAUD8");
Serial1.print("AT+BAUD8"); //CHANGE SPEED TO 115K
if (Serial1.available()) {
while(Serial1.available()) { // While there is more to be read, keep reading.
command += (char)Serial1.read();
delay(2000);
Serial.println(command);
}
}
}
void loop(){
} //one-shot - nothing here
Dear Nick_Pyner, if there is another way to use the serial on MEGA2560 please tell me because this is the only way that i know. I try to run your posted code and try to change the pin code to 1111, but bluetooth module doesn't affect and pin still at default 1234 ?
It probably isn't. I bet you have used hardware serial before. The only time using software serial is justified on a Mega is when you have five or more serial devices connected to it.You might find the following background notes useful.
Sorry, I had not noticed that you were using a Mega and SoftwareSerial. There is no need to use SoftwareSerial on a Mega. Just use Serial1.begin() etc in place of mySerial.begin() - it works much better.
Robin2:
Sorry, I had not noticed that you were using a Mega and SoftwareSerial. There is no need to use SoftwareSerial on a Mega. Just use Serial1.begin() etc in place of mySerial.begin() - it works much better.
...R
can u explain more specific how to use HC-06 with MEGA 2560?
This involves no more than that explained in reply#5, and there are examples and pictures in the notes alluded to in reply #4. Serial3, serial2, and serial1 are on pins 14>19 and clearly marked on the board.
It is advantageous to use one of these routinely if you can, as it then leaves Serial0, on pins D0,D1 open for the serial monitor.