Maker.io main logo

Breadboard a Traffic Light That Reacts to You

73

2025-07-16 | By Cheyenne Chua

License: Attribution

Materials Needed

imageimageimageimageimageimage

Process 1: Create the Circuit

  1. ‎‎Connect the red wire from the battery (4×AA = 6V) to the positive rail of the breadboard, and the black wire to negative rail

  2. This provides a common power and ground reference for the entire circuit

  3. Connect IR Sensor

    • VCC → ESP32 3.3V

    • GND → ESP32 GND

    • OUT → ESP32 GPIO 18

    • The sensor output pin sends a digital HIGH or LOW signal depending on object proximity. GPIO 18 is the chosen input pin on ESP32 for reading this signal.

  4. Connect the LED as:

    • Anode (longer leg) → 220Ω resistor → ESP32 GPIO pin (GPIO 23 for Red, GPIO 22 for Yellow, GPIO 21 for Green)

    • Cathode (shorter leg) → ESP32 GND

    • LEDs need resistors to limit current. 220Ω is a good safe value to prevent burnout without being too dim

Process 2: Create the Code

Refer to the code as shown below

  • The IR sensor detects the start and end of a hand gesture.

  • When the hand is removed, the program calculates how long it was there and lights up the corresponding LED

Copy Code
// Pin assignments
const int irPin = 18;
const int redLED = 23;
const int yellowLED = 22;
const int greenLED = 21;
unsigned long handStartTime = 0;
bool handDetected = false;
void setup() {
    pinMode(irPin, INPUT);
    pinMode(redLED, OUTPUT);
    pinMode(yellowLED, OUTPUT);
    pinMode(greenLED, OUTPUT);
    Serial.begin(115200);
}
void loop() {
    int irValue = digitalRead(irPin);
    if (irValue == LOW) { // LOW = hand detected (typical for IR modules)
        if (!handDetected) {
            handStartTime = millis();
            handDetected = true;
            Serial.println("Hand placed on sensor.");
        }
        
        unsigned long duration = (millis() - handStartTime) / 1000;
        // Light logic
        if (duration >= 3000) {
            digitalWrite(redLED, LOW);
            digitalWrite(yellowLED, LOW);
            digitalWrite(greenLED, HIGH); 
        } else if (duration >= 2000) {
            digitalWrite(redLED, LOW); 
            digitalWrite(yellowLED, HIGH); 
            digitalWrite(greenLED, LOW); 
        } else if (duration >= 1000) {
            digitalWrite(redLED, HIGH); 
            digitalWrite(yellowLED, LOW); 
            digitalWrite(greenLED, LOW); 
        } else { // Less than a second
            // Has all OFF
            digitalWrite(redLED, LOW);  
            digitalWrite(yellowLED, LOW);  
            digitalWrite(greenLED, LOW);  
        }
    } else { // No hand detected -- reset everything
        handDetected = false;  
        digitalWrite(redLED, LOW);   
        digitalWrite(yellowLED, LOW);   
        digitalWrite(greenLED, LOW);   
    }
}

Process 3: Transferring Data

  1. Connect ESP32 to your laptop via a USB Type-C cable.

  2. In Arduino IDE:

    • Go to Tools > Board > ESP32 Dev Module

    • Go to Tools > Port, select the COM port (e.g., COM4)

  3. Click Upload

  4. Once uploaded, disconnect the cable

  5. The board is now running on battery

Process 4: Testing

  1. Power up your circuit with the battery holder.

  2. Place your hand close to the IR sensor:

    • Hold for 10 seconds → Red LED lights up

    • Hold for 20 seconds → Yellow LED lights up

    • Hold for 30 seconds → Green LED lights up

  3. Remove your hand to trigger the light

 

Troubleshooting & Suggestions

table

Closing thoughts

Congratulations! You've now built an interactive, sensor-based traffic light using touch detection and microcontroller logic! This project teaches fundamental electronics, sensor interfacing, and control logic. While we used the ESP32 here, feel free to experiment with an Arduino Nano or Uno if you prefer simpler setups.

image

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.