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

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 ...

Space Invader With MakeCode

  Microsoft MakeCode is a framework for creating interactive and engaging programming experiences for those new to the world of programming. The platform provides the foundation for a tailored coding experience to create and run user programs on actual hardware or in a simulated target. MakeCode uses the blocks programming model to let the user learn coding concepts in a more tangible fashion. The blocks map directly to actual lines of code in a programming language. So, once a user has a sense of confidence and familiarity with how the blocks work, they can transition to coding more complex programs in the programming language itself. In this Instructable,  we'll develop  a simplified version of the classic ' Space Invaders ' game using Microsoft MakeCode. As opposed to the real game of Space Invaders game, our Invader(enemy) will be firing and moving horizontally across the top of the screen. The player will control a laser cannon by moving it horizontally across the ...

Bernoulli’s Mist Sprayer

  No doubt students learn more when they are mentally and physically engaged in the learning process, regardless of the subject they are studying, and when it comes to STEM education , hands-on experiential learning is an indispensable part of the learning process. In this hands-on activity, we will make a water sprayer using two straws, and experience Bernoulli’s Theorem . Category:  Physics Sub Topic:   Fluid Pressure Time Required : 20-30 min Difficulty:  Easy Things We Need 2 Straws (4 inches each) Insulation Tape Eraser Scissors A glass of water Assembly Pic 1:   Cut two insulation tape pieces and attach them to either end of both the straws. Pic 2:   While attaching the tape, make sure to leave some space from the end(4-5mm approx). Pic 3 : Tape the 1st straw(I used Green straw as 1st) to the eraser so that the straw is exactly aligned with the edge of the eraser. Pic 4:   Next, tape the second straw(I used Orange straw as 2nd) to the edge of th...