Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Controlled Traffic Light System with Touch Sensor Interaction

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Traffic Light System with Touch Sensor Interaction

Traffic Light Control Circuit Documentation

Summary

This circuit is designed to control a traffic light system using an Arduino UNO microcontroller and a touch sensor. The system operates by cycling through the traffic light's red, yellow, and green LEDs in response to touch sensor inputs. Each press of the touch sensor advances the traffic light to the next state: red, yellow, green, and then off. The circuit is powered by the Arduino UNO, which also manages the control logic through an embedded program.

Component List

Traffic Light

Arduino UNO

Touch Sensor

Wiring Details

Traffic Light

Arduino UNO

Touch Sensor

Documented Code

/*
 * This Arduino sketch controls a traffic light system using a touch sensor.
 * When the touch sensor is pressed, the red LED turns on. On the next press,
 * the yellow LED turns on. On the next press, the green LED turns on. On the
 * next press, all LEDs turn off. This cycle repeats with each subsequent press.
 */

const int redPin = 2;    // Pin connected to the red LED
const int yellowPin = 3; // Pin connected to the yellow LED
const int greenPin = 4;  // Pin connected to the green LED
const int touchPin = 7;  // Pin connected to the touch sensor

int currentState = 0;     // Current state of the traffic light
int lastTouchState = LOW; // Previous state of the touch sensor

void setup() {
  pinMode(redPin, OUTPUT);    // Set the red LED pin as an output
  pinMode(yellowPin, OUTPUT); // Set the yellow LED pin as an output
  pinMode(greenPin, OUTPUT);  // Set the green LED pin as an output
  pinMode(touchPin, INPUT);   // Set the touch sensor pin as an input
}

void loop() {
  int touchState = digitalRead(touchPin); // Read the state of the touch sensor

  // If the touch sensor is pressed (and was not pressed before)
  if (touchState == HIGH && lastTouchState == LOW) {
    currentState = (currentState + 1) % 4; // Advance to the next state
    updateLEDs(); // Update the LEDs based on the new state
    delay(200); // Debounce delay to avoid detecting multiple presses from one touch
  }

  lastTouchState = touchState; // Update the last touch state
}

void updateLEDs() {
  // Turn off all LEDs
  digitalWrite(redPin, LOW);
  digitalWrite(yellowPin, LOW);
  digitalWrite(greenPin, LOW);

  // Turn on the appropriate LED based on the current state
  switch (currentState) {
    case 1:
      digitalWrite(redPin, HIGH); // Red LED on
      break;
    case 2:
      digitalWrite(yellowPin, HIGH); // Yellow LED on
      break;
    case 3:
      digitalWrite(greenPin, HIGH); // Green LED on
      break;
  }
}

This code is saved in a file named sketch.ino and is intended to be uploaded to the Arduino UNO microcontroller. The code sets up the necessary pins as inputs or outputs and contains a main loop that reads the state of the touch sensor to cycle through the traffic light states. The updateLEDs function is responsible for turning on the appropriate LED based on the current state.