• nithya7rns

Controlling Home Appliances using Arduino and HC-05 Bluetooth module

Updated: Dec 31, 2020

Interfacing Electronic components and building small devices is the way to explore our technical knowledge as well as our creativity. These devices can be built on a small scale as projects and also on a large and more practical level as a #Home Automation system.


In this article, we are going to learn how to control Home #Appliances using the Arduino board and HC-05 Bluetooth module. Let us start with the Hardware requirements for the circuit.


Hardware required

All components required.
All components required.
  1. Arduino UNO


Arduino UNO
Arduino UNO

#Arduino board is a microcontroller that is used to accept inputs from sensors connected and provide an output action on the desired device connected to it. The sensor inputs can be from light-detecting sensors, motion sensors (Ultrasonic or IR), temperature sensors, etc. The output function done on the devices can be turning on an LED, the sound of a buzzer, or a Serial monitor display.


Buy Arduino UNO from here.


2. HC-05 Bluetooth Module


HC-05 Bluetooth Module
HC-05 Bluetooth Module

It is a Serial #Bluetooth module for Arduino and other microcontrollers. The range of Operating Voltage is 4V to 6V. It operates with a current of 30mA. The Bluetooth devices have a certain radius of distance inside which it can communicate to the device connected. This device has a range of around 100m. The Bluetooth module uses serial communication to control other devices connected. It can operate in Master, Slave, or Master/Slave mode though the default mode is Slave mode.


Buy the HC-05 Bluetooth Module from here.


3. 2-Channel Relay


2-Channel Relay
2-Channel Relay

A relay is basically a switch that works on the principle of #Electromagnetism. It operates on a small amount of current to control a large amount of current flow. When current flows through the circuit, it activates the electromagnet. This in turn generates a magnetic field. One of the two magnets of the switch attracts the component of the other magnet which causes the switch to open and close every time a current passes through it.


Buy 2-Channel Relay from here.


4. AC Bulbs (with Bulb holder)


AC Bulb
AC Bulb

These are light-emitting devices. In this project, AC Bulbs are used instead of #LEDs. Also, LEDs can be used to Automate and control.


NOTE: In this project demonstration, we have used a Mosquito repellent machine which is controlled and is a part of the Automation #system. You can use another Bulb, LED, DC Motor, etc.


5. Jumper Wires


Jumper Wires
Jumper Wires

These are the main components that are used to establish the connections between different devices of the circuit.


Buy Jumper Wires from here.


Software Requirements


  1. Arduino IDE


Arduino IDE Logo
Arduino IDE Logo

#Arduino IDE (Integrated Development Platform) which is used to interface the boards like NodeMCU to run the desired code designed to interface hardware devices.


2. Arduino Bluetooth Controller App for Android



Arduino bluetooth controller- all in one App - logo
Arduino bluetooth controller- all in one App - logo

This is an #App for Android devices that is used to interface the device or circuit built. It allows controlling operations using Buttons, Timers, Dimmers, and Controllers, etc.



Circuit Connections


Circuit connections for this device can be understood by dividing them into different parts.




Circuit Diagram
Circuit Diagram

Part 1: Connections between the Bluetooth Module and Arduino.

We will be using four out of six pins of the Bluetooth module to connect it to the Arduino board. The VCC pin of the Bluetooth Module is connected to the VIN terminal of Arduino. The Ground pin of #HC-05 is connected to the GND terminal of Arduino. The TXD( Transmission Data) pin of HC-05 is connected to the RX (Receiving) terminal of Arduino. The RXD (Receiving Data) pin HC-05 is connected to the TX (Transmitting) terminal of Arduino.


These connections between RX, TX, and TXD, RXD pins of Arduino and Bluetooth module respectively show that both Arduino and the Bluetooth module are capable of receiving and transmitting data. So the data/inputs transmitted by one of the two Boards are going to be received by one of them on the other end.


Part 2: Connections between the Arduino UNO board and the Relay Module

Next, let us see how the relay module is connected to Arduino. As usual, the power supply terminal, 5V of the Arduino is connected to the VCC terminal of the Relay module and the same with the Ground terminals of both the components (GND -> GND).


The Digital pin 9 of Arduino is connected to the IN1 (Input pin 1) of the #Relay Module. Remember that PIN 9 of Arduino can also give PWM (Pulse Width Modulated) output. Next, PIN 8 (digital pin) is connected to IN2 (Input pin 2) of the Relay module. The connections between these two components end here.


Part 3: Connections between the Relay Module and the other Components (Which we will be controlling).

So, it is to be noted that, the article and the video give an explanation where an AC bulb and another Electronic component ( Mosquito Repellent Device) are the components used that are going to be Automated. But, we can also use two LEDs to build the same circuit and work with them.


  • Case 1: When two LEDs are used as the components to be Automated.

When two LEDs are used, the connection is as follows. The COM (common) pin of the Relay module is connected to the Negative terminal of the LED and the Positive terminal of the LED is connected to the power supply. The negative terminal of the power supply must be connected to the NO (Normally Open) pin of the relay module.


NOTE: The power supply module can be used as the main power supply to the LEDs.


  • Case 2: When other components are used as output.

We can use AC Bulbs and other small electronic components as output devices. The circuit shown in the video has an AC bulb and a Mosquito Repellent Machine as output and is Automated. The positive terminal of the bulb is connected to the power supply and the negative terminal of the bulb is connected to the COMM (Common) pin of the relay. The Negative terminal of the power supply is connected to the NO (Normally Open) pin of the relay.


Codes


int lamp1 = 2;
int lamp2 = 3;
int lamp3 = 4;
int music = 5;
int pc = 6;
int mobile = 7;
int fan = 8;
int socket = 9;

We start the program code with the variable definitions. Each component is given a specific PIN and the value of the pin is assigned to unique variables. The above lines code define variables for three lamps, a music device, a PC, a mobile phone, a fan, and a socket each of which is initialized with the value of the PINs.

int Received = 0;
int lamp1_state = 0;
int lamp2_state = 0;
int lamp3_state = 0;
int music_state = 0;
int pc_state = 0;
int mobile_state = 0;
int fan_state = 0;
int socket_state = 0;

Next, the state of each of the components connected is defined using different variables. The state of each of the variables is given as 0 which implies that they are at a LOW state (OFF).

void setup() {

  Serial.begin(38400);
  pinMode(lamp1, OUTPUT);
  pinMode(lamp2, OUTPUT);
  pinMode(lamp3, OUTPUT);
  pinMode(music, OUTPUT);
  pinMode(pc, OUTPUT);
  pinMode(mobile, OUTPUT);
  pinMode(fan, OUTPUT);
  pinMode(socket, OUTPUT);
}

The void setup( ) function is used to define the state and mode of each of the components connected. The serial transmission of the data begins with the #baud rate of 38400 which is the rate used for data transmission to the Bluetooth device. The rest of the statements of the setup( ) function are pinMode( ) functions which define each component to be OUTPUT (in this project all #components connected are OUTPUT). With this the setup( ) function ends.

void loop() {
  if (Serial.available() > 0)
  {
    Received = Serial.read();
  }

The loop( ) function is used to run the lines of code repeatedly. The first "if" condition is to check if the data is transmitted through the serial line. If the data is #transmitted, the received data is stored in the variable "Received" using Serial.read( ) function.

if (lamp1_state == 0 && Received == '1')
  {
    digitalWrite(lamp1, HIGH);
    lamp1_state = 1;
    Received = 0;
  }

The next "if" statement checks two conditions, if the state of the lamp is LOW (0) and if the data is #received. If both the conditions checks true, the digital data are written which changes the state of the lamp1 to be HIGH. This means that the value of "lamp" is HIGH and the value of "lamp1_state" is 1. The value of Received is initialized back to 0 and the "if" condition ends.

if (lamp1_state == 1 && Received == '1')
  {
    digitalWrite(lamp1, LOW);
    lamp1_state = 0;
    Received = 0;
  }

The consecutive "if" condition is similar to the previous one except that it checks if the state of the lamp is 1 instead of 0. If so and if the value of the "Received" variable is 1, the value of lamp1 is written as LOW, the lamp1_state is initialized to 1, and the Received variable is also initialized back to 0.

 if (lamp2_state == 0 && Received == '2')
  {
    digitalWrite(lamp2, HIGH);
    lamp2_state = 1;
    Received = 0;
  }
  if (lamp2_state == 1 && Received == '2')
  {
    digitalWrite(lamp2, LOW);
    lamp2_state = 0;
    Received = 0;
  }

The same set of "if" conditions are checked for each component. The above set of conditions is for lamp2. First, it checks if the lamp2_state value is 0, then the value of lamp2_state is equated to 1 and switches the lamp to HIGH (ON). If the state of lamp2 is 1, the value of lamp2_state is equated to 0 and it switches the lamp to LOW (OFF). Each time it initializes the value of Received back to 0.

if (lamp3_state == 0 && Received == '3')
  {
    digitalWrite(lamp3, HIGH);
    lamp3_state = 1;
    Received = 0;
  }
  if (lamp3_state == 1 && Received == '3')
  {
    digitalWrite(lamp3, LOW);
    lamp3_state = 0;
    Received = 0;
  }

Next, the conditions for lamp3 is validated. We can easily understand using these flow statements.

If Received =3,

lamp3_state = 0

then, lamp3 -> HIGH

lamp3_state -> 1

lamp3_state = 1

then, lamp3 -> LOW

lamp3_state -> 0

Each time the value of Recieved is equated to 0.

if (music_state == 0 && Received == '4')
  {
    digitalWrite(music, HIGH);
    music_state = 1;
    Received = 0;
  }
  if (music_state == 1 && Received == '4')
  {
    digitalWrite(music, LOW);
    music_state = 0;
    Received = 0;
  }

Next, the conditions for "music" is validated. We can easily understand using these flow statements.

If Received =4,

music_state = 0

then, music -> HIGH

music_state -> 1

music_state = 1

then, music -> LOW

music_state -> 0

Each time the value of Recieved is equated to 0.

if (pc_state == 0 && Received == '5')
  {
    digitalWrite(pc, HIGH);
    pc_state = 1;
    Received = 0;
  }
  if (pc_state == 1 && Received == '5')
  {
    digitalWrite(pc, LOW);
    pc_state = 0;
    Received = 0;
  }

Next, the conditions for "pc" is validated. We can easily understand using these flow statements.

If Received = 5,

pc_state = 0

then, pc -> HIGH

pc_state -> 1

pc_state = 1

then, pc -> LOW

pc_state -> 0

Each time the value of Recieved is equated to 0.

if (mobile_state == 0 && Received == '6')
  {
    digitalWrite(mobile, HIGH);
    mobile_state = 1;
    Received = 0;
  }
  if (mobile_state == 1 && Received == '6')
  {
    digitalWrite(mobile, LOW);
    mobile_state = 0;
    Received = 0;
  }

If Received = 6,

mobile_state = 0

then, mobile -> HIGH

mobile_state -> 1

mobile_state = 1

then, mobile -> LOW

mobile_state -> 0

Each time the value of Recieved is equated to 0.

if (fan_state == 0 && Received == '7')
  {
    digitalWrite(fan, HIGH);
    fan_state = 1;
    Received = 0;
  }
  if (fan_state == 1 && Received == '7')
  {
    digitalWrite(fan, LOW);
    fan_state = 0;
    Received = 0;
  }

If Received = 7,

fan_state = 0

then, fan -> HIGH

fan_state -> 1

fan_state = 1

then, fan -> LOW

fan_state -> 0

Each time the value of Recieved is equated to 0.

 if (socket_state == 0 && Received == '8')
  {
    digitalWrite(socket, HIGH);
    socket_state = 1;
    Received = 0;
  }
  if (socket_state == 1 && Received == '8')
  {
    digitalWrite(socket, LOW);
    socket_state = 0;
    Received = 0;
  }
}

If Received = 8,

socket_state = 0

then, socket -> HIGH

socket_state -> 1

socket_state = 1

then, socket -> LOW

socket_state -> 0

Each time the value of Recieved is equated to 0. The void loop( ) function ends here.


NOTE: In the #code given above, multiple values are defined so that various components can be connected to the device. The required number of components can be chosen and the respective number of conditions can be used by just changing the component name. Refer to the video to start with two components to control.


COMPLETE CODE


int lamp1 = 2;
int lamp2 = 3;
int lamp3 = 4;
int music = 5;
int pc = 6;
int mobile = 7;
int fan = 8;
int socket = 9;

int Received = 0;
int lamp1_state = 0;
int lamp2_state = 0;
int lamp3_state = 0;
int music_state = 0;
int pc_state = 0;
int mobile_state = 0;
int fan_state = 0;
int socket_state = 0;

void setup() {

  Serial.begin(38400);
  pinMode(lamp1, OUTPUT);
  pinMode(lamp2, OUTPUT);
  pinMode(lamp3, OUTPUT);
  pinMode(music, OUTPUT);
  pinMode(pc, OUTPUT);
  pinMode(mobile, OUTPUT);
  pinMode(fan, OUTPUT);
  pinMode(socket, OUTPUT);

}

void loop() {

  if (Serial.available() > 0)
  {
    Received = Serial.read();

  }

  if (lamp1_state == 0 && Received == '1')
  {
    digitalWrite(lamp1, HIGH);
    lamp1_state = 1;
    Received = 0;
  }
  if (lamp1_state == 1 && Received == '1')
  {
    digitalWrite(lamp1, LOW);
    lamp1_state = 0;
    Received = 0;
  }


  if (lamp2_state == 0 && Received == '2')
  {
    digitalWrite(lamp2, HIGH);
    lamp2_state = 1;
    Received = 0;
  }
  if (lamp2_state == 1 && Received == '2')
  {
    digitalWrite(lamp2, LOW);
    lamp2_state = 0;
    Received = 0;
  }

  if (lamp3_state == 0 && Received == '3')
  {
    digitalWrite(lamp3, HIGH);
    lamp3_state = 1;
    Received = 0;
  }
  if (lamp3_state == 1 && Received == '3')
  {
    digitalWrite(lamp3, LOW);
    lamp3_state = 0;
    Received = 0;
  }

  if (music_state == 0 && Received == '4')
  {
    digitalWrite(music, HIGH);
    music_state = 1;
    Received = 0;
  }
  if (music_state == 1 && Received == '4')
  {
    digitalWrite(music, LOW);
    music_state = 0;
    Received = 0;
  }

  if (pc_state == 0 && Received == '5')
  {
    digitalWrite(pc, HIGH);
    pc_state = 1;
    Received = 0;
  }
  if (pc_state == 1 && Received == '5')
  {
    digitalWrite(pc, LOW);
    pc_state = 0;
    Received = 0;
  }

  if (mobile_state == 0 && Received == '6')
  {
    digitalWrite(mobile, HIGH);
    mobile_state = 1;
    Received = 0;
  }
  if (mobile_state == 1 && Received == '6')
  {
    digitalWrite(mobile, LOW);
    mobile_state = 0;
    Received = 0;
  }

  if (fan_state == 0 && Received == '7')
  {
    digitalWrite(fan, HIGH);
    fan_state = 1;
    Received = 0;
  }
  if (fan_state == 1 && Received == '7')
  {
    digitalWrite(fan, LOW);
    fan_state = 0;
    Received = 0;
  }

  if (socket_state == 0 && Received == '8')
  {
    digitalWrite(socket, HIGH);
    socket_state = 1;
    Received = 0;
  }
  if (socket_state == 1 && Received == '8')
  {
    digitalWrite(socket, LOW);
    socket_state = 0;
    Received = 0;
  }

}

For the complete code for this program, download the following document.

Home_automation_code
.txt
TXT • 3KB

Note: Please change the file format to .ino(Arduino IDE) format before you upload the code to the Arduino board.



Working


All components connected
All components connected

Configuring the Arduino Bluetooth Controller App.

First, we have to switch on Bluetooth in our #smartphone and search for the HC-05 #Module, whose name will be displayed in terms of a few digits like an IP Address. (refer to video). Once the device is connected and paired to the phone, open the App downloaded, search for the devices using the search icon. Select the HC-05 module to configure buttons, dimmers, etc according to how we want to control the device.


In this project, we will configure a button to control the LED and another button to control the Mosquito Repellent #Machine. The button icon is selected after the HC-05 module is paired and connected. A name should be given to the buttons and particular numerical values need to be given to the input control and output control (Refer to video). Then using the button, we can control the LED and the Good Knight machine by turning it ON or OFF from our smartphone.




Let us analyze how the #relay works as a switch. Once the command from the smartphone is given through a button, the current is passed through the coil inside one channel of the relay, a magnetic field is generated and the contact of the switch is changed. The bulb is turned #ON and OFF in this manner.


The same happens when the command is given from the mobile phone for the next button, the second coil of the relay receives current, magnetic fields are generated and the contact of the switch is changed. With this the second component, the Mosquito repellent machine is switched ON and #OFF.


An 8-channel relay can be used for implementing the complete code given above. For just two components, a set of two "if" conditions can be used, and working for the same is given.


Watch the video given below for the practical demonstration of the Project.


Project video By - Farhan Ahamed

Building an Automation system is as easy as it seems and gets complex when taking it to the next level. It is not difficult if the design of the device is built systematically, specific buttons are used to control specific components and noted in real-time about which components are active, etc. So, let's get on with it and design our own mini Home Automation system now.


SEE ALSO: