Here we will show how bluetooth module can be used with IR sensor to inform user remotely whenever the IR sensor detects an object. This can be helpful in robotics projects, manufacturing industry as well as in home automation. Intrusion, alarm detection, counting of object are some application examples of this where user is informed remotely. So here you will learn how to use bluetooth module hc-05, IR sensor with ATMega328 microcontroller.
The IR sensor and HC-05 bluetooth module is connected to the ATmega328p. When the IR sensor detects an object, it informs the ATmega328p microcontroller The ATmega328p then sends out object detected message to the user mobile phone via bluetooth. To communicate with a mobile phone using Bluetooth module ATmega328p USART is used. How ATmega328p USART can be programmed was explained Programming ATmega328p USART tutorial. A simple example of ATmega328p communication with mobile phone using HC-05 bluetooth module was demonstrated in tutorial HC-05 Bluetooth Module and ATmega328p USART Communication. An example of using IR sensor was demonstrated in the tutorial IR sensor with Arduino, LCD, Buzzer and LED.
Interfacing of ATmega328p HC-05 Bluetooth Module and IR sensor
The picture below shows connection between ATmega328p, HC-05 bluetooth module and IR sensor on a breadboard.
The schematic diagram of interfacing ATmega328p, HC-05 bluetooth module and IR sensor is provided below.
In the schematic, the bluetooth module TXD and RXD are connected to the RXD and TXD pins of the ATmega328 pins respectively. The RXD pin of the bluetooth module is 3.3V logic level pin so two resistors 1KOhm and 2.2KOhm forming resistor divider is used to transform the microcontroller 5V logic level to 3.3V. The Vcc and GND pins of the bluetooth module are connected to the microcontroller +5V and ground connections. Similarly, the IR sensor out pin is connected to the PORT C pin 0 of the microcontroller and the Vcc and GND are connected to the common +5V and ground of the microcontroller.
Programming & Coding of ATmega328p Bluetooth and IR sensor
Following is the C program code for wireless bluetooth communication and detection of object by the IR sensor.
#ifndef F_CPU
#define F_CPU 8000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
#define UBRRVAL 51 //9600 baud rate for 8MHz
void usart_init(void);
void sendbyte(unsigned char);
void sendstr(unsigned char *);
unsigned char defaultmsg[] = "Object Detected!:\n";
int main(){
DDRC &= ~(1<<PC0); //make PORTC pin 0 input for IR sensor
usart_init();
while(1)
{
while(PINC & (1<<PC0));
UCSR0B |= (1<<TXEN0);
sendstr(msg);
_delay_ms(3500);
UCSR0B &= ~(1<<TXEN0);
}
return 0;
}
void usart_init(void){
UBRR0H= (unsigned char)(UBRRVAL>>8); //high byte
UBRR0L=(unsigned char)UBRRVAL; //low byte
UCSR0B &= ~(1<<TXEN0);
UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00); //Set data frame format: asynchronous mode,no parity, 1 stop bit, 8 bit size
}
void sendbyte(unsigned char MSG){
while((UCSR0A&(1<<UDRE0)) == 0); // Wait if a byte is being transmitted
UDR0 = MSG;
}
void sendstr(unsigned char *s){
unsigned char i = 0;
while(s[i] != '\0'){
sendbyte(s[i]);
i++;
}
}
The usartinit() function configures the USART in 8 bit mode, 1 stop bit, with 9600 baud rate, in asynchronous mode. Here we have not enabled the transmitter by setting the TXEN0 bit in the UCSR0B. The transmitter is enabled only when the message has to be sent and after the transmitter has completed sending the message the transmitter is disabled back again. This is done so that the microcontroller does not continuously transmit message via its USART. Otherwise we get continuous message on the receiving mobile phone via bluetooth.
In the while loop, another while() function periodically checked whether the PORT C pin 0 is high or low. It remains there and the code below it are not executed until the port pin C goes high. When the IR detects objects it's out pin goes high and hence the port C pin 0 gets high. So when an object is detected then the program exits from the while() function and code below it are executed. First when an objected is detected, the USART transmitter is turned on, then the message is sent the bluetooth module via USART. After sending the message we wait for 3500ms so that USART does not send the same message "object detected!" multiple times. Then we turn off the USART transmitter.
The following video demonstrates how it works.
You might also be interested in demonstration of Communication between Microcontrollers using USART-ATmega328P and ATmega32A.