PIR Sensor for Drawer/Light Switch: Gesture-Controlled Lighting

Project Overview

This project creates a gesture-controlled light switch that turns lights on and off with a simple hand wave. Using a PIR sensor, the system detects motion and toggles a relay that controls the light. A short wave turns the light on, and another wave turns it off.

Difficulty: Beginner
Estimated time: 1-2 hours
Estimated cost: $15-25

How It Works

A PIR sensor detects hand movement within a short range (10-30cm). Each time motion is detected, the Arduino toggles a relay that controls the light. A status LED indicates the current state, and debouncing prevents rapid toggling.

Materials Needed

  • Arduino Nano or Arduino Uno (1)
  • HC-SR501 PIR sensor (1)
  • Relay module (5V, single channel)
  • LED for status indication
  • 220Ω resistor for LED
  • Jumper wires
  • Power supply (5V 1A)
  • Project enclosure

Circuit Diagram

Connection Table

VCC

5V

GND

GND

OUT

Digital Pin 2

IN

Digital Pin 3

Anode

Digital Pin 13 (via 220Ω)

Arduino Code

// Gesture-Controlled Light Switch
const int pirPin = 2;
const int relayPin = 3;
const int ledPin = 13;

bool lightState = false;
unsigned long lastTriggerTime = 0;
const unsigned long debounceDelay = 1000;

void setup() {
  Serial.begin(9600);
  
  pinMode(pirPin, INPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  
  digitalWrite(relayPin, LOW);
  digitalWrite(ledPin, LOW);
  
  delay(30000);
}

void toggleLight() {
  lightState = !lightState;
  digitalWrite(relayPin, lightState ? HIGH : LOW);
  digitalWrite(ledPin, lightState ? HIGH : LOW);
}

void loop() {
  bool motion = digitalRead(pirPin) == HIGH;
  
  if (motion && (millis() - lastTriggerTime > debounceDelay)) {
    lastTriggerTime = millis();
    toggleLight();
  }
  
  delay(50);
}

Enhanced Version with LED Fade Effect

const int pirPin = 2;
const int relayPin = 3;
const int ledPin = 13;

bool lightState = false;
unsigned long lastTriggerTime = 0;
const unsigned long debounceDelay = 1000;
unsigned long fadeStartTime = 0;
bool fading = false;

void fadeLED(int start, int end) {
  int duration = 500;
  unsigned long startTime = millis();
  while (millis() - startTime < duration) {
    int brightness = map(millis() - startTime, 0, duration, start, end);
    analogWrite(ledPin, brightness);
    delay(5);
  }
  analogWrite(ledPin, end);
}

void toggleLight() {
  lightState = !lightState;
  digitalWrite(relayPin, lightState ? HIGH : LOW);
  
  if (lightState) fadeLED(0, 255);
  else fadeLED(255, 0);
}

void setup() {
  pinMode(pirPin, INPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(relayPin, LOW);
  digitalWrite(ledPin, LOW);
  delay(30000);
}

void loop() {
  if (digitalRead(pirPin) == HIGH && (millis() - lastTriggerTime > debounceDelay)) {
    lastTriggerTime = millis();
    toggleLight();
  }
  delay(50);
}

Installation Steps

  1. Adjust PIR sensor sensitivity to minimum range (10-30cm)
  2. Assemble circuit on breadboard and test
  3. Upload code and test gesture detection
  4. Mount sensor in desired location
  5. Wire the light to the relay
  6. Enclose electronics in a safe enclosure
  7. Wave hand to turn light on/off

Project Extensions

  • Add second PIR sensor for dimming control
  • Use RGB LED strip and cycle colors with multiple gestures
  • Add multiple sensors to control different lights
  • Add ESP32 to send state to Home Assistant
  • Add rechargeable battery for portable use

Conclusion

This gesture-controlled light switch adds a touch of magic to any room, perfect for kitchens, workshops, or simply for the novelty of waving to control lights.

Leave a Reply

Your email address will not be published. Required fields are marked *

Component Pin Arduino Pin
PIR Sensor PIR Sensor PIR Sensor Relay Module Status LED