Simple RGB LED Control with Arduino Using LM358 and Voice-Controlled C# Application

 


This project demonstrates how to control a 2-pin RGB LED connected to an Arduino using an LM358 microcontroller for pulse-width modulation (PWM) output. The LED's color can be controlled by voice commands, processed by a C# Windows application. The application translates the voice commands into text-based commands and sends them via serial communication to the Arduino.


How It Works

1. Hardware Setup:

  • The RGB LED is connected to the Arduino through an LM358 operational amplifier to enhance the PWM output.
  • The LM358 helps to provide stable control of the RGB LED colors.
  • The circuit diagram (attached above) showcases how components like capacitors and resistors are used to stabilize the circuit.

2. Software Components:

  • Arduino Sketch: The Arduino reads serial commands sent by the C# application and adjusts the PWM signals to control the RGB LED's colors.
  • C# Windows Application: This app features voice recognition functionality. It listens to the user's voice, converts it into text commands (e.g., "Red," "Green," "Blue"), and sends these commands to the Arduino over a serial connection.
3. Voice Recognition:
  • The voice recognition engine translates user commands into text.
  • Commands are mapped to RGB color values and sent as serial data to the Arduino.

Circuit Diagram Explanation

The image shows:

  • Arduino UNO: Supplies control signals for the RGB LED via the LM358.
  • LM358 Operational Amplifier: Amplifies the PWM signals from the Arduino to drive the RGB LED. Ensures smooth transitions between colors.
  • 100µF Capacitor: Stabilizes voltage fluctuations.
  • Resistor: Limits the current to the RGB LED to prevent damage.

Code Explanation

Arduino Code

int PWM_PIN = 3;    // The output that drives the LED color (PWM)

void setup() {
  pinMode(PWM_PIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    String command = Serial.readStringUntil('\n'); // Read command
    setColor(command);
  }
}

void setColor(String color) {
  if (color == "Red") {
    analogWrite(PWM_PIN, 102);
  } else if (color == "Green") {
    analogWrite(PWM_PIN, 204);
  } else if (color == "Blue") {
    analogWrite(PWM_PIN, 255);
  } else if (color == "Off") {
    analogWrite(PWM_PIN, 0);
  }
}

C# Code for Voice Recognition

using System;
using System.IO.Ports;
using System.Speech.Recognition;

class Program
{
    static SerialPort serialPort;

    static void Main(string[] args)
    {
        serialPort = new SerialPort("COM3", 9600); // Adjust COM port as needed
        serialPort.Open();

        SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine();
        recognizer.SetInputToDefaultAudioDevice();
        
        Choices colors = new Choices(new string[] { "Red", "Green", "Blue", "Off" });
        Grammar grammar = new Grammar(new GrammarBuilder(colors));
        recognizer.LoadGrammar(grammar);

        recognizer.SpeechRecognized += Recognizer_SpeechRecognized;
        recognizer.RecognizeAsync(RecognizeMode.Multiple);

        Console.WriteLine("Speak a color (Red, Green, Blue, Off)...");
        Console.ReadLine();

        serialPort.Close();
    }

    private static void Recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        string color = e.Result.Text;
        Console.WriteLine("Command: " + color);
        serialPort.WriteLine(color);
    }
}

Features

  • Interactive Voice Control: Users can control the LED color hands-free by issuing voice commands.
  • Serial Communication: Reliable communication between the C# application and Arduino via a USB connection.
  • Modular Circuit Design: The LM358 allows efficient and stable control of LED brightness and color blending.

Applications

  • Home Automation: Enhance smart lighting systems with voice control.
  • Educational Projects: A beginner-friendly project to understand serial communication, PWM, and voice recognition.
  • Prototyping: A foundation for more complex IoT projects.

This project combines software and hardware to demonstrate the possibilities of voice-controlled RGB LED systems, providing an excellent foundation for more advanced applications.
Previous Post Next Post

نموذج الاتصال