Control LED with Mobile App Using Arduino and Bluetooth Module

Today we will learn a super practical and interesting little project – Control LED lights via a mobile app using Bluetooth module. This project will not only help you get familiar with the basic GPIO operations of Arduino but also let you understand the practical applications of Bluetooth communication. After completing the project, you can turn the LED light on and off with your phone, and even expand it to control more devices.

What is a Bluetooth Module?

A Bluetooth module is a type of wireless communication module that can achieve data transmission between devices through the Bluetooth protocol. In this project, we use the Bluetooth module as a bridge to enable communication between the phone and Arduino. Common Bluetooth modules are HC-05 or HC-06, which are inexpensive and easy to use.

  • HC-05: Supports master-slave mode and has more functions.
  • HC-06: Only supports slave mode, simpler.

Life Analogy: The Bluetooth module is like a “translator” that helps the phone (person) and Arduino (machine) understand each other’s “language”.

Hardware Preparation

  1. Arduino Development Board (such as Uno or Nano)
  2. Bluetooth Module (HC-05/HC-06)
  3. LED Light (1 piece)
  4. Resistor (220 ohms, used for current limiting)
  5. Breadboard and Jumper Wires

Hardware Connections

Below are the specific steps for hardware connections:

  1. Wiring the Bluetooth Module

  • VCC → Arduino’s 5V
  • GND → Arduino’s GND
  • TXD → Arduino’s RX (D0)
  • RXD → Arduino’s TX (D1) Note: When connecting RXD of HC-06 to Arduino, a resistor divider is needed to prevent damage.
  • Wiring the LED

    • Positive (long leg) → Arduino’s D13
    • Negative (short leg) → Resistor → Arduino’s GND

    Below is the connection diagram:

    1 Bluetooth Module       Arduino
    2 VCC ---------> 5V
    3 GND ---------> GND
    4 TXD ---------> RX (D0)
    5 RXD ---------> TX (D1)
    6
    7 LED Light
    8 Long leg ---------> D13
    9 Short leg ---------> Resistor ---------> GND
    

    Precautions:

    • The TX and RX pins of the Bluetooth module are dedicated to serial communication, please avoid using the Arduino’s serial debugging (Serial Monitor) while using Bluetooth, otherwise, conflicts may occur.
    • The RXD level of the Bluetooth module is usually 3.3V, while the Arduino’s TX is 5V, use a resistor divider to protect the module.

    Software Development

    1. Prepare the Mobile App

    Download an app that supports Bluetooth serial communication, such as “Bluetooth Serial Debug Assistant” (Android) or similar tools (iOS).

    • Open the app and connect to the Bluetooth module (the default pairing password is usually <span>1234</span> or <span>0000</span>).
    • Once the Bluetooth connection is successful, you can send data to Arduino.

    2. Arduino Code

    Below is the complete code:

     1 // Define LED pin
     2 const int ledPin = 13;
     3
     4 void setup() {
     5   // Initialize serial communication
     6   Serial.begin(9600);
     7   // Set LED pin as output mode
     8   pinMode(ledPin, OUTPUT);
     9 }
    10
    11 void loop() {
    12   // Check if there is data coming from the Bluetooth module
    13   if (Serial.available() > 0) {
    14     char command = Serial.read(); // Read the data sent by Bluetooth
    15
    16     // Control the LED based on the received command
    17     if (command == '1') {
    18       digitalWrite(ledPin, HIGH); // Turn on LED
    19       Serial.println("LED is ON");
    20     } else if (command == '0') {
    21       digitalWrite(ledPin, LOW); // Turn off LED
    22       Serial.println("LED is OFF");
    23     } else {
    24       Serial.println("Invalid command");
    25     }
    26   }
    27 }
    

    3. Code Explanation

    • Serial.begin(9600): Initializes serial communication with a baud rate set to 9600 (consistent with the default of the Bluetooth module).
    • Serial.available(): Checks if there is data incoming.
    • Serial.read(): Reads the command received from the Bluetooth module.
    • digitalWrite(): Controls the LED’s on/off state.

    Note: HC-06 defaults to receiving a single character, so use <span>'1'</span> and <span>'0'</span><code><span> to represent the on/off operations.</span>

    Practical Application Cases

    1. Test Bluetooth Communication Open the mobile app, connect to the Bluetooth module, and test sending <span>1</span> and <span>0</span>, observe whether the LED lights up or goes out. If there is no response, check the serial connection and baud rate settings.

    2. Expand Control of Multiple LEDs If you want to control multiple LEDs, you can add more pin definitions and commands. Send <span>A</span> to control LED1, and send <span>B</span> to control LED2.

    3. Application Scenarios

    • Smart Home: Control lights with your phone.
    • Remote-Controlled Devices: Such as the direction lights of a toy car.

    Common Issues and Solutions

    1. Bluetooth Cannot Pair

    • Check if the Bluetooth module’s indicator light is flashing. If not, the module may not be powered correctly.
    • Ensure the Bluetooth function on the phone is turned on, the default password for HC-05/HC-06 is <span>1234</span> or <span>0000</span>.
  • LED No Response

    • Check if the LED is connected incorrectly (long leg to positive, short leg to negative).
    • Check if the serial connections between Arduino and the Bluetooth module are correct.
  • Serial Conflict During Debugging

    • HC-05/HC-06 occupies the hardware serial port of Arduino, and the serial monitor cannot be used simultaneously. You can try using the SoftwareSerial library to extend the serial port.

    Practical Suggestions

    • Expand Functionality: Try using PWM output to control the brightness of the LED, or use a button app to send more commands.
    • Debugging Tools: Use a multimeter to test if the circuit connections are normal.
    • Safety Reminder: Bluetooth communication may be interfered with, and when it involves actual control, encryption or data verification should be added.

    After completing this project, you will not only master the basic use of the Bluetooth module but also apply wireless control to more scenarios, adding infinite possibilities to your DIY projects!

    Leave a Comment