In this example we will take command from the user through Bluetooth app and control our appliance here led which is connected on arduino pin number. Arduino pin number is set as output so we control arduino pins according to the user input. Let’s have a look at appliance control using Bluetooth and Arduino
CIRCUIT DIAGRAM
S.N. |
Arduino |
HC-05 |
1. |
+ 5V |
+ 5V |
2. |
GND |
GND |
3. |
Rx |
Tx |
4. |
Tx |
Rx |
S.N. |
Arduino |
LED |
1. |
4 |
Anode |
2. |
GND |
Cathode |
APPLIANCE CONTROL USING BLUETOOTH ARDUINO CODE
Here we are using LED as our appliance
int led = 4; void setup() { // To start UART (serial communication) bus Serial.begin(9600); // Transmitting a string from MCU to HC05 Serial.println("Connection established successfully:"); // Initialize the LED pin as an output pinMode(led, OUTPUT); } void loop() { // To check if data is available on Serial port if (Serial.available() > 0) { // To read a data from HC05 via serial communication protocol char data = Serial.read() ; // If data is capital case 'A' then turn On led and print a message "led is on" if (data == 'A') { digitalWrite (led, HIGH); Serial.println("led is on"); } // If data is small case 'a' then turn Off led and print a message "led is off" else if (data == 'a') { digitalWrite (led, LOW); Serial.println("led is off"); } else { Serial.println("Wrong Input"); } } }
APPLIANCE CONTROL USING BLUETOOTH ARDUINO CODE EXPLANATION
1. Let’s take a quick look at the code to see how it works. Declared a global variable to assign a output port pin number 4 among digital pins of MCU (ATMEGA328 P-PU).
int led = 4;
2. In this structure namely void setup(), serial communication is initialized by setting a baud rate of 9600 bits per second using begin() built-in function.
After that, Serial.println() built-in function is used to print user defined messages (“Connection established successfully: “) is displayed on your phone’s application after a successful connection is made. At the end in this structure pinMode () built-in function is included to initialize the digital pin as an OUTPUT to which 5V based peripheral will be interfaced.
void setup() { // To start UART (serial communication) bus Serial.begin(9600); // Transmitting a string from MCU to HC05 Serial.println("Connection established successfully:"); // Initialize the LED pin as an output pinMode(led, OUTPUT); }
3. In void loop structure read() built-in function is used to receive data from the user, which is stored in the UART register and fetched by Arduino, bit by bit from the UART register.
Received data is stored in the user defined variable “data” of char data type.
In this structure a conditional statement is used. If received data is matched with ASCII code of A which is enclosed in single quotation then the output will become HIGH and provide 5 volts and 20 mA current and display message “device is on” on the mobile phone. If the condition is not satisfied it will check else if condition, if satisfied then set output pin LOW and display a message “device is off” on the mobile phone app and if the input is neither ‘A’ nor ‘a’ it will display the message “Wrong Input“, if and only if HC05 is successfully paired and connected with user phone.
void loop() { // To check if data is available on Serial port if (Serial.available() > 0) { // To read a data from HC05 via serial communication protocol char data = Serial.read() ; // If data is capital case 'A' then turn On led and print a message "led is on" if (data == 'A') { digitalWrite (led, HIGH); Serial.println("led is on"); } // If data is small case 'a' then turn Off led and print a message "led is off" else if (data == 'a') { digitalWrite (led, LOW); Serial.println("led is off"); } else { Serial.println("Wrong Input"); } } }
// with the help of this program you can design advanced projects to solve many problems. You can implement home automation, for that 5 volts operated relay is connected at the output pin.
OUTPUT
When a user sends capital case “A” from the App it will turn ON the LED and give a message “led is on” and when user send small case “a” it will turn OFF the led and give message “led is off”.
READ NEXT
TEMPERATURE MONITORING SYSTEM USING BLUETOOTH ARDUINO