Arduino Bluetooth controlling – This is a very detailed tutorial about how to design and develop an android Application for controlling a 220v bulb using Arduino and Android application.
For the best understanding and easy follow-up, I am going to controlling a 12v relay from the android cell phone app. In this tutorial I will add one toggle button in the app, toggle button will be used to turn on and off the bulb with a single button. Later after understanding the basics then you can add multiple toggle buttons and you can simply copy and paste the code the only difference will be in the ID, which you will see in the programming.
In this tutorial I am going to use a lot of pictures and I will try to explain everything, so this Tutorial can be a bit longer, but trust me if you follow all the steps and read this article, at the end you will be able to make your own Android app to control anything you want. Enough with the introduction, Let’s continue without any further delay!!!
The software’s used for Arduino Bluetooth controlling system are:
About the Android Studio:
Android studio is one of the most commonly used software used for android app development which includes designing and programming. Before you follow this tutorial you should have some basic know how to use the Android studio. Otherwise, It can be really confusing for you. My recommendation is first you should follow some basic tutorials and then you can resume from here.
Before we start the Android Toggle Button app development, first of all, I would like to explain the circuit diagram and Arduino programming. Because we can only design a good android application if we know exactly for what purpose we are making this application.
Arduino Bluetooth controlling Circuit Diagram:
This is a very basic Bluetooth controlling Circuit diagram designed in CadeSoft Eagle 9.1.0 version.
A bulb is connected with Pin number 13 of the Arduino through 12v relay. This is a 220v bulb and that’s why I connected this with 12v relay. It really doesn’t matter if you use a 2.5v BULB or a 5v bulb or even if you add a relay to control a 220 Vac bulb, it will have no effect on the programming.
On the left side, you can see a Bluetooth module, you can use the HC-05 or HC-06 Bluetooth Module. If you are using Bluetooth for the first time then you can watch my getting started tutorial on the Bluetooth module available in the related projects section. The Bluetooth module Tx and Rx pins are connected with the Arduino’s pin number 2 and pin number 3. Pin number and pin number 3 will be used as the Serial Port, which will be defined in the programming. While the VCC and GND are connected with the Arduino’s 5v and GND.
Arduino Bluetooth controlling Programming:
// receiver code
SoftwareSerial Blue(2, 3);
long int data;
int relay1 = 13; // DEVICE 1
long int password1 = 92;// light on
long int password2 = 551;// to off
char state = 0;
data = Blue.parseInt();
if (data == password1)
Serial.println("device1 on ");
if( data == password2)
Arduino Bluetooth controlling Program Explanation:
As you know my friends in Arduino Uno we have only one Serial port which is available on pin number 0 and pin number 1. As I always say never use these pins with any Serial communication supported devices. The Arduino’s default Serial Port should only be used for debugging purposes. You can always define other Serial Ports using the SoftwareSerial library. So that’s the reason I added the SoftwareSerial.h header file.
I defined a Serial Port with the name Blue on pin number 2 and pin number 3 of the Arduino. The pin number is the Rx while Pin number 3 is the Tx.
On the third line, I defined a variable data of the type long int. This variable will be used to store the number which is sent from the Android cell phone.
the bulb is connected with pin number 13 of the Arduino.
Then I defined two variables password1 and password2 of the type long integer. The number 92 is used to turn on the bulb while 551is used to turn off the bulb. This bulb can be replaced with a relay, this way you will be able to control anything you want.
Then in the void setup function, I set the BULB as output and set it to off state using the digitalwrite function. low means off and High means ON.
To activate the serial communication I used the Serial.begin() function while 9600 is the baudrate and similarly for the Bluetooth module. then starts the void loop function.
this line means that if the Bluetooth module has not received any data from the android cell phone then simply wait here.
this condition means if the Bluetooth module has received data from the android cell phone then store the received number in variable data and then using the if conditions the number stored in variable data is compared with the password1 and password2. If the number is equal to password1 then the BULB is turned ON and if the number stored in data is equal to password2 then the bulb is turned OFF. So that’s all about Arduino’s programming.
The Android platform provides Bluetooth network stack support, which allows a phone to exchange data wirelessly with other Bluetooth devices. With Android Bluetooth APIs, the application framework provides access to the Bluetooth features. These APIs allow wireless connection of applications to other Bluetooth devices, allowing point-to-point and wireless multipoint features.
Using the Bluetooth APIs, the following can be achieved by an Android application:
- Check for other phones with Bluetooth
- For paired Bluetooth devices, ask for a local Bluetooth adapter
- Establish channels for RFCOMM
- Link to other phones by finding the service
- Data transfer from and to other phones
- Manage multiple links
This page is dedicated to Classic Bluetooth. Classic Bluetooth is the right choice for more battery-intensive operations, including streaming and Android phone connectivity. For low-powered Bluetooth devices, Android 4.3 (API level 18) introduces Bluetooth Low Energy API support. See Bluetooth Low Energy for more information. The document defines different models for Bluetooth, including the Health System Profile. It then describes how to use the Android Bluetooth APIs to accomplish the four main tasks required for Bluetooth communication: setting up Bluetooth, identifying devices that are either paired or accessible in the local area, linking devices, and transferring data between devices.
The basic of Bluetooth
To allow Bluetooth devices to transmit data between themselves, they must first use a pairing method to form a communication channel. For incoming connection requests, one device, a discoverable device, makes itself available. Another device uses a service discovery process to find the discoverable device.
The two devices complete a bonding process where they exchange security keys after the discoverable device acknowledge the pairing request. These keys are cached for later use by the devices. The two devices exchange information after the pairing and bonding processes are complete. The device that initiated the pairing request will release the stream that connected it to the discoverable device when the session is complete. Nevertheless, the two devices remain connected, so they can automatically reconnect during a future session so long as they are within each other’s reach and the bond has not been broken by either system.
for android app development visit my next article Arduino Bluetooth controlling application development using android studio.