Android Toggle Button for Automation using Arduino & Bluetooth– A few months back I uploaded an article on how to create your own android cell phone application to control an LED using Arduino, HC-05 Bluetooth module and an Android cell Phone. For the best understanding, I used only two buttons light ON and light OFF. The two buttons were used to turn ON and turn OFF the led. You can read this article by clicking on the link given below.
Using two buttons to control an LED or any other load AC or DC type is good for the beginners. But when it comes to the final app development where you need to control multiple loads, this seems very impractical to have two buttons for every load. If you want to control 8 electrical loads you will need 16 buttons. After covering the basics, I decided to make another tutorial focusing on the Android toggle button or switch.
Instead of using two buttons, the same thing can be done by using only one button which is called a toggle switch or a toggle button. Previously I used an LED, so that’s why this time I decided to use a 220Vac light bulb. Later you can increase the number of toggle buttons to control multiple loads. You can download some cool png images of the electrical appliances and use them as the buttons, which you will learn in a minute.
For the practical demonstration, circuit diagram explanation, and programming watch video given at the end of this article.
Without any further delay let’s get started!!!
The components and tools used in this project can be purchased from Amazon, the components Purchase links are given below:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Android Toggle Button Arduino Bluetooth Circuit Diagram:
The HC-05 or HC-06 Bluetooth Module Tx and Rx pins are connected with the Arduino’s pin number 2 and pin number 3. While the Vcc and GND pins of the Bluetooth module are connected with the Arduino’s 5 Volts and ground.
A 12 volts SPDT “Single Pole and Double Throw” type relay is used to control the AC load. This relay has a total of 5 pins, out of these 5 pins two are the coil pins, common, Normally Open, and normally closed. This relay is controlled by the driver circuit which consists of the Transistor and a 10k ohm resistor. This is a general-purpose NPN transistor. As you can see the emitter is connected with the Ground, the collector is connected with the relay coil pin while the other side of the relay coil is connected with the 12 volts. The base is connected with the Arduino’s pin number 13 through a 10k resistor. A diode is connected across the relay coil, which is used for the back EMF protection. You can use a readymade relay module or you can make the one by yourself.
About HC 05 Bluetooth Module:
The HC-05 Bluetooth Module used in this project is given below.
As you can see clearly in the images above, this Bluetooth module has a total of 6 male headers which are clearly labeled as STATE, RXD, TXD, GND, VCC, and EN. The EN pin has a push button that is used during the time when this module is needed to be entered into the AT commands Mode. I have a very detailed tutorial on how to use the AT commands to change the name of the Bluetooth module and the pairing code. You can watch the following video.
Relay Module Interfacing with Arduino
As you can see on the left side is the 3 channel relay module, but out of these three relays currently, I am using only one relay which is connected with the Arduino’s pin number 13 and make sure you connect the relay module ground ”blue wire” with the Arduino’s ground. The Bluetooth Module is connected with the Arduino as per the circuit diagram already explained.
Arduino Bluetooth Programming:
SoftwareSerial Blue(2, 3);
long int data;
int relay1 = 13; // DEVICE 1
long int password1 = 92;// light on
long int password2 = 551;// to off
data = Blue.parseInt();
if (data == password1)
Serial.println("device1 on ");
if( data == password2)
Arduino Bluetooth Program explanation:
I started off by including the Software Serial library. The SoftwareSerial library is used to create Serial Ports. You can define multiple Serial Ports. If you are using the Arduino Mega then you don’t need to use the SoftwareSerial library as you know in Arduino Mega we have multiple Serial ports. But when it comes to the Arduino we have only one Serial Port which is available on pin number 0 and pin number 1. As I always say never use the Arduino’s default Serial Port for communication with other devices. The Arduino’s default Serial Port should only be used for debugging purposes. So using the SoftwareSerial library I defined a serial port of the Bluetooth module on pin number 2 and pin number 3 of the Arduino.
The variable data is of the type long integer and is used to store the number sent from the Bluetooth application. This number can be 6 digits long to increase security.
Relay is connected with the Arduino’s pin number 13 which is already explained in the circuit diagram.
long int password1 = 92;
long int password2 = 551;
These are the two passwords that are used to turn ON and turn OFF the load. You can increase the security by using numbers consisting of 6 digits.
In every Arduino or Mega program, we have at least two functions which are the void setup and void loop functions. The void setup function executes only one time when the Arduino or Mega board is turned ON, while the void loop function executes forever until we turn OFF the Board. In the void setup function, the relay is set as the output using the pinMode() function. The relay is turned OFF by using digitalWrite function and finally, the Serial communication is activated.
In the void loop function,
If the Arduino has not received any data from the Bluetooth module then simply wait over here.
data = Blue.parseInt();
This condition means if the Arduino has received data from the Bluetooth then store the received number in data and compare this with the predefined values and then turn ON and turn OFF the relay accordingly. So that’s all about Arduino programming.
About the Android Toggle Button app development:
The Android cell phone application used in this project is designed in an android studio and is developed by the programming digest. The source code and apk file can be downloaded from the programming digest by clicking on the link below.
This project was mainly focused on the toggle button or toggle switch to control an electrical load using only one button. The application used in this project was successfully tested on different cell phones. You can watch a practical demonstration in the video given below. If you have any questions let me know in a comment.
Support my Website and YouTube channel by subscribing, liking and sharing. Don’t forget to click the bell icon on my YouTube channel, if you want to get notifications of my upcoming video tutorials.
Watch Video Tutorial: