Skip to main content

Control an LED with Just Your Hand


 A Beginner-Friendly STEAM Project Using Raspberry Pi, Computer Vision & Python





Imagine This for a Moment…

You walk into a room. Instead of searching for a switch, you simply raise your finger — and the light turns on.
Lower your finger — the light turns off.

No buttons.
No physical contact.
Just vision, code, and a bit of electronics.

This is not science fiction. This is exactly what your students will build in this project.

Welcome to a hands-on STEAM activity where computer vision meets electronics, designed especially for beginners who are learning Raspberry Pi, Python, and real-world problem solving.


In real life, gesture control is already everywhere:

  • Automatic doors

  • Touch-free switches

  • Smart TVs

  • Motion-controlled games

  • Assistive technology for people with limited mobility

This project introduces you to how those systems actually work, not through theory slides — but by building one themselves.


By the end of this activity, students will:

  • Use a USB camera to detect a human hand

  • Track fingers using computer vision

  • Write Python code to interpret gestures

  • Control a real LED using hand movements


What This Project Does

You point a camera at your hand.

  • Raise your index finger → the LED turns ON

  • Lower your finger → the LED turns OFF

That’s it.

No buttons.
No touch sensors.
Just vision + code + a bit of GPIO.

Under the hood, the Pi is:

  • Looking at the camera feed

  • Detecting your hand

  • Tracking finger positions

  • Translating that into a physical action

It’s the same idea behind gesture-controlled TVs and smart devices — just stripped down to something you can build in an afternoon.


Why an LED? 

Yes, this controls an LED.

But the LED is just a placeholder.

Once this works, you can swap it out for:

  • A relay (to control real lights)

  • A fan

  • A motor

  • A buzzer

  • Anything you’d normally control with a GPIO pin

The hard part isn’t the output.
It’s teaching the computer to understand your hand.


What You’ll Need

Nothing exotic here:

  • Raspberry Pi 5 (headless is fine)

  • A laptop (I’m using Windows, but Mac/Linux works)

  • USB webcam (Logitech BRIO 100 works reliably)

  • 1 LED

  • 1 × 330Ω resistor

  • Breadboard

  • Jumper wires


Wiring the LED

https://cdn.shopify.com/s/files/1/0176/3274/files/raspberry_pi_led_breadboard_circuit.jpg?v=1683712372

This is the simplest part, but don’t rush it.

  • GPIO17 (Pin 11) → 330Ω resistorlong leg of LED

  • Short leg of LED → GND

The resistor matters.
Skipping it is a good way to shorten the life of both the LED and your Pi.

Once wired, leave it alone for now — we’ll test it later.

https://projects-static.raspberrypi.org/projects/leds-buzzers-scratch-games/77c618ed9d4a6ed14ba59ec76dfb3cc979c5ff62/en/images/lightLED_ledResistorAndWires.png


A Quick Word About Tools 

You’ll use a few different things during this build:

This isn’t overkill — it’s just how headless Pi setups work.


Connecting to the Raspberry Pi

From your laptop, open a terminal or Command Prompt and connect:


ssh pi@<your_pi_ip_address>

Once you’re in, everything you type runs directly on the Raspberry Pi.


First Sanity Check: Is the Camera Visible?

Before installing anything fancy, make sure the Pi can see the camera:


ls /dev/video*

You should see something like:


/dev/video0


If you don’t:

  • Unplug and reconnect the camera

  • Try a reboot

No point going further until this works.


The Python Version Problem (This Is Where Most Tutorials Break)

Here’s the awkward reality:

  • Raspberry Pi OS uses Python 3.11+

  • MediaPipe (the hand-tracking library) still requires Python 3.10

  • Installing Python 3.10 with apt no longer works

  • Downgrading system Python is a bad idea

This is why so many people get stuck halfway through gesture projects.

The solution isn’t a hack — it’s standard practice.


Installing Python 3.10 Safely Using pyenv

Instead of touching system Python, we install another Python version just for this project.

This keeps your Pi stable and your project clean.


Install the required build tools

sudo apt update sudo apt install -y \ build-essential libssl-dev zlib1g-dev libbz2-dev \ libreadline-dev libsqlite3-dev libncursesw5-dev \ xz-utils tk-dev libxml2-dev libxmlsec1-dev \ libffi-dev liblzma-dev git curl


Install pyenv

curl https://pyenv.run | bash

Tell your shell about it:


echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo 'eval "$(pyenv init --path)"' >> ~/.bashrc echo 'eval "$(pyenv init -)"' >> ~/.bashrc source ~/.bashrc

Check:

pyenv --version


Install Python 3.10

pyenv install 3.10.13

This takes a while on a Pi. Let it finish.


Setting Up a Clean Project Environment

Now let’s keep everything contained:


mkdir ~/hand_led_project cd ~/hand_led_project pyenv local 3.10.13 python -m venv venv source venv/bin/activate

Verify:

python --version


You should see Python 3.10.13.


At this point:

  • System Python is untouched

  • This project uses exactly what it needs

  • MediaPipe will behave


Installing the Libraries

With the virtual environment active:

pip install --upgrade pip pip install mediapipe opencv-python cvzone gpiozero

Opening Python IDLE (This Detail Matters)

From VNC Viewer, open a terminal:

cd ~/hand_led_project source venv/bin/activate python -m idlelib

Inside IDLE:

import sys print(sys.version)

Make sure it still says Python 3.10.


Testing the Camera 

Create a file called camera_test.py:


import cv2 cap = cv2.VideoCapture(0) while True: success, img = cap.read() if not success: break cv2.imshow("Camera Test", img) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()

If a live window opens, the camera side is done.


Testing the LED (One Pi-5 Specific Fix)

Raspberry Pi 5 uses a newer GPIO backend. Install it once:

sudo apt install -y python3-lgpio pip install lgpio sudo usermod -aG gpio pi sudo reboot

After reboot, test the LED:

from gpiozero import LED
from time import sleep led = LED(
led = LED(17) while True:
led.on()
sleep(1)
led.off()
sleep(1)


Blinking LED = everything is wired correctly.


The Fun Part: Hand Gesture Control

Now we connect vision to hardware.

Create hand_led_control.py:


import cv2 from cvzone.HandTrackingModule import HandDetector from gpiozero import LED led = LED(17) cap = cv2.VideoCapture(0) detector = HandDetector(detectionCon=0.7, maxHands=1) while True: success, img = cap.read() hands, img = detector.findHands(img) if hands: fingers = detector.fingersUp(hands[0]) if fingers[1] == 1: led.on() else: led.off() cv2.imshow("Hand Gesture LED Control", img) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() led.off()

Run it.

Raise your index finger.
Watch the LED respond.

That moment never gets old.


Where This Can Go Next

Once this works, ideas start multiplying:

  • Replace the LED with a relay

  • Add more gestures

  • Control multiple outputs

  • Combine with voice or sensors

  • Build touch-free interfaces

This project isn’t about an LED.

It’s about realizing that your hands can directly control hardware using code — and once you see that, you start seeing applications everywhere.



Comments

Popular posts from this blog

Table Toy

  Tinkercad is well suited for students as it not only introduces us to CAD but also helps us to practice some mathematics lessons and therefore, I like using Scale while designing on Tinkercad. I'll be making a Mini Dustpan and Brush Toy for my study table.  This will help me to keep my study table clean from Rubber/Eraser dust and Pencil shavings. Things We Need Laptop or Computer Internet Connection Tinkercad Account 3D printer , filament and a paper cutter (if you are printing) Designing Dustpan Log in to your Tinkercad account and click  3D Design  (Left of your screen). Then click on   Create new design . Click and drag the   ruler   (Top right of your screen) to the   W orkplane . Keep the workplane snap grid measurement to 1.0 mm (Bottom right of your screen). Now, click on   Box   shape under   Tinkercad basic shapes   menu (Right of your screen) and drag it to the workplane. Change the dimensions of the box to 30...

STEAM Horse

You can find cardboard just about anywhere and for cheap, if not free. It’s versatile, easy to use, and an excellent teaching tool. At my home and school this the first thing that I like to give to the kids so that they get the opportunity to embrace STEM activities with a simple and inexpensive material: cardboard. In this project, I'll share how to make a Cardboard Horse and make it run with the help of Power Of Science, without any battery or wheels.  "This is my opening lines in the class before I start with this activity." Time Required: 45  min What:  Horse made out of cardboard which will move with the help of fluid pressure. Why:  Hands-on fun activity on liquid pressure . How:  Read-along... Things We Need 1) Cardboard 30cm*30cm 2) Glue Gun and Glue Sticks(2sticks)  3) X-ACTO knife   4) 2 10 ml Syringe   5) 1 foot of clear plastic tubing that fits the syringe tips  6) A pair of Scissors  7) 1 Toothpick piece  8) Straw, ...

Rose Window with Tinkercad

I love to study ancient civilizations and architecture. Last summer I attended a 3D printing workshop, "Tinkering with Tinkecad" at my school. At the workshop, I learned how 3D printing is not only revolutionizing STEM learning but also aiding other streams like Geography and History. I learned how I can print out historical artifacts to examine and understand them much better. Here, I'm sharing how I used Codeblocks to create the Rose Window of the Abbey of Fiastra . Things We Need Computer Internet 3D software ( Tinkercad ) Computer mice, 3D printer, and filaments for printing(optional) Division For convenience, we will divide the Rose Window into 2 parts,   Inner Shell  and  Outer Shell . Inner Shell: Division and Identification The Inner shell of the Rose Window can divide it into 3 parts: Central Part:   made up of a solid cylinder at the center with hole cylinder with 12 cylindrical holes with the base pointing towards you. Middle Part:   is made up of ...