Arduino Bluetooth
Arduino Bluetooth
Arduino Bluetooth
Apparatus:
1. Arduino UNO & programming cable
2. HC-06 Bluetooth module
3. 4 x LEDs
4. 4 x 220 ohms resistors
5. Breadboard
6. Connecting wires
Pinouts:
2. Arduino UNO
Arduino Uno is a microcontroller board based on the ATmega328P. It
has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6
analog inputs, a 16 MHz ceramic resonator, a USB connection, a power jack,
an ICSP header and a reset button. It contains everything needed to support
the microcontroller; simply connect it to a computer with a USB cable or
power it with a AC-to-DC adapter or battery to get started.
Circuit:
Procedure:
a) Connect the circuit as shown.
We use the 5V power supply from Arduino
Connect the HC-06 to Arduino accordingly:
Connect the 1st LED anode to 220 ohm resistor and 220 ohm resistor to pin 4 of Arduino
UNO. LED cathode should be connected to ground.
Similarly, connect 2nd LED to pin 5 of Arduino UNO, 3rd LED to pin 6 and 4th LED to pin 7
of Arduino UNO.
void setup()
{
Serial.begin(9600); //Start communication with Bluetooth module
void loop()
{
//If the incoming data is available from Bluetooth,
//then store the data in the variable
if (Serial.available()>0)
{
cmdIn = Serial.read();
}
/*
*These if statements check that the received data is 'A', 'B', 'C' or 'D'.
*when any of the command is received, the respective if statement gets executed.
*Here, we toggle the respective LED. To do this, we first check if the LED
*is ON or OFF and perform the opposite action, and display the status of LED.
*Example: if LED is ON then turn the LED OFF.
*/
/*As you can see the code in below if statements is different than the above ones.
*Here, the same process is done as above, but in a different way
*digitalWrite(LED3, !digitalRead(LED3)) to understand this function, here is the format
*digitalWrite(_output_pin_, NOT of _output_pin_)
*The exclamation mark '!' refers to NOT function.
*Hence the function digitalWrite(LED3, !digitalRead(LED3)) means
*turn LED3 ON or OFF, based on the compliment of previous state of that LED3
*To display the message about status of LED, we join one string "LED3 is "
*with the string value of current state of LED3 i.e. (String(digitalRead(LED3))
*
*/
if (cmdIn == 'C')
{
digitalWrite(LED3, !digitalRead(LED3));
Serial.println("LED3 is " + (String(digitalRead(LED3))));
}
if (cmdIn == 'D')
{
digitalWrite(LED4, !digitalRead(LED4));
Serial.println("LED4 is " + (String(digitalRead(LED4))));
}
Mobile App:
Download and install the following mobile application from Google play store or scan the
QR code below
◆ When the application is connected to Bluetooth module, Then press the reset button
on the Arduino board. You will receive a ‘‘Hello’’ message displayed on the black
terminal.
◆ Then type the commands A, B, C or D in the textbox which says ‘Enter ASCII
Command’ and click on ‘Send ASCII’ button just besides the textbox.
◆ You will see the respective LED connected to Arduino turning ON and a text will be
displayed on the app.
◆ Now type and send the same command you sent earlier, now you will see the LED
turn OFF and a text displayed
◆ Remember, command ‘A’ controls LED1, ‘B’ controls LED2, ‘C’ controls LED3 & ‘D’
controls LED4.
◆ Try sending other commands, also try sending multiple commands like ABC, BD,
ABCD, AC, etc.