JYMCU Bluetooth Units, HC05 and HC06 Part 2

Part 1 showed how to set up the Slave Bluetooth HC06 and can be seen here.

The HC05 is a Master/Slave Bluetooth Unit, that at first sight looks identical to the Slave only HC06. I suggest that as soon as you receive the HC05 you mark the board with an M. There are normally six connections on the board, although often only four, Vcc, Gnd, Tx and Rx will have pins. One more connection is required, KEY this is normally connected to ground via a resistor. When this pin is connected to Vcc the unit goes into AT command mode. If this connection does not have a pin then either solder a single pin or a connecting wire.

The HC05 can be powered from either 3 volts or 5 volts, but Tx and Rx should be 3 volts only, so use a Logic Level Converter. The normal baud rate for communications is 9600, but when in the AT mode the baud rate is set at 38400.

To program the HC05 with AT commands first upload the sketch shown below, then remove the power from the Arduino. Connect Vcc to Arduino +5 volts, Gnd to Arduino Gnd. Software Serial is used in the sketch shown here, so connect Tx from the HC05 to pin 2 on the Arduino (Software Serial Rx) and Rx from the HC05 to pin 3 on the Arduino (Software Serial Tx).  Connect the KEY pin to Vcc and apply power to the Arduino.

The HC05 will now be in the AT mode and communication is at 34800. Type AT into the text entry box in the Serial Monitor, which should be set to 9600 baud and the Send CR and LF setting selected. If all is well OK will be displayed. All AT commands must be in UPPERCASE. Try some of the following commands and when you are finished send AT+RESET to exit the AT command mode, connect the KEY pin to Gnd to select communication mode. It is probably best leaving the baud rate at 9600, but you can rename the HC05  to whatever suits you. Of course you need to set the HC05 to be a MASTER by sending AT+ROLE1.

Most useful AT commands are

AT : Ceck the connection.
AT+NAME : See default name
AT+ADDR : see default address
AT+VERSION : See version
AT+UART : See baudrate
AT+ROLE: See role of bt module(1=master/0=slave)
AT+RESET : Reset and exit AT mode
AT+ORGL : Restore factory settings
AT+PSWD: see default password

 

If you mess things up, or forget what you did then AT+ORGL will reset everything back to factory settings. 


/*******************************************************
// HC05 Test Sketch - Sending AT Commands
//
// On first running the Bluetooth module is reaset to factory settings then the speed is set to 9600
// and the name is reset to HC05. The current speed and bluetooth number (MAC) is printed
//
// Connect RX from the HC05 to pin 2 on the Arduino.
// Connect TX from the HC05 to pin 3 on the Arduino
// to send AT commands remove all power and connect KEY to Vcc
// Switch the power on
// Open up the Serial Port Monitor
// Type in AT and click on the SEND button, OK will be printed in the monitor
// showing that the module is receiving commands.
//
*****************************************************/

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

String buildString = ""; // Stores response of bluetooth device
// which simply allows \n between each response.

void setup()
{
// Open serial communications and wait for port to open:
// Hardware Serial port set to 9600
Serial.begin(9600);
// SoftwareSerial "com port" data rate. 38400.
mySerial.begin(38400); // default speed in AT mode
//
// reset bluetooth module to factory settings
// Note: the delay blocks are necessary to allow the commands to take place
Serial.println("Initialising ...");
mySerial.write("AT+ORGL\r\n");
delay(2000);
mySerial.write("AT+UART=9600,0,0\r\n");
delay(2000);
mySerial.write("AT+NAME=BluetoothBee\r\n");
delay(2000);
mySerial.write("AT+UART?\r\n");
delay(2000);
mySerial.write("AT+NAME?\r\n");
delay(2000);
Serial.println("Ensure that both NL and CR are selected");
delay(2000);
Serial.println("Enter AT commands");
}

void loop() {
// Read device output if available.
if (mySerial.available()) {
while(mySerial.available()) { // While there is more to be read, keep reading.
buildString += (char)mySerial.read();
}
Serial.println(buildString);
buildString = ""; // reset string
}
// Read user input if available.
if (Serial.available()){
delay(10);
mySerial.write(Serial.read());
}

}

Part 3 will show how to connect two Arduino’s via bluetooth to send data.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s