Control an LED Using Voice Command, BlueBot App and HC-05

Control an LED Using Voice Command: Ever wondered how to control an LED with voice commands or text? In this blog, I’ll show you how to make it happen using an Arduino, the HC-05 Bluetooth module, and the BluBot app. With this project, you can send simple commands like “on,” “off,” or “blink” to control the LED remotely.

Control an LED Using Voice Command, BluBot App and HC-05

Let’s dive in step-by-step, and I’ll explain the code so even beginners can understand!

What You’ll Need

  1. Arduino Uno (or any compatible board)
  2. HC-05 Bluetooth Module
  3. LED
  4. Resistor (220 ohms) – to prevent your LED from burning out
  5. Connecting Wires
  6. BluBot App – For sending commands via Bluetooth

Circuit Connections

  1. HC-05 Bluetooth Module
    • VCC → 5V
    • GND → GND
    • TX → Arduino Pin 2 (RX)
    • RX → Arduino Pin 3 (TX)
  2. LED
    • Positive leg (longer leg) → Arduino Pin 13 (via a 220-ohm resistor)
    • Negative leg → GND

Arduino Code

Here’s the full Arduino code to control the LED using the BluBot app:

#include <SoftwareSerial.h>

SoftwareSerial BTSerial(2, 3);  // RX, TX pins for the Bluetooth module (HC-05)
int ledPin = 13;  // Pin for the built-in LED on Arduino

void setup() {
  // Start serial communication with Bluetooth and monitor
  BTSerial.begin(9600);  // HC-05 communication at 9600 baud rate
  Serial.begin(9600);    // Serial monitor at 9600 baud rate

  // Set LED pin as output
  pinMode(ledPin, OUTPUT);

  Serial.println("Ready to receive commands.");
}

void loop() {
  if (BTSerial.available()) {
    // Read the incoming Bluetooth data
    String command = BTSerial.readStringUntil('\n'); // Read until newline character
    command.trim(); // Remove leading/trailing spaces
    Serial.println("Command received: " + command); // Display received command

    // Check for command and execute appropriate action
    if (command.indexOf("on") != -1) {
      digitalWrite(ledPin, HIGH);  // Turn the LED on
      Serial.println("LED is ON");
    } 
    else if (command.indexOf("off") != -1) {
      digitalWrite(ledPin, LOW);  // Turn the LED off
      Serial.println("LED is OFF");
    }
    else if (command.indexOf("blink") != -1) {
      for (int i = 0; i < 5; i++) {
        digitalWrite(ledPin, HIGH);  // Turn LED on
        delay(500);                   // Wait for 500ms
        digitalWrite(ledPin, LOW);   // Turn LED off
        delay(500);                   // Wait for 500ms
      }
      Serial.println("LED blinked 5 times");
    } 
    else {
      Serial.println("Unknown command");
    }
  }
}

How the Code Works: Line-by-Line Explanation

1. Setting Up Bluetooth Communication

SoftwareSerial BTSerial(2, 3);  // RX, TX pins for the HC-05
int ledPin = 13; // Pin for the LED
  • BTSerial is used to communicate with the HC-05 module.
  • We assign Pin 2 for receiving data and Pin 3 for transmitting data.
  • The LED is connected to Pin 13.

2. Initialization in setup()

BTSerial.begin(9600);  // Start HC-05 communication
Serial.begin(9600); // Start Serial Monitor communication
pinMode(ledPin, OUTPUT);
  • Both Bluetooth and Serial Monitor communication are set to 9600 baud rate.
  • We set the LED pin as an output to control its state.

3. Reading Bluetooth Commands in loop()

if (BTSerial.available()) {
String command = BTSerial.readStringUntil('\n'); // Read the command
command.trim(); // Remove unwanted spaces
Serial.println("Command received: " + command);
}
  • The program constantly checks if any data is available from the Bluetooth module.
  • The readStringUntil('\n') function reads a command until the newline character is detected.
  • trim() ensures no leading or trailing spaces interfere with the command.

4. Turning the LED On

eif (command.indexOf("on") != -1) {
digitalWrite(ledPin, HIGH); // Turn LED on
Serial.println("LED is ON");
}
  • indexOf("on") != -1 checks if the word “on” exists in the command.
  • If true, the LED is turned on.

5. Turning the LED Off

else if (command.indexOf("off") != -1) {
digitalWrite(ledPin, LOW); // Turn LED off
Serial.println("LED is OFF");
}
  • Similarly, this checks for the word “off” in the command and turns the LED off.

6. Making the LED Blink

else if (command.indexOf("blink") != -1) {
for (int i = 0; i < 5; i++) {
digitalWrite(ledPin, HIGH); // Turn LED on
delay(500); // Wait for 500ms
digitalWrite(ledPin, LOW); // Turn LED off
delay(500); // Wait for 500ms
}
Serial.println("LED blinked 5 times");
}
  • When the command contains “blink”, the LED turns on and off 5 times.
  • A for loop repeats this process, with a delay of 500 milliseconds between each state.

How Does indexOf("blink") != -1 Work?

The indexOf function helps us check if a specific word (like “on,” “off,” or “blink”) is present in the received command.

Here’s how it works:

  1. indexOf("blink")
    • If the word “blink” is present in the command, it returns the position of the word (e.g., 0 if “blink” is the first word).
    • If “blink” is not found, it returns -1.
  2. != -1
    • The condition indexOf("blink") != -1 means:
      • True → The word “blink” exists in the command → Execute the blinking logic.
      • False → The word “blink” does not exist in the command → Skip this part of the code.

7. Handling Unknown Commands

else {
Serial.println("Unknown command");
}
  • If no matching keyword is found, the program displays “Unknown command” in the Serial Monitor.

Testing the Setup

  1. Power on your Arduino and HC-05.
  2. Pair your phone with the HC-05 (default PIN: 1234).
  3. Open the BluBot app and connect to the HC-05.
  4. Send these commands:
    • "on" – The LED will turn on.
    • "off" – The LED will turn off.
    • "blink" – The LED will blink 5 times.

Conclusion

Control an LED Using Voice Command: You’ve just created a Bluetooth-enabled LED controller! By sending commands like “on,” “off,” or “blink,” you can remotely control the LED via the BluBot app. This project is an exciting way to learn about Arduino, Bluetooth communication, and handling commands in code.

Ready to take it further? Try adding more commands to control additional LEDs or even motors!