Ad Code

Home automation via Bluetooth

Home automation via Bluetooth 


Hi, in this project we will talk about home automation via Bluetooth connection. Automation via Bluetooth makes our life much easier, it can provide ease of access or automate our day to day tasks.

In this, we will try to control our day to day tasks like turning on and off the light bulb. controlling the fan and also controlling the water pump via a Bluetooth connected terminal.

Working principle

This project is based on the Bluetooth serial communication, we are using two Bluetooth module one is connected with the virtual terminal which will work transmitting the commands (this transmission side will work as a handheld device or mobile phone), and another module is connected to the microcontroller which will control the light bulb, fan and pump.

when we transmit 'a' through the virtual terminal the controller will turn on the bulb, and on transmitting 'b' it will turn off the light.

'a' -  turn on light
'b' - turn off light
'c' - turn on the fan
'd' - turn off the fan
'e' -  turn on the pump
'f' - turn off the pump


We will cover the topic in the following ways
  • Tools used
  • Components required
  • Making connections of all components
  • Using virtual serial port driver
  • Writing codes and generating hex files
  • Uploading the hex file to the controller
  • Simulating the project

Tools used

  • I used Proteus 8 professional for simulating the project.
  • MPlab IDE for writing the codes in C and generating the hex file.
  • The Virtual serial port driver for simulating Bluetooth communication.

Components required

The components required for the simulation of the projects are given below.
  1. PIC18F4520 (PIC controller)
  2. Bluetooth module (HC-05)
  3. virtual terminal
  4. 16x2 LCD display
  5. 5V Relay
  6. Electric bulb
  7. L293D (motor driver)
  8. Simple DC motor

In this simulation, I had used a simple DC motor for the simulation of Fan and water pump, if you want to operate an AC motor then, you just need to add a 5V relay between the controller and the motor.

Here the virtual terminal will act as a screen of your mobile device.


Making components connection

After selecting all the required components, now it's time to make all the connections correctly.

The picture of the connections is given below.


Please make sure all the terminals of the components are connected to the right pins of the microcontroller.

Using Virtual serial port driver

For simulating the Bluetooth communication a virtual serial port driver is essential.
You can download any virtual serial port driver from the net and simulate the project.


For establishing the virtual connection you need to click on the Bluetooth component connected to the virtual terminal and change the physical port to PORT 1 for virtual terminal side and PORT 2 for the controller side.


Now open your virtual serial port driver and establish the connection between port 1 and port 2.

Writing codes and generating HEX file

Now, the most important part, writing the code for the microcontroller and generating the hex files.
I am using the MPlab IDE for compiling and generating the hex file.
If you still confused about generating the hex file then you can click here.

The code for the microcontroller is given below.

#include <xc.h>
#define _XTAL_FREQ 20000000
#define led RB0
#define fan_pin1 RB1
#define fan_pin2 RB2
#define pump_pin1 RB3
#define pump_pin2 RB4
#define rs RC0
#define rw RC1
#define en RC2
char text1[]="To on light press 'a'";
char text2[]="To on fan press 'b'";
char text3[]="To off light press 'c'";
char text4[]="To off fan press 'd'";
char text11[]="To on pump press 'e'";
char text12[]="To off pump press 'f'";
char text5[]="#---------- Light is on -------------#";
char text6[]="#---------- Light is off -------------#";
char text7[]="#---------- Fan is on -------------#";
char text8[]="#---------- Fan is off -------------#";
char text13[]="#---------- Pump is on -------------#";
char text14[]="#---------- Pump is off -------------#";
char text9[]="Enter valid key";
char text10[]="#---- Welcome to Home Autoomation ----#";
int i;
void lcd_cmd(unsigned char command)
{
PORTD=command;
rs=0;
rw=0;
en=1;
__delay_ms(25);
en=0;
}
void lcd_data(unsigned char dat)
{
PORTD=dat;
rs=1;
rw=0;
en=1;
__delay_ms(25);
en=0;
}
void lcd_string(const unsigned char *str, unsigned char num)
{
unsigned char i;
for(i=0;i<num;i++)
{
lcd_data(str[i]);
}
}
void lcd_initialize()
{
lcd_cmd(0x38);
lcd_cmd(0x04);
lcd_cmd(0x0C);
lcd_cmd(0x01);
}
void press_enter() // press enter function
{
TXREG=0x0D;
while(TXIF==0);
TXIF=0;
}
void main()
{
TRISB=0;
TRISD=0;
TRISC0=0;
TRISC1=0;
TRISC2=0;
int value;
TXSTA=0x20;
SPEN=1;
RCSTA=0x90;
SPBRG=32;
lcd_initialize();
lcd_cmd(0x80);
lcd_string("Connected..!!",13);
while(1)
{
press_enter();
for(i=0;i<sizeof text10;i++)
{
TXREG=text10[i];
while(TXIF==0);
TXIF=0;
}
press_enter();
for(i=0;i<sizeof text1;i++)
{
TXREG=text1[i];
while(TXIF==0);
TXIF=0;
}
press_enter();
for(i=0;i<sizeof text2;i++)
{
TXREG=text2[i];
while(TXIF==0);
TXIF=0;
}
press_enter();
for(i=0;i<sizeof text3;i++)
{
TXREG=text3[i];
while(TXIF==0);
TXIF=0;
}
press_enter();
for(i=0;i<sizeof text4;i++)
{
TXREG=text4[i];
while(TXIF==0);
TXIF=0;
}
press_enter();
for(i=0;i<sizeof text11;i++)
{
TXREG=text11[i];
while(TXIF==0);
TXIF=0;
}
press_enter();
for(i=0;i<sizeof text12;i++)
{
TXREG=text12[i];
while(TXIF==0);
TXIF=0;
}
press_enter();
while(RCIF==0);
value=RCREG;
RCIF=0;
press_enter();
if(value=='a')
{
led=1;
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_string("LIGHT ON",8);
for(i=0;i<sizeof text5;i++)
{
TXREG=text5[i];
while(TXIF==0);
TXIF=0;
}
press_enter();
}
else if(value=='c')
{
led=0;
lcd_cmd(0x01);
lcd_cmd(0xC0);
lcd_string("LIGHT OFF",9);
for(i=0;i<sizeof text6;i++)
{
TXREG=text6[i];
while(TXIF==0);
TXIF=0;
}
press_enter();
}
else if(value=='b')
{
fan_pin1=1;
fan_pin2=0;
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_string("FAN ON",6);
for(i=0;i<sizeof text7;i++)
{
TXREG=text7[i];
while(TXIF==0);
TXIF=0;
}
press_enter();
}
else if(value=='d')
{
fan_pin1=0;
fan_pin2=0;
lcd_cmd(0x01);
lcd_cmd(0xC0);
lcd_string("FAN OFF",7);
for(i=0;i<sizeof text8;i++)
{
TXREG=text8[i];
while(TXIF==0);
TXIF=0;
}
press_enter();
}
else if(value=='e')
{
pump_pin1=1;
pump_pin2=0;
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_string("PUMP ON",7);
for(i=0;i<sizeof text13;i++)
{
TXREG=text13[i];
while(TXIF==0);
TXIF=0;
}
press_enter();
}
else if(value=='f')
{
pump_pin1=0;
pump_pin2=0;
lcd_cmd(0x01);
lcd_cmd(0xC0);
lcd_string("PUMP OFF",8);
for(i=0;i<sizeof text14;i++)
{
TXREG=text14[i];
while(TXIF==0);
TXIF=0;
}
press_enter();
}
else
{
lcd_cmd(0x01);
lcd_string("TRY AGAIN",9);
for(i=0;i<sizeof text9;i++)
{
TXREG=text9[i];
while(TXIF==0);
TXIF=0;
}
press_enter();
}
}
}

Upload the Hex file to the microcontroller

Now, after generating the hex file upload the code to the microcontroller.

This can be done by double-clicking on the microcontroller and select the hex file created.


Simulating the project

After uploading the hex file your project is ready for simulation. 

To simulate the project you have to click the play button on the bottom left side of the simulator.

Video