Skip to Content

Integrating Python with Arduino: Real-Time Emotion Recognition and LED Control System

17 November 2024 by
Integrating Python with Arduino: Real-Time Emotion Recognition and LED Control System
Purple Tecnologies

Introduction

Emotion recognition is a fascinating and useful application of machine learning, particularly when combined with real-time systems like Arduino. In this blog, we'll explore how to integrate Python with Arduino to create a system that detects emotions (such as happy, sad, angry, and neutral) and displays the corresponding emotion on LEDs connected to an Arduino board. This is a fun and practical project that demonstrates how to combine machine learning with embedded systems for real-world applications.

What You'll Need

Before we get started, here's a list of the hardware and software tools you'll need:

Hardware:

  1. Arduino Board (Arduino Uno, Nano, etc.)
  2. 4 LEDs (one for each emotion)
  3. Resistors (220Ω) for each LED
  4. Jumper wires and breadboard

Software:

  1. Python (for emotion detection)
  2. Arduino IDE (for programming the Arduino)
  3. Serial communication between Python and Arduino

Step 2: Writing the Arduino Code

Now, let's write the Arduino code to control the LEDs. The Arduino will listen for serial data from the Python script (which will send emotion data) and turn on the corresponding LED based on the emotion.


const int ledHappy = 3;

const int ledSad = 5;

const int ledAngry = 6;

const int ledNeutral = 9;

void setup() {

  pinMode(ledHappy, OUTPUT);

  pinMode(ledSad, OUTPUT);

  pinMode(ledAngry, OUTPUT);

  pinMode(ledNeutral, OUTPUT);

  Serial.begin(9600); // Initialize serial communication

}

void loop() {

  if (Serial.available() > 0) {

    char emotionChar = Serial.read(); // Get emotion character from the computer

    // Turn off all LEDs

    digitalWrite(ledHappy, LOW);

    digitalWrite(ledSad, LOW);

    digitalWrite(ledAngry, LOW);

    digitalWrite(ledNeutral, LOW);

    // Activate the corresponding LED

    if (emotionChar == 'H') digitalWrite(ledHappy, HIGH);     // Happy

    else if (emotionChar == 'S') digitalWrite(ledSad, HIGH);  // Sad

    else if (emotionChar == 'A') digitalWrite(ledAngry, HIGH); // Angry

    else if (emotionChar == 'N') digitalWrite(ledNeutral, HIGH); // Neutral

  }

}


This code listens for characters sent via the serial port. When it receives a character corresponding to an emotion (e.g., 'H' for Happy), it turns on the corresponding LED and turns off the others.



Connecting Python with Arduino

To make this work, you’ll need to ensure that your Python script is communicating with the Arduino via the correct serial port. You can find the correct port by checking your Arduino IDE or the device manager.

Make sure that the baud rate in the Python script (9600) matches the baud rate in your Arduino code.

Once connected, running the Python script will send the emotion data to the Arduino, and the appropriate LED will light up.

Testing and Troubleshooting

When you run both the Python script and the Arduino program, you should see the corresponding LED light up for each emotion. The LEDs will turn on for 2 seconds (as defined by the time.sleep(2) in the Python script) before moving to the next emotion.

If you don’t see the LEDs lighting up, check the following:

  1. Ensure the Arduino is properly connected to your computer.
  2. Verify that the correct COM port is selected in the Python script.
  3. Make sure you’ve uploaded the Arduino code correctly.
  4. Check the serial monitor for any error messages or connection issues.


"To purchase, contact us today!

📞 Call/WhatsApp: +91 9346934699 / 961743699

📧 Email: info@diykit.in

Our team is ready to assist with any questions. Get your DIY home automation kit and start building your smart home today!"