HC-05 Bluetooth Module Interfacing with AVR ATmega16/ATmega32
Introduction
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 HC-05 Bluetooth module has 3.3 V level for RX/TX and the microcontroller can detect 3.3 V level, so, no need to shift transmit level of the HC-05 module. But we need to shift the transmit voltage level from the microcontroller to RX of HC-05 module.
For more information about HC-05 Bluetooth module and how to use it, refer 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 inside section.

HC-05 Bluetooth Module
Interfacing Diagram

HC05 Bluetooth Module Interfacing with ATmega microcontroller
Example
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 HC-05 Bluetooth module. Data from HC-05 is received/ transmitted serially by ATmega16/32.
In this application, when 1 is sent from 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 message to mobile that select proper option.
Programming steps
- Initialize ATmega16/ATmega32 USART communication.
- Receive data from HC-05 Bluetooth module.
- Check whether it is ‘1’ or ‘2’ and take respective controlling action on LED.
Program
/*
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 */
}
}
No comments:
Post a Comment