Interfacing HC-05 Bluetooth module with AVR Microcontroller Using Atmega

Interfacing HC 05 Bluetooth modul

In this tutorial let us learn How to interface HC-05 Bluetooth Module with AVR ATmega8 microcontroller. We will establish communication between Android mobile and Atmega8 through Bluetooth module which takes place through UART serial communication protocol. In this project we will control a LED using Bluetooth of our smartphone.

Interfacing HC 05 Bluetooth modul

Material Required:

  1. AVR Atmega8
  2. HC-05 Bluetooth module
  3. LED
  4. USBASP programmer
  5. 10-pin FRC cable
  6. Jumper wires
  7. Breadboard

Software Used:

We will use CodeVisionAVR software for writing our code and SinaProg software for uploading our code to Atmega8 using USBASP programmer.

You can download these softwares from the given links:

CodeVisionAVR http://www.hpinfotech.ro/cvavr_download.html

SinaProg http://www.ablab.in/sinaprog-hex-file-downloader-software-to-download-code-to-avr-microcontroller/#inline_content

Bluetooth Module HC-06:

Bluetooth can operate in the following two modes:

  1. Command Mode
  2. Operating Mode

In Command Mode we will be able to configure the Bluetooth properties like the name of the Bluetooth signal, its password, the operating baud rate etc. The Operating Mode is the one in which we will be able to send and receive data between the PIC Microcontroller and the Bluetooth module. Hence in this tutorial we will be toying only with the Operating Mode. The Command mode will be left to the default settings. The Device name will be HC-05 (I am using HC-06) and the password will be 0000 or 1234 and most importantly the default baud rate for all Bluetooth modules will be 9600.

Bluetooth Module HC 06

The module works on 5V supply and the signal pins operate on 3.3V, hence a 3.3V regulator is present in the module itself. Hence we need not worry about it. Out of the six pins only four will be used in the Operating mode. The pin connection table is shown below

S.No

Pin on HC-05/HC-06

Pin name on MCU

Pin number in PIC

1

Vcc

Vdd

31st pin

2

Vcc

Gnd

32nd pin

3

Tx

RC6/Tx/CK

25th pin

4

Rx

RC7/Rx/DT

26th pin

5

State

NC

NC

6

EN (Enable)

NC

NC

Check our other projects to learn more about Bluetooth module HC-05 with other microcontrollers:

Creating the Project for Atmega 8 using CodeVision:

After installing these softwares follow the below steps to create project and writing code:

Step 1. Open CodeVision Click on File -> New -> Project. Confirmation Dialogue box will appear. Click On Yes

Creating the Project for Atmega 8

Step 2.  CodeWizard will open. Click on first option i.e. AT90, and click OK.

CodeWizard

Step 3. Choose your microcontroller chip, here we will take Atmega8 as shown.

Choose your microcontroller chip

Step 4. Click on Ports. In our project, we will be using Port C4 and C5 for led interfacing. So, make Bit 4 and bit 5 as output by clicking on it. As shown below:

As shown below

Step 5. We will use USART for Rx and Tx. So, click on USART option and click on Receiver option and leave rest as it is.

We will use USART for Rx and Tx. S o click on USART

Step 6. Click on Program -> GenerateSave and Exit. Now, more than half of our work is completed

Click on Program Generate Save and Exit.

Step 7. Make a New folder on desktop so, that our files remains in folder otherwise it we will be scattered on whole desktop window. Name your folder as you want and I suggest use the same name to save program files.

We will be having three dialogues box one after other to save files

We will be having three dialogues box one after other to save files.

Do the same with other two dialogue boxes which will appear after you save the first.

Now, your workspace looking like this.

Now your workspace looking like this

Our most of the work is completed with the help of the Wizard. Now, we have to write only few lines of code to interface the bluetooth module and control the LED.

Circuit Diagram:

Circuit diagram for interfacing Bluetooth HC-05 with AVR is given below.

Circuit Diagram

Hookup the one side of FRC cable to USBASP programmer and other side will connect to SPI pins of microcontroller as described below:

connect to programmer

  1. Pin1 of FRC female connector -> Pin 17 ,MOSI of Atmega8
  2. Pin 2  connected to Vcc of atmega8 i.e. Pin 7
  3. Pin 5 connected to Reset of atmega8 i.e. Pin 1
  4. Pin 7 connected to SCK of atmega8 i.e. Pin 19
  5. Pin 9 connected to MISO of atmega8 i.e. Pin 18
  6. Pin 8 connected to GND of atmega8 i.e. Pin 8

Connect the remaining components on the breadboard as per circuit diagram.

Code and Explanation:

Complete AVR code with Demonstration Video is given at the end of the article.

Here we have declared a variable in void main function for storing incoming character from Bluetooth module.

#include <io.h>
// Declare your global variables here
// Standard Input/Output functions
#include <stdio.h>
void main(void)
{
char var;                 // Declare your local variables here

Rest of the code is easy and can be easily understandable. Now, come on the last lines of code where you will find a while loop. Our main code will be in this loop. Here we are continuously checking the incoming character from the Bluetooth module and turning the LED On or off accordingly.

while (1)
      {
       scanf("%c",&var);                    //this function is to used to check any character coming from our android app .                    
      if (var == 'a')                              // We will send ‘a’ from Bluetooth Terminal to ON the LED and ‘b’ to OFF the LED
      {
            PORTC.5 = 1;
            PORTC.4 = 0;
      }      
          if (var == 'b')
      {
            PORTC.5 = 0;
            PORTC.4 = 0;
      }     
      }

Our code is completed. Now, we have to build our project. Click on Build the project icon as shown.

code is completed

After building the project, a HEX file is generated in the Debug-> Exe folder which can be found in the folder you make previously to save your project. We will use this HEX file to upload in atmega8 using Sinaprog software.

Uploading the code to Atmega 8 using Sinaprog:

We will upload the above generated Hex file using the Sinaprog, so open it and Choose Atmega8 from Device drop down menu. Select the HEX file from the Debug-> Exe folder as shown.

Uploading the code to Atmega 8

Now, Click on Program.

Now Click on Program

Your Microcontroller is programmed. Now, we need an Android app to connect with our module. We will use “Bluetooth Terminal app” to control the LED light.  

Android App for controlling LED using AVR:

We will use “Bluetooth Terminal app” in our smart phone to talk to the HC-05 at the other end to control the LED light.  This app can be downloaded from given link:

https://play.google.com/store/apps/details?id=Qwerty.BluetoothTerminal&hl=en_IN

You can power up your circuit using the same usbasp by plugging it into your PC or you can apply external 5V (not more than 5!!!!!) to the Atmega8 Vcc pin.

After installing it .Open the app and connect it with Bluetooth module (HC-05 and default password is 1234).

Android App for controlling LED

Now, send ‘a’ and see the led will glow. Send ‘b’, led will off.

Now send ‘a and see the led will glow. Send b led will off

So now by sending the character ‘a’ and ‘b’, you can control the LED wirelessly using your Smartphone. And if you use the voice keyboard with Bluetooth app then you don’t even need to type the character ‘a’ and ‘b’, you can just speak them and can control the LED using your voice.

Leave a Comment

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