#4 Arduino Mega: Bluetooth Communication to Toggle the LED (using HC-05)

IoT Tutorial #4 [ Arduino Mega Tutorials #4 ]
Here I will demonstrate the mini project, Turning ON / OFF the LED using wireless Serial Communication using Bluetooth Module HC-05 using Arduino Mega (ATMega 2560).

Problem Statement:
- To Turn ON / OFF the LED using wireless Serial Communication (Tx1 and Rx1).
- Serial communication will be done by USB cable (A to B) for cross-checking the output. (through Tx0 and Rx0)

Requirements:
- Arduino Mega (ATMega 2560)
- USB Cable (A to B)
- Android Phone with "Bluetooth Terminal" App installed in it.
- 1 LED
- 1k Resistor
- Bread Board
- Male to Male wires (minimum 6)



Program Outcome:
Output is shown using LED
If Mobile keypad key "a" / "A" is pressed  -> LED ON
If Mobile keypad key "b" / "B" is pressed  -> LED OFF

> Output message displayed on Arduino Terminal on Desktop 
  using serial communication (Tx0 and Rx0).
NOTE:
Baud Rate is set to 9600
Steps:
1. Install "Bluetooth Terminal" app (By Juan Sebastian Ochoa Zambrano) 
   on Android mobile from Play Store.
2. Make Physical Connections of the BLuetooth Module (HC-05) as given below.
3. On Android mobile, Search for Bluetooth and Connect to HC-05 
   and pair it using password "0000" or "1234".
4. Open "Bluetooth Terminal" App on the Mobile and 
   connect to paired HC-05 module
Physical Connection:
Arduino Mega -> BluetoothModule
Pin18(Tx1)   -> Rx
Pin19(Rx1)   -> Tx
Gnd          -> GND
Vcc          -> VCC(+5V)

Pin13        -> LED+
Gnd          -> LED-

Serial Communication through USB cable(Tx0 and Rx0) to
Arduino Terminal(Desktop)is used just to cross verify.
Downloads:
Download link is given in the Description of the YouTube video shown below.

Demonstration:




Program:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //Tx0 and Rx0  //Set Baud Rate to 9600 for Serial Communication Tx0 and Rx0
  Serial1.begin(9600);  //Tx1 and Rx1  //Connected to Bluetooth Module HC-05 (Bluetooth 2.0)
  //Serial2;  //Tx2 and Rx2
  //Serial3;  //Tx3 and Rx3

  pinMode(13, OUTPUT);  //Set Pin 13 as Output (Connected to LED)

  Serial.println("press \"a\" or \"A\" to switch ON the LED");  //To Desktop
  Serial.println("press \"b\" or \"B\" to switch OFF the LED");
  Serial1.println("press \"a\" or \"A\" to switch ON the LED"); //To mobile
  Serial1.println("press \"b\" or \"B\" to switch OFF the LED");
}

void loop() {
  // put your main code here, to run repeatedly:
  
  if(Serial1.available()>0)
  {
    int buffer_value = Serial1.read();
    if(buffer_value == 'a' || buffer_value == 'A')
    {
      digitalWrite(13, HIGH);    //Turn ON LED
      Serial.println("LED ON");  //Arduino Terminal of Desktop 
      Serial1.println("LED ON"); //Bluetooth Terminal on Mobile
    }
    else if(buffer_value == 'b' || buffer_value == 'B')
    {
      digitalWrite(13, LOW);      //Turn OFF LED
      Serial.println("LED OFF");  //Arduino Terminal on Desktop
      Serial1.println("LED OFF"); //Bluetooth Terminal on Mobile 
    }
  }
}

Click here to see more codes for Arduino Mega (ATMega 2560) and similar Family.

Feel free to ask doubts in the comment section. I will try my best to solve it.
If you find this helpful by any mean like, comment and share the post.
This is the simplest way to encourage me to keep doing such work.

Thanks and Regards,
-Akshay P. Daga
إرسال تعليق (0)
أحدث أقدم