AI x IoT Project 2 : Voice-Controlled LED with ESP32 and Python

Have you ever dreamed of controlling gadgets using just your voice—like Iron Man? In this tutorial, we’ll show you how to control an LED using voice commands via your computer's microphone and an ESP32 board. It's a fun and beginner-friendly IoT project combining Python, Speech Recognition, and serial communication with an ESP32 microcontroller.

What You’ll Need

Hardware:

  • ESP32 Development Board
  • LED (optional – onboard LED also works)
  • USB Cable (to connect ESP32)

Software:

  • Python 3 installed on your PC
  • speech_recognition library
  • Arduino IDE (for uploading code to ESP32)
  • pyserial library (for serial communication)

Project Overview:

  • The user gives voice commands like “turn on the light” or “turn off the light.”
  • Python captures the voice input using a microphone, processes it using Google’s Speech Recognition API, and sends appropriate commands via serial communication to the ESP32.
  • The ESP32 receives these commands and turns the LED ON or OFF accordingly.

Step 1: Circuit Connection

You can use the onboard LED (usually on pin 2) or connect an external LED.

For external LED:

  • Long leg (anode) → GPIO 2
  • Short leg (cathode) → GND

Step 2: ESP32 Code (Arduino)

Open the Arduino IDE and upload the following code to your ESP32:

---------------------------------------------------------------------------------------

// ESP32 code to receive commands from laptop via Serial
#define LED_PIN 2

void setup() {
  // Initialize serial communication
  Serial.begin(115200);

  // Configure LED pin
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);

  Serial.println("ESP32 ready to receive commands");
}

void loop() {
  // Check if data is available to read
  if (Serial.available() > 0) {
    // Read the incoming command
    String command = Serial.readStringUntil('\n');
    command.trim();  // Remove any whitespace

    // Process the command
    if (command == "ON") {
      digitalWrite(LED_PIN, HIGH);
      Serial.println("LED turned ON");
    } 
    else if (command == "OFF") {
      digitalWrite(LED_PIN, LOW);
      Serial.println("LED turned OFF");
    }
  }

  // Small delay for stability
  delay(10);
}

--------------------------------------------------------------------------------------

Step 3: Python Voice Command Script

Install the necessary Python libraries:

'pip install speechrecognition pyserial pyaudio'

Note: On some systems, pyaudio may need extra setup. Use pip install pipwin && pipwin install pyaudio if you face issues on Windows.

Now create a Python file voice_led_control.py and paste this:

-----------------------------------------------------------------------------------

import speech_recognition as sr
import serial
import time

# Configure serial port - change COM3 to your ESP32's port
# On Windows it's typically COMx
# On Mac/Linux it's typically /dev/ttyUSB0 or /dev/ttyACM0
ser = serial.Serial('/dev/cu.usbserial-0001', 115200, timeout=1)
time.sleep(2)  # Allow time for connection to establish

def recognize_speech():
    # Initialize recognizer
    r = sr.Recognizer()

    with sr.Microphone() as source:
        print("Listening for voice commands...")
        # Adjust for ambient noise
        r.adjust_for_ambient_noise(source)
        # Listen for audio
        audio = r.listen(source)

    try:
        # Recognize speech using Google Speech Recognition
        text = r.recognize_google(audio).lower()
        print(f"You said: {text}")
        return text
    except sr.UnknownValueError:
        print("Could not understand audio")
        return ""
    except sr.RequestError as e:
        print(f"Error with speech recognition service; {e}")
        return ""

def main():
    print("Voice-controlled LED system started")
    print("Say 'turn on the light' or 'turn off the light'")

    while True:
        text = recognize_speech()

        if "turn on" in text and ("light" in text or "led" in text):
            print("Sending command: TURN ON")
            ser.write(b'ON\n')

        elif "turn off" in text and ("light" in text or "led" in text):
            print("Sending command: TURN OFF")
            ser.write(b'OFF\n')

        # Optional: quit command
        elif "exit" in text or "quit" in text:
            print("Exiting program...")
            break

        time.sleep(0.5)

    ser.close()

if __name__ == "__main__":
    main()

-------------------------------------------------------------------------------------

✅ Make sure to update the serial.Serial(...) port name to match your ESP32’s serial port (e.g., COM3 on Windows, /dev/ttyUSB0 or /dev/cu.usbserial-0001 on macOS).

Step 4: Test It Out!

1. Run the Python script

'python voice_led_control.py'

2. Speak commands like:

  • “Turn on the light”
  • “Turn off the light”
  • “Quit” to exit

Watch the LED respond to your voice!

How It Works

  • Python uses your microphone to capture audio.
  • It converts speech to text using Google's Speech Recognition API.
  • Based on the recognized command, it sends a message over serial to the ESP32.
  • The ESP32 receives the message and toggles the LED accordingly.

What’s Next?

Here are some cool ideas to take it further:

  • Control multiple devices (e.g., fans, bulbs, motors)
  • Use a GUI or mobile app to send voice commands
  • Add Wi-Fi or Bluetooth functionality for wireless control

Conclusion

This project is a great starting point for exploring voice-controlled IoT. You’ve combined speech recognition with embedded systems to build something truly magical. 💡

If you liked this project, follow along for more cool tech tutorials!

 

Back to blog

Leave a comment