I’ve recently been through a bit of frustration connecting Aideepen HC-05 with an Arduino Mega 2560 on behalf of my client. I couldn’t find a good guide, so I’m writing one. Here’s everything you need to know.
The Aideepen HC-05
The Aideepen HC-05 is a Bluetooth serial component built around the popular HC-05 Bluetooth chip. It converts Bluetooth data to serial data. A big chunk of figuring this out was finding the datasheet for the Aideepen HC-05. Here’s the datasheet for the Aideepen HC-05.
There are six pins on this chip set: EN, VCC, GND, TXD, RXD and STATE. We’re only going to use the middle four.
The Mega 2560
All of the code I could find used the Arduino Nano (which is a lot of fun to work with, BTW). None of it used the Mega 2560 USART stack, so I converted the existing code using the Mega 2560 spec. This processor has four onboard USART chips, numbered 0-3. My example uses 2 because I had pins 16-17 (marked RX2 and TX2 on the board) available on my project. You and do the same with the others.
Each of the USART chips links to a pair of pins on the Mega 2560 so the USART 0 uses RX0 and TX0, USART 1 with RX1 and TX1, and, obviously by now, USART 3 with RX3 and TX3. Just make sure to change out the register names to use those numbers instead of 2 in the example.
Using RX0 and TX0 will disable your serial monitor and your ability to upload code. I wouldn’t do it. You’ve been warned.
Wiring it Up
Having a look at the image, here’s how we’re going to Connecting Aideepen HC-05 with an Arduino Mega 2560.
- VCC-This is the power. I hooked this up to 5V on the Mega
- GND-Ground. Plug this straight to ground.
- TXD-Transmit. You can plug this directly into the RX pin, in this case RX2.
- RXD-Recieve. This is the hard one. See below.
The RXD Pin and a Voltage Divider
So everything I read said that you can just hook up the RXD pin to the TX2 pin and get on with your life. This, however, is not recommended because the RXD pin is designed to take less than the 5V sent out of the TX2 pin. It is therefore recommended to use a voltage divider to dump the extra voltage.
This is electrical engineer stuff and therefore not of a huge interest to me, but the principle is that you use two resistors, one double the value of the other, to dump the extra voltage and save wear and tear on your RXD pin. Here’s how I set it up.
Connect the TX2 pin to a spot on your breadboard. In my case, I’m using an orange wire to indicate the 5V TX signal.
Next, run this through a 1K ohm resistor to another spot on the breadboard.
From here, we divide (thus voltage divider) where the power goes. You first want to run a wire from the output of the 1K ohm resistor to the RXD pin on the chipset (in my case, the yellow wire). On the other side of this wire, run the 2K ohm resistor to ground. This will complete your voltage divider.
Connecting Aideepen HC-05 with an Arduino Mega 2560
Now my favorite part: the software. The goal of this software is simply to relay your serial monitor to the Bluetooth chipset and the other way round. In this way, you can communicate without writing a bunch of code.
Setting Up
const int FOSC 16000000 // Clock Speed
const int BAUD 38400
const int MYUBRR (FOSC/16/BAUD-1)
const char NO_DATA = 0xff;
We’re going to start by setting up some constants. FOSC is the clock speed, which is 16 MHz for the Mega 2560. We’re using a BAUD rate of 38,400 which is fine for day to day communication and happens to be the default for the command mode as well. Then we do a calculation for the USART register and set up a flag for later.
void setup() {
Serial.begin(115200);
Serial.println("Arduino is ready");
Serial.println("Remember to select Both NL & CR in the serial monitor");
USART_Init(MYUBRR);
}
Next is the ubiquitous setup method. Here we setup the serial monitor at 115200 and remind the user to change the serial monitor to both NL & CR. Make sure you do both with this example to make it work correctly.
The USART Functions
/**
* Initialize USART 2 for pins 16 & 17
* @param ubrr
*/
void USART_Init(unsigned int ubrr) {
/* Set baud rate */
UBRR2H = (unsigned char) (ubrr >> 8);
UBRR2L = (unsigned char) ubrr;
/* Enable receiver and transmitter */
UCSR2B = (1 << RXEN2) | (1 << TXEN2);
}
unsigned char USART_Receive() {
char result = NO_DATA;
if (UCSR2A & (1 << RXC2)) {
result = UDR2;
}
return result;
}
void USART_Transmit(char data) {
/* Wait for empty transmit buffer */
while (!(UCSR2A & (1 << UDRE2)));
/* Put data into buffer, sends the data */
UDR2 = data;
}
void USART_Transmit(char *str) {
int idx = 0;
char ch;
while ((ch = str[idx]) != 0) {
USART_Transmit(ch);
idx++;
}
}
These are basic transmit and receive functions to use USART to communicate with the HC-05. Don’t forget that the 2s here should be replaced with the ID of whichever USART you’re using.
The details of these values would require a lot of explanation from the Mega 2560 spec. If you are curious, please look there.
The Read/Write Loop
void loop() {
// Read from HC-05 and send any data to Arduino Serial Monitor
char ch = USART_Receive();
if (ch != NO_DATA) {
Serial.write(ch);
}
// Read from Arduino Serial Monitor and send to HC-05
if (Serial.available())
{
ch = Serial.read();
USART_Transmit(ch);
}
}
And the main loop. Read a character from the USART. If you get one, send it to the serial monitor.
Then, if available, read a character from the serial monitor. Write that character to the USART.
The Proof of the Pudding
#include <Arduino.h>
const int FOSC 16000000 // Clock Speed
const int BAUD 38400
const int MYUBRR (FOSC/16/BAUD-1)
const char NO_DATA = 0xff;
void setup() {
Serial.begin(115200);
Serial.println("Arduino is ready");
Serial.println("Remember to select Both NL & CR in the serial monitor");
USART_Init(MYUBRR);
}
/**
* Initialize USART 2 for pins 16 & 17
* @param ubrr
*/
void USART_Init(unsigned int ubrr) {
/* Set baud rate */
UBRR2H = (unsigned char) (ubrr >> 8);
UBRR2L = (unsigned char) ubrr;
/* Enable receiver and transmitter */
UCSR2B = (1 << RXEN2) | (1 << TXEN2);
}
unsigned char USART_Receive() {
char result = NO_DATA;
if (UCSR2A & (1 << RXC2)) {
result = UDR2;
}
return result;
}
void USART_Transmit(char data) {
/* Wait for empty transmit buffer */
while (!(UCSR2A & (1 << UDRE2)));
/* Put data into buffer, sends the data */
UDR2 = data;
}
void USART_Transmit(char *str) {
int idx = 0;
char ch;
while ((ch = str[idx]) != 0) {
USART_Transmit(ch);
idx++;
}
}
void loop() {
// Read from HC-05 and send any data to Arduino Serial Monitor
char ch = USART_Receive();
if (ch != NO_DATA) {
Serial.write(ch);
}
// Read from Arduino Serial Monitor and send to HC-05
if (Serial.available())
{
ch = Serial.read();
USART_Transmit(ch);
}
}
Upload your code and then disconnect the USB Cable. Just above the EN pin on the Bluetooth chipset is a small button. Hold that down and plug the USB cable back in. The onboard LED should now flash once every two seconds. Let go of the button. You are now in commend mode.
Open the serial monitor and type “AT” and press enter. You should get back “OK.” This tells you you are in command mode.
Use the AT commands to make any settings changes you wish and then pull the USB cable again. Wait a few seconds and plug back in to be in normal mode.
I used the LED Controller app on my tablet to connect to the HC-05 and send data to it. You should see that data mirrored on the serial monitor. This is the proof that everything is working correctly.
Connecting Aideepen HC-05 with an Arduino Mega 2560
Well, that’s it. That should be everything you need to know to start Connecting Aideepen HC-05 with an Arduino Mega 2560. I hope you enjoyed this.