Using the HC-05 Bluetooth Module

Bluetooth Module

The HC-05 bluetooth module provides a bluetooth serial connection to a computer or another microcontroller. It is available on eBay for anywhere from $3 to $12 each, depending on how long you want to wait for it. The module is 3.3V only, so you could use a Teensy 3.1, 3.2, or LC to drive it, although any 3.3V Arduino or clone will work. I used an Arduino Uno with a resistor divider to prevent damage to the 3.3V module. Many modules accept 5V for VCC. We will only look at using the HC-05 as a slave for now - master mode is another article.

What problems did I run into?

All HC-05 modules may be equal, but the carrier boards they are on are not! Beware of modules which claim to be master/slave capable, but have no "KEY" pin on the header. They will all do slave mode, connecting your microcontroller to your PC, but if they don't bring out pin 34 from the module, you can't put them in AT mode to give them the command to change to master mode, at least not without soldering a wire to pin 34. See the HC-05 Command Mode saga for details on how to do it.

What does it do?

By default, the HC-05 bluetooth module establishes a radio link with a master, presenting itself as a bluetooth serial port. If the master is a PC, you can communicate with the Arduino via serial terminal by selecting the HC-05 module from the list of serial ports.

The HC-05 connected to the Arduino Uno. The divider R1/R2 keeps the transmit signal from the Uno below 3.3V, so the module is not damaged. The LEDs are not required, but are there to aid in troubleshooting.

The power requirements on most boards are 3.6V to 6V on VCC, but 3.3V on signal lines. That is because the carrier boards have regulators that regulate the VCC down to 3.3V. Check your carrier board for a three-terminal regulator before applying 5V. The benefit of doing this is an additional 0.3V available to the module for RF power.

This code allows you to make a link between the Arduino and the PC using bluetooth. It is compatible with the Teensy 3.1, 3.2, LC, and Arduino:

    
#include <SoftwareSerial.h>

#define LED_BT 7
#define LED_PC 8

SoftwareSerial BTSer(9, 10);

long LED_BT_TimeOn = 0;
long LED_PC_TimeOn = 0;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  
  BTSer.begin(9600);
  BTSer.println("Start up Bluetooth");
  pinMode(LED_BT, OUTPUT);  // LED - BT RX
  pinMode(LED_PC, OUTPUT);  // LED - PC RX
}

void loop() {
  if ( BTSer.available()) {
    digitalWrite(LED_BT, HIGH);   // set the LED on
    LED_BT_TimeOn = millis();
    Serial.write(BTSer.read());
  }

  if ( Serial.available()) {
    digitalWrite(LED_PC, HIGH);   // set the LED on
    LED_PC_TimeOn = millis();
    BTSer.write( Serial.read() );
  }
  if (millis() - LED_BT_TimeOn > 20) {
    digitalWrite(LED_BT, LOW);   // set the LED off
  }
  if (millis() - LED_PC_TimeOn > 20) {
    digitalWrite(LED_PC, LOW);   // set the LED off
  }
}
    

The Code

The code is a simple echo program, where anything coming to the Arduino from the USB port is sent back to the PC through the bluetooth, and anything coming from the PC through bluetooth is sent to the PC through the USB. I added the two LEDs so I could tell when the USB and the bluetooth were receiving data. Using two serial programs I was able to send data back and forth using bluetooth and USB.

The code was tested on Arduino Uno, Teensy 3.1, Teensy LC, and Anarduino Mini.

Arduino Board Logo

 

Arduino-Board is the go-to source for information on many available Arduino and Arduino-like boards, tutorials and projects.

Help and Support

Arduino-Board

Stay updated

Sign up if you would like to receive our once monthly newsletter.