HC-05 Bluetooth Module Interfacing with AVR ATmega16/ATmega32

Bluetooth is a wireless communication protocol used to communicate over short distances. Here we design a smartphone-controlled LED using the HC-05 Bluetooth module.

61.4k views3 min readUpdated Sep 4, 2023
HC-05 Bluetooth Module Interfacing with AVR ATmega16/ATmega32

Overview of Bluetooth

HC-05 is a Bluetooth device used for wireless communication. It works on serial communication (USART).

  • It is a 6 pin module.
  • The device can be used in 2 modes; data mode and command mode.
  • The data mode is used for data transfer between devices whereas command mode is used for changing the settings of the Bluetooth module.
  • AT commands are required in command mode.
  • The module works on 5V or 3.3V. It has an onboard 5V to 3.3V regulator.

As the HC-05 Bluetooth module has a 3.3 V level for RX/TX and the microcontroller can detect 3.3 V level, so, no need to shift the transmit level of the HC-05 module. But we need to shift the transmit voltage level from the microcontroller to RX of the HC-05 module.

For more information about the HC-05 Bluetooth module and how to use it, refer to the topic Bluetooth module HC-05 in the sensors and modules section.

For information on USART in AVR ATmega16/ATmega32 and how to use it, refer the topic on USART in AVR ATmega16/ATmega32 in the ATmega basics section.

HC-05 Bluetooth Module

 

HC-05 Bluetooth Modules Pins
  1. Key: It is used to bring Bluetooth module in AT commands mode. If key pin is set to high, then this module will work in command mode. Otherwise by default it is in data mode. The default baud rate of HC-05 in command mode is 38400 bps.
  2. VCC: 5 V or 3.3 V. 
  3. GND: Ground.
  4. TXD: Transmit Serial data (wirelessly received data by Bluetooth module transmitted out serially on TXD pin)
  5. RXD: Receive data serially (received data will be transmitted wirelessly by Bluetooth module).
  6. State: It tells that module is connected or not (here not used). 
Testing via Computer Terminal

Following are some AT command generally used to change setting, customising of Bluetooth module.

To send these commands, we have to connect HC-05 Bluetooth module to the PC via serial to USB converter and transmit these commands through serial terminal of PC.

However, without changing any settings, we can also use bluetooth module HC-05 with it's default settings.

CommandDescriptionResponse
ATChecking communicationOK
AT+PSWD=XXXX

Set Password

e.g. AT+PSWD=4567

OK
AT+NAME=XXXX

Set Bluetooth Device Name

e.g. AT+NAME=MyHC-05

OK 
AT+UART=Baud rate, stop bit, parity bit

Change Baud rate

e.g. AT+UART=9600,1,0

OK
AT+VERSION?Respond version no. of Bluetooth module

+Version: XX OK

e.g. +Version: 2.0 20130107   OK

AT+ORGLSend detail of setting done by manufacturerParameters: device type, module mode, serial parameter, passkey, etc.

Connection Diagram of HC-05 Bluetooth Module With ATmega16/32

HC05 Bluetooth Module Interfacing with ATmega microcontroller
HC05 Bluetooth Module Interfacing with ATmega microcontroller

 

Controle the LED using HC-05 Bluetooth and ATmega16/32

Here let’s develop a small application in which we can control LED ON-OFF through a smartphone.

This is done by interfacing AVR-based ATmega16/ATmega32 with the HC-05 Bluetooth module. Data from HC-05 is received/ transmitted serially by ATmega16/32.

In this application, when 1 is sent from the smartphone, LED will Turn ON and if 2 is sent LED will get Turned OFF. If received data is other than 1 or 2, it will return a message to mobile that select the proper option.

 Programming steps

  1. Initialize ATmega16/ATmega32 USART communication.
  2. Receive data from the HC-05 Bluetooth module.
  3. Check whether it is ‘1’ or ‘2’ and take respective controlling action on the LED.

HC-05 Bluetooth Code for ATmega16/32

/*
   Bluetooth_Interface with ATmega16 to Control LED via smartphone
   http://www.electronicwings.com
 */ 

#include <avr/io.h>
#include "USART_RS232_H_file.h"	/* include USART library */

#define LED PORTB		/* connected LED on PORT pin */

int main(void)
{
	char Data_in;
	DDRB = 0xff;		/* make PORT as output port */
	USART_Init(9600);	/* initialize USART with 9600 baud rate */
	LED = 0;
	
	while(1)
	{
		Data_in = USART_RxChar();	/* receive data from Bluetooth device*/
		if(Data_in =='1')
		{
			LED |= (1<<PB0);	/* Turn ON LED */
			USART_SendString("LED_ON");/* send status of LED i.e. LED ON */
			
		}
		else if(Data_in =='2')
		{
			LED &= ~(1<<PB0);	/* Turn OFF LED */
			USART_SendString("LED_OFF"); /* send status of LED i.e. LED OFF */
		}
		else
			USART_SendString("Select proper option"); /* send message for selecting proper option */
	}		
}

 

Video of Bluetooth Communication using ATmega16

Components Used

Powered byMouser Electronics

Downloads

Comments28

Join the discussion — share a question or tip.

  • SA
    SaberBorjoeifar

    Hi Is it possible to make this module command only from a specific mobile phone?

  • JI
    JidanABC

    How do I get the library #include "USART_RS232_H_file.h"

  • JO
    josiaagt

    Hello, i have connected everthing to plan and i also used the same bluetooth terminal application on my smartphone. When i send 1,2 i get ??? on the terminal app.... I really need it for my project. Do you know what could be the reason? My Controller is also Atmega32/16 Also the LED doesnt light up when i send 1 through bluetooth

  • TH
    ThurunuChalitha

    Anyone working with HC-12 serial communication module between two ATmega32A?

  • RU
    RUTUJAGOGULWAR

    how to send data from microcontroller to bluetooth device

  • MO
    MortezaAskari

    hi how can use delay and can I change the number 1 to A for on the led

  • MS
    Msadr471

    Hi. For Interfacing HC-06 is different? or not? same as this should I write? thanks.

  • SH
    shani0tnt

    if i want to send String like " Total" from Bluetooth then how to handle this string and make my logic accordingly ...? Is there any function to convert string to char ?

    • LO
      lokeshc

      Though you are sending string but UART will transmit it character by character. You can receive each character and store them into the array for comparison. Once you received your desired string, you can compare the string and take control action. Ex. int i=0; char data_in[size]; char received_char; while(1){ while((received_char = USART_RxChar()) ! = '\0')){ data_in[i] = received_char; i++; } data_in[i] = '\0'; // adding null as last element in the array //{now compare your string with array element } } I have not tested it but you can write logic like this.

    • SH
      shani0tnt

      Replying to @lokeshc

      Thank you Mr. Lokeshc .. i tried that way as you mention but its not working.. still i can't get it.. plz help me please try once in hardware and then send that code.. your reply is very valuable for me . i am waiting .... thankyou so muchh

  • SH
    shani0tnt

    if i want to send String like " Total" from Bluetooth then how to handle this string and make my logic accordingly ...? Is there any function to convert string to char ?

  • PA
    pandeashutos

    We can get it here, https://www.youtube.com/watch?v=mCVtozmq3_0

  • OL
    olcaycaliskan· edited

    program is running . but how can we read the analog value ? example : int deneme = 50; deneme ??

  • MO
    mohamedsobhy2282

    hi bro... I write the code and no errors show and the program on phone is connected to hc05 but no action happens when running the code !!

  • MA
    madhumansukh

    SAme problem is occurring, the connection are okay but the commands are not properly working. it shows "??????", please help

  • SE
    sekay003

    Pour moi le code marche sur atmega 328p mais le bluetooth ne reçoit pas encore les données pouvez vous m'aider Mr?

    • LO
      lokeshc

      check your Bluetooth connection.

  • SA
    Samkelisiwe

    hi I need to connect two HC-05 to communicate with each other, how do i do that? which one must be a master and which must be a slave? Thanks in advance

    • LO
      lokeshc

      You can use any one HC-05 Bluetooth as a master and another one will be a slave. Just you have to configure them as master/slave using AT commands. For establishing communication between two HC-05 Bluetooth, you need AT command to connect them. This can be done by sending AT command to the slave device from controller. After establishing a connection between them, you can transfer communicate data between master and slave.

    • SA
      Samkelisiwe

      Replying to @lokeshc

      thank you Sir

  • HU
    husainwamique· edited

    Sir for this one only....... can you give me the codes for cv avr as this one seems for arduino

    • LO
      lokeshc

      This code is for avr atmega which built on Atmel Studio. Do you want to build this code on code vision (cv) avr?

  • SA
    sahasayan16

    Shows ? in the bluetooth terminal Code didn't work.

    • LO
      lokeshc

      Check your connections properly. Download the proper bluetooth terminal application on your smartphone. Some application have output in different format. If still it not work then let me know.

    • SA
      sahasayan16

      Replying to @lokeshc

      It didn't work. I feel there is some problem with Usart file.

    • LO
      lokeshc

      Replying to @sahasayan16

      You can try this application, it works for me https://play.google.com/store/apps/ details?id=ptah.apps.bluetoothterminal

    • BE
      Benax

      Replying to @lokeshc

      Hello, code dont work for me too :/ in terminal when i send 1 or 2 or 3 ... the answer always a lot of : ?? (question marks) ://