Arduino based LED using Bluetooth & Mobile
Arduino based LED using Bluetooth & Mobile
AIM: To study Arduino Uno based LED Switching using Bluetooth & Mobile.
OBJECTIVES:
1. To understanding the basics of Arduino Uno and its Pin configuration.
2. To write an Arduino Uno program for LED switching using Bluetooth & Mobile.
LED interfacing with Arduino Uno using mobile app Bluetooth terminal HC-05
1
Procedure for Arduino Uno Programming: Arduino Uno IDE
a. Open new sketch, Write a program to control the LEDs, and Buzzer via Bluetooth
through a phone app and save it as “bluetooth.py” filename.
b. Select your board type and port.
c. You'll need to select the entry in the Tools > Board menu that corresponds to your
Arduino board. Select the serial device of the board from the Tools > Serial Port
menu. This is likely to be COM3 or higher (COM1 and COM2 are usually
reserved for hardware serial ports). To find out, you can disconnect your board
and re-open the menu; the entry that disappears should be the Arduino board.
Reconnect the board and select that serial port.
d. To Upload the program:
Now, simply click the "Upload" button in the environment. Wait a few seconds -
you should see the RX and TX led’s on the board flashing. If the upload is
successful, the message "Done uploading." will appear in the status bar.
Program1:
// Code to control LED’s and Buzzer via Bluetooth through the phone app.
#define GREEN 5
#define RED 6
#define BUZ 7
/*declaring that there is a variable called switch state, which will hold a character value. This
is due to programming of the app, which will send a text value to arduino. If we use 'int'
instead of 'char' the code will not work properly.*/
/*To start serial communication at a rate of 9600 bits per second. This is the default rate
anyways.*/
void setup()
{
Serial.begin(9600);
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(BUZ, OUTPUT); //Declaring that the LED is an output.
}
void loop()
{ .
if (Serial.available() > 0)
{
delay(50);
//code to be executed only when Serial.available()>0
2
/*Serial.available>0 is to check if there is any reading from the HC-05 Bluetooth module.*/
switchstate = Serial.read();
while(Serial.available() > 0)
{
Serial.read();
}
/*The character we had declared earlier is now being assigned a value- the value of
whatever Serial.read() is.*/
digitalWrite(GREEN, LOW);
digitalWrite(RED, LOW);
digitalWrite(BUZ, LOW);
if (switchstate == 'g')
{
digitalWrite(GREEN, HIGH);
}
else if (switchstate == 'r')
{
digitalWrite(RED, HIGH);
}
else if (switchstate == 'b')
{
digitalWrite(BUZ, HIGH);
delay(1000);
digitalWrite(BUZ, LOW);
}
}
}