My second post to the site. Woo Hoo! Ok let's get down to business. Finding the proper material to get the HC-05 to work was difficult, but it has finally paid off for the most part. Here's a breakdown of what I learned and how to get it to work.
WIRING the HC-06 to ARDUINO:
The HC-05 has 4 Pins (VCC, GND, TXD, and RXD). I'm using the version that does NOT have a button. Apparently there are 3 different versions! D: Not to panic. This should work for them all. Hopefully...
- Connect GND to GND on the Arduino.
- Connect VCC to +5V on the Arduino.
- Connect TXD to Pin 10 on the Arduino. Next Step is Tricky.
- Connect RXD to both GND and TX 1 using a 2.2K Resistor and 1K Resistor, respectively. I did this by connecting a lead to GND with a 2.2K and a lead to Pin 11 with a 1K, then connecting the free ends of the resistors to a lead connected to RXD on the HC-05.
Plain English:
(GND) --> GND
(VCC) --> +5V (Later +5V will change to Pin 7)
(TXD) to Pin 10 (Later Pin 10 will change to RX 0)
GND --> 2.2K Resistor --> (RXD) <-- 1K Resistor <-- Pin 11 (Later Pin 11 will change to TX 1)
Don't ask how this works. It just does. XD
CODING with your HC-05
The first thing you should know is that working setting this unit up can be a pain. There are two separate codes you'll need to get started.
The FIRST CODE allows you to give it a name, ensure it's wired correctly, and set the Baud Rate for the HC-05.
Before you upload to Arduino, unplug +5V, so no power goes to the TX and RX pins. This is important. If not done your upload will hang. Also be sure to use a USB to upload, as this setup won't allow you send a sketch over bluetooth. Now that it's uploaded, reconnect +5V and reset Arduino. If anything is out of sync (BAUD RATE WRONG), you won't be able to communicate with the unit and you may also get weird characters in your Serial. If all is well, it will print several lines, if it isn't working right you will only see "OK" followed by blanks and eventually "Done!" (WIRED WRONG). The Bluetooth has a built-in memory to record the settings you choose. (I've tried using other pins beside 10 and 11, and was NOT able to get it to work! Not sure why this is exactly.) D:
#define ROBOT_NAME "RobotName"
//Change "RobotName" to what you want it called. Must be ONEWORD!
// If you haven't configured your device before use this
#define BLUETOOTH_SPEED 57600
// If you are modifying your existing configuration, use this:
// #define BLUETOOTH_SPEED 57600
#include <SoftwareSerial.h>
// Swap RX/TX connections on bluetooth chip
// Pin 10 --> Bluetooth TX
// Pin 11 --> Bluetooth RX
SoftwareSerial mySerial(10, 11); // RX, TX
/*
The possible baudrates are:
AT+BAUD1-------1200
AT+BAUD2-------2400
AT+BAUD3-------4800
AT+BAUD4-------9600 - Default for hc-05
AT+BAUD5------19200
AT+BAUD6------38400
AT+BAUD7------57600
AT+BAUD8-----115200
AT+BAUD9-----230400
AT+BAUDA-----460800
AT+BAUDB-----921600
AT+BAUDC----1382400
*/
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Starting config");
mySerial.begin(BLUETOOTH_SPEED);
delay(1000);
// Should respond with OK
mySerial.print("AT");
waitForResponse();
// Should respond with its version
mySerial.print("AT+VERSION");
waitForResponse();
// Set pin to 0000
mySerial.print("AT+PIN0000");
//The Pin Used to Pair Is 0000 (Change it to PIN1234 to make it 1234)
waitForResponse();
// Set the name to ROBOT_NAME
mySerial.print("AT+NAME");
mySerial.print(ROBOT_NAME);
waitForResponse();
// Set baudrate to 9600
mySerial.print("AT+BAUD4"); //Only change if using another BaudRate. Else LEAVE IT ALONE!!!
waitForResponse();
Serial.println("Done!");
}
void waitForResponse() {
delay(1000);
while (mySerial.available()) {
Serial.write(mySerial.read());
}
Serial.write("\n");
}
void loop() {}
The SECOND CODE sends data from your laptop (or desktop if it has Bluetooth) to your Arduino via the HC-05. You will need to make the changes to the wiring we discussed earlier in the Wiring section. This setup will allow you to upload to Arduino WITH the HC-05 still attached. After you've completed your wiring upload the new sketch. I suggest adding an LED between Pin 13 and GND so you can visually see it working. Press 1 in Serial to Turn on LED and Press 0 to turn it off. If this works you are good to go. If all checks out, go up to the Taskbar, choose Tools --> Port --> /dev/cu.RobotName-DevB. Unplug USB from computer and plug it into a wall outlet. Then go back to Serial and Press 1 and 0 to see if it turns on and off.
NOTE: By making Pin 7 Output initially we keep the bluetooth unit from sending power to the TX 1 and RX 0 on the Arduino (which will make it hang). Once initialized HC-05 will turn back on and flash red. When you open Serial, it should reconnect to Bluetooth (turning solid red) as long as that is the chosen Port. Please note that it WILL disconnect when you close Serial.
char data = 0; //Variable for storing received data
void setup()
{
Serial.begin(9600); //Sets the data rate in bits per second (baud) for serial data transmission
pinMode(13, OUTPUT); //Sets digital pin 13 as output pin
pinMode(7, OUTPUT);
digitalWrite(7, HIGH);
}
void loop()
{
if(Serial.available() > 0) // Send data only when you receive data:
{
data = Serial.read(); //Read the incoming data and store it into variable data
delay(100);
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n"); //New line
if(data == '1') //Checks whether value of data is equal to 1
digitalWrite(13, HIGH); //If value is 1 then LED turns ON
else if(data == '0') //Checks whether value of data is equal to 0
digitalWrite(13, LOW); //If value is 0 then LED turns OFF
}
}
I hope that this has been helpful. I'm currently working on a method to upload a sketch to Arduino via bluetooth. I'll let you know if I succeeded in a later post. Let me know if you have any questions.