Arduino Bluetooth Tutorial HC-05





You can download our e-book ‘Learn Arduino’ from this link

Bluetooth has been around for quite a while now. Being a common way to communicate with mobile phones, it was only natural to include this protocol in the Arduino arena.
Bluetooth is typically a low power, medium range device, in fact it can reach up to 10 meters. Bluetooth operates on the same frequencies as WiFi, 2.4Ghz.Connections are normally one to one meaning no group communication is allowed by the protocol. The connection is established by a master device, which connects to a slave device. In internet terminology the master is the client and the slave is the server. Certain devices can be configured to act as both master and slave (like the HC-05) while others are pre-programmed as slaves only (like the HC-06).
After a successful connection has been established the two devices can exchange data. Data is exchanged as bytes but there are libraries which make it easy to send strings and other data types in a transparent manner.

Can Arduino control a Bluetooth device? Of course, the most common devices are the HC-05 and HC-06. The difference between the two is that the HC-05 can act as a slave and as a master, while the HC-06 can act just as a slave. So don’t bother with the HC-06 🙂  In this tutorial we will set up a slave HC-05 Bluetooth device and pair it with an Android phone. Then using a Bluetooth terminal found on the Android Play Store, we will be able to send commands to the Arduino and receive confirmation. We will try to switch the builtin LED on pin 13, on and off.

Hardware needed

Schematic Diagram
hc-05-serial_schem
hc-05 connections
hc-05 connections

 

And finally the source code 🙂

#include <SoftwareSerial.h>

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

void setup() {
  // Open serial communications with the PC
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port
  }

  pinMode(13,OUTPUT);

  // set the data rate for the bluetooth communication port
  mySerial.begin(38400);

}

void loop() {
  if (mySerial.available()) {
    char x = mySerial.read();
    Serial.write(x);
    if (x == 'H') { 
      digitalWrite(13,HIGH);
      mySerial.write("Ok - Turning On");
    }
    else if (x == 'L') { 
      digitalWrite(13,LOW);
       mySerial.write("Ok - Turning Off");
    }
    else  mySerial.write("Error");
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}

Now install a Bluetooth terminal on your phone and connect to the hc-05 module.

Once connected try sending ‘H’ to switch the LED on and ‘L’ to switch it off. Anything else you send will return an error, kind of.

Tags:

  1. [* Shield plugin marked this comment as “spam”. Reason: Failed GASP Bot Filter Test (comment token failure) *]
    HC-05 works at 3.3V, not 5V. In your diagram, even if it works, the module will stop working soon. You need to use a voltage level shifter in the TX/TX lines.

Leave a Reply

Your email address will not be published. Required fields are marked *