View Course Path

Bluetooth (HC-05) interfacing with 8051 with practical application

Here’s what you’ll be able to accomplish by the end of this post:

  • Interface the HC-05 Bluetooth module with an 8051 and use it to control a couple of LEDs via a relay.
  • Learn to write a C program to control a peripheral device connected via a relay to 8051 based on the data received from the smartphone via Bluetooth.
  • Build the circuit step by step to implement the above using the HC-05 Bluetooth module and AT89C51 microcontrollers.
  • Learn how to set up an app on your smartphone to access the microcontroller via Bluetooth.

Who wouldn’t want to control all the appliances in their home with just a touch of a button on your smartphone? Wireless technology has expanded tremendously over the past few decades, and Bluetooth wireless technology is one of a kind, the process of transmission and reception of data between two devices has become super easy with the help of Bluetooth technology. The Bluetooth protocol operates at 2.4GHz in the same unlicensed ISM frequency band where RF protocols like ZigBee and WiFi also exist. This is the reason why we’re still using Bluetooth in our day to day lives. You can get a quick overview of the Bluetooth standard here.

Let’s just dive into the tutorial by making ourselves familiar with all the components that are required for the interfacing.

Components Required

  1. 8051 microcontroller – AT89C51/AT89S51/52 or similar variants.
  2. Oscillator crystal – 12Mhz
  3. Capacitors – 22pF x 2, 10µF x 1
  4. Resistors – 10kΩ x 1
  5. LED’s x 2
  6. Bluetooth Module – HC-05
  7. ULN2003a x 1
  8. Relay x 2

Working of Bluetooth module (HC-05)

The HC-05 Bluetooth Module is an easy-to-use Bluetooth serial module. It is designed for transparent wireless serial connection setup. Its communication is via the serial mode, which makes it easy to interface it with any microcontroller.

The HC-05 Bluetooth module provides a switching mode between master and slave mode. A single master device can be connected to up to several different slave devices. But, a slave device in the Bluetooth network can only be connected to a single master. The master device can send data to any of its slaves and request data from them as well, but, the slaves are only allowed to transmit to and receive from their master. They can’t talk to other slaves in the network.

The HC-05 Bluetooth module can be configured as a master device or a slave device with the help of AT commands which you’ll understand in detail in the further sections.

HC-05 bluetooth module

To enable Bluetooth communication, the HC-05  module is interfaced with the 8051 microcontroller via the built-in UART circuitry present in the microcontroller. And the data is transmitted in the form of packets. The 8051 microcontrollers have inbuilt pins P3.0 (RXD) and P3.1 (TXD) for receiving and transmitting the data from/to the HC-05 Bluetooth module respectively.

Features of Bluetooth module

Here are some specifications of the HC-05 Bluetooth module:

  • Bluetooth protocol: Bluetooth Specification v2.0 + EDR.
  • The frequency is 2.4GHz ISM band.
  • The range of operation is less than 100m.
  • It uses frequency-hopping spread spectrum (FHSS) radio technology to send data over the air.
  • The emission power is less than 4dBm, and it is a Class 2 Bluetooth Module.
  • The operating current of the HC-05 Bluetooth module is 30mA.
  • The operating voltage of the HC-05 Bluetooth module is 3.3V- 5V.
  • Can operate in Master, Slave or Master/Slave mode
  • The dimensions are 29.9mm x 13mm x 2.2mm.
  • It can be easily interfaced with laptops or smartphones.
  • It has a security overlay in the form of authentication and encryption.
  • Supported baud rates are 9.6, 19.2, 38.4, 57.6, 115.2, 230.4, 460.8 Kbps.

Pin diagram

Pin diagram of HC-05

Pin Number Pin Name Description
1 Enable / Key This pin is used to toggle between Connection Mode and AT command mode. By default, it is in Connection mode. LOW – Connection Mode, HIGH – AT command mode.
2 VCC Powers the module. Connect to +5V Supply voltage
3 GND Ground pin of the module, connect to system ground.
4 TXD – Transmitter Transmits Serial Data. Everything received via Bluetooth will be given out by this pin as serial data.
5 RXD – Receiver Receive Serial Data. Every serial data given to this pin will be broadcasted/ transmitted via Bluetooth.
6 State The state pin is connected to an on-board LED, and it can be used as feedback to check if Bluetooth is working properly.
7 LED Indicates the status of Module
Repeated Blinking: Waiting for connection in Connection Mode
Blink twice in 1 sec: Connection successful in Connection Mode
Blink once in 2 sec: Module has entered AT Command Mode
8 Button Used to control the Key/Enable pin to toggle between Connection and command Mode

Modes of operation – AT and Connection

AT Command Mode

  • Whenever the Enable (EN)/key command is set HIGH, the AT command mode is activated.
  • The AT commands are set of commands which are used to set up and configure the Bluetooth module.
  • In this mode, the module is not discoverable by the other Bluetooth devices.
  • The commands are sent to the module serially like string.
  • The string should be in the capital letters, and every command should be appended with ‘rn’ at the end.
  • If the command is known by the module, then the module will reply with the string: ‘OK’.
  • If the command is unknown by the module, then it will reply with the string: ERROR ( ).
  • The error determines the type of error and a specific code.
    AT commands
  • The AT commands are used to set the password for Bluetooth pairing or change the name of the connection.
  • Also, to check or change the baud rate, AT commands are written in the Bluetooth Terminal.
  • We don’t need to dive deep into the AT commands because we are using the serial communication of the HC-05 Bluetooth module. We will be using the module with its default settings. For more information on using AT commands to set up the module, refer to its datasheet.

Connection Mode

  • When the Enable (EN)/key is LOW, the connection mode is set.
  • In the connection mode, this device can connect directly to any other device.
  • We can discover/ identify this device on other devices and can connect with it using a default passkey which is usually ‘1234’ or ‘0000’.
  • The configuration cannot be changed, but we can communicate with it serially.

Steps to set up the mobile app

Now how do we send data to the 8051 via the Bluetooth module? Just turning on the Bluetooth on your phone won’t suffice. Here’s what you have to do.

  • Step 1: Download any Bluetooth terminal application on your smartphone. Just launch the app store and search for “BT Terminal” or “Bluetooth Terminal” and install any application with a good rating.
  • Step 2: Go to the Bluetooth settings on your phone and click on “Add new device”. Select the device named “HC-05”, the default passkey is “1234” or “0000”.
  • Step 3: Now that your smartphone is paired with the Bluetooth module, which is connected to the 8051 microcontroller, you can send data serially into the microcontroller via Bluetooth.
  • Step 4: For that, open the application in your phone and make sure you select the required baud rate. The default baud rate of the HC-05 module is 9600 bps.
  • Step 5: The setup is complete now. All you need to do is program the microcontroller to respond to whatever serial data you’ll be sending via the Bluetooth module. We’ll do that in the next section.

Working principle of the project

Our main goal is to receive information wirelessly via the Bluetooth module connected to the microcontroller to control the pins P0.0 and P0.1, which are connected to the LEDs via a relay.

Here, we’re using a current amplifying IC – the ULN2003a (also called the relay driver), because the output current of pins of 8051 is a maximum of 10mA which is not sufficient. The output of this relay driver is given to a relay to which the appliance is connected, in our case, the LEDs.

relay is an electrically operated switch or an electromechanical switch. Relays are used where it is necessary to control an electronic circuit by a low-power signal. It can be broken down mainly into two parts, the electromagnet (coil over a permanent magnet), and the contactor (the switch). Current flowing through the coil of the relay creates a magnetic field that attracts the lever and changes the switch contacts. We have covered relays in-depth in our post on direct relay interfacing with the 8051.

We’ll execute the following logic to receive characters from the smartphone via Bluetooth and control the LEDs.

  • If the character ‘A’ is received, P0.0 is set to LOW, which implies LED 1 is turned ON.
  • If the character ‘a’ is received, P0.0 is set to HIGH, which implies LED 1 is turned OFF.
  • If the character ‘B’ is received, P0.1 is set to LOW, which implies LED 2 is turned ON.
  • If the character ‘b’ is received, P0.1 is set to HIGH, which implies LED 2 is turned OFF.

All this is possible due to serial communication with UART present in the 8051 microcontrollers. Feel free to brush up your concepts on serial communication with UART like how to select timer, how to set baud rate, how to transfer or receive data using UART in 8051 here: Serial communication with UART in 8051.

To summarize the configuration in which we execute the serial communication,

  1. Timer 1 is selected as a timer in Mode 2 (8-bit auto-reload mode) by using TMOD = 0x20; command.
  2. The UART frame consists of 10-bit of data to be transmitted or received, including one start bit and one stop bit.
  3. A baud rate of 9600 bps is set by using TH1 = 0xFD; command.
  4. The SCON register (Serial Control) is set in such a way that it is selected in Mode 2 and the REN (reception) bit is set by using SCON = 0x50; command.

Now that we’ve all the ingredients required to interface a Bluetooth module with 8051 let’s move on to understanding how to set up the circuit, and write the C program to control the LEDs based on the data received.

Circuit diagram to interface Bluetooth module and relay with 8051

  • Step 1: If you’re using Proteus or and other simulation software or even hardware, select the AT89C51 or AT89S51 microcontroller or any other compatible variant.
  • Step 2: Connect a 12 MHz oscillator between pin 18 and 19.
  • Step 3: Connect two capacitors of 22pF, with one terminal on either side of the oscillator and the other terminal to ground, as shown below.
  • Step 4: Set Pin 31, i.e., EA pin to HIGH by connecting it to the +5V DC source.
  • Step 5: Now, to make the RESET circuit, connect Pin 9 (RST) to +5V through a capacitor of 10µF and connect the same pin to +0V (GND) through a 10kΩ resistor or a potentiometer.
  • Step 6: Connect two LEDs to the microcontroller such that they turn ON when a low pulse is given through Port 0’s output. For this, ULN2003a driver is used, pin P0.0 and P0.1 are connected to 1B and 2B of ULN2003A respectively.
  • Step 7: The output pins 1C and 2C of ULN2003a are given to one terminal of the coil of the relay and the other terminal of the coil of the relay is connected to the COM pin of ULN2003A which is connected to a DC supply.
  • Step 8: One side of the contactor of the relay is connected to the cathode of the LED, and the anode is connected to the power supply. Connect the other side of the contactor to the COM pin of ULN2003A.
  • Step 9: Here comes the main part, connect the RXD (P3.0) and TXD (P3.1) of the 8051 microcontrollers to TXD and RXD pins of HC-05 Bluetooth module respectively.
  • Step 10: VCC and GND pins of the HC-05 Bluetooth module are to be given to +5V/3.3V and GND, respectively, for the module to turn ON.
  • Step 11: Ignore the other pins of HC-05, as they do not contribute to our project.

Circuit diagram to interface bluetooth to 8051 using a relay

C Program to interface the HC-05 Bluetooth module with 8051

#include<reg51.h>
#include<stdio.h>

sbit LED1 = P0^0; //Assign LED1 to pin P0.0
sbit LED2 = P0^1; //Assign LED2 to pin P0.1

void main()
{
    char mychar; // Assign a variable for the character to be received

    TMOD = 0x20; // Set time Timer 1 in 8-bit auto-reload mode (mode 2) 
    TH1 = 0xFD; // FD = -3, to send/ receive the data at 9.6Kbps
    SCON = 0x50; // Serial Control is set in Mode 1, with REN = 1 (reception ON)
    TI = 0; // Initialize the Timer Flag
    RI = 0; // Initialize the Timer

    TR1 = 1; // Start the timer (Timer 1)

    while(1) // Runs forever until power supply is cutoff
    {
        while (RI == 0); // Wait till the data is recieved, as soon as the data is received completely, the RI flag is set
        RI = 0; // Clear the RI flag to declare the availability for reception of another data

        if (mychar == 'A') // If the data received through the Bluetooth module is the character 'A'
        {
            LED1 = 0; // Turn ON LED 1
        } 
        else if (mychar == 'a') // If the data received through the Bluetooth module is the character 'a'
        {
            LED1 = 1; // Turn OFF LED 1 
        }

        if (mychar == 'B') // If the data received through the Bluetooth module is the character 'B'
        {
            LED2 = 0; // Turn ON LED 2
        }
        else if (mychar == 'b') // If the data received through the Bluetooth module is the character 'b'
        {
            LED2 = 1; // Turn OFF LED 2 
        } 
    } 
}

We hope you had fun building this project. Check out the other projects in our free 8051 course. If you have any queries regarding this one, the comments section below awaits your presence!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.