Aarush Sood
Published © GPL3+

Control Led with Open-cv python(Hand gestures) and arduino

Here I'm demonstrating a simple led circuit being controlled by hand gestures using python-openCv and arduino

IntermediateFull instructions providedOver 1 day12,547

Things used in this project

Hardware components

Breadboard (generic)
Breadboard (generic)
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Green
5 mm LED: Green
×1
3 mm LED: Yellow
3 mm LED: Yellow
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 330 ohm
Resistor 330 ohm
×1
Arduino UNO
Arduino UNO
×1

Software apps and online services

Arduino IDE
Arduino IDE
Jupyter Notebook
Jupyter Notebook
OpenCV
OpenCV

Story

Read more

Schematics

Schematics-I

Schematics-II

Code

Arduino Code

Arduino
Here serialData is receiving an integer value from the python code. You can use any code for sending the values. An example of blinking Led is also given using python.
#include <cvzone.h>

SerialData serialData(2, 1); //(numOfValsRec,digitsPerValRec)
// no of values recieved : 1:(0-1);2(1-99)
//here we take 2 as we have range between 1-5
int valsRec[1]; // array of int with size numOfValsRec 

void setup() {
  serialData.begin();
  pinMode(3, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(13, OUTPUT);
  
  
}

void off(){
  if(valsRec[0]==5){
    digitalWrite(13, LOW);
    digitalWrite(7, LOW);
    digitalWrite(3, LOW);
  }
}

void loop(){
  int v;
   serilData.Get(valsRec);//get value from python code

  v=valsRec[0];//take the first element of array
  if(v==1){
    
    off(); 
    digitalWrite(3, HIGH);
  }
  else if(v==2){
    
    off(); 
    digitalWrite(7, HIGH);
  }
  else if(v==3){
    
    off(); 
    digitalWrite(13, HIGH);
  }
  else{
    off();
  }
}

Connect Python with Arduino

Python
Here we are using cv zone which you can download using pip install. This allows to connect the arduino with python. Here I'm using Jupyter notebook. This is the basic code for blinking an led with a delay of 3sec
from cvzone.SerialModule import SerialObject
from time import sleep

arduino = SerialObject()#port inside like "com6"
while True:
    arduino.sendData([1])#sending array/list
    sleep(3)#delay
    arduino.sendData([0])
    sleep(1)

Credits

Aarush Sood

Aarush Sood

5 projects • 9 followers
Technology enthusiast , ECE graduate from India. An electronics hobbyist who loves working with arduino's and esp's.

Comments