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

Arduino UNO Controlled Traffic Light with Touch Sensor

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled Traffic Light with Touch Sensor

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 in a cycle where the touch sensor's input is used to switch between the traffic light states: red, yellow, green, and off. The Arduino UNO reads the state of the touch sensor and controls the traffic light LEDs accordingly.

Component List

Touch Sensor

Arduino UNO

Traffic Light

Wiring Details

Touch Sensor

Arduino UNO

Traffic Light

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 touchPin = 7; // Pin connected to touch sensor IO
const int redPin = 2;   // Pin connected to red LED
const int yellowPin = 3; // Pin connected to yellow LED
const int greenPin = 4;  // Pin connected to green LED

int touchState = 0; // Current state of the touch sensor
int lastTouchState = 0; // Previous state of the touch sensor
int ledState = 0; // State of the LEDs (0: off, 1: red, 2: yellow, 3: green)

void setup() {
  pinMode(touchPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  digitalWrite(redPin, LOW);
  digitalWrite(yellowPin, LOW);
  digitalWrite(greenPin, LOW);
}

void loop() {
  touchState = digitalRead(touchPin);

  // Check if the touch sensor state has changed
  if (touchState != lastTouchState) {
    if (touchState == HIGH) {
      // Cycle through LED states
      ledState = (ledState + 1) % 4;
      updateLEDs();
    }
    delay(50); // Debounce delay
  }

  lastTouchState = touchState;
}

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 (ledState) {
    case 1:
      digitalWrite(redPin, HIGH);
      break;
    case 2:
      digitalWrite(yellowPin, HIGH);
      break;
    case 3:
      digitalWrite(greenPin, HIGH);
      break;
  }
}

The code is written for the Arduino UNO microcontroller. It initializes the pins connected to the touch sensor and traffic light LEDs, reads the state of the touch sensor, and cycles through the traffic light states upon each touch sensor activation. The updateLEDs function is responsible for turning on the appropriate LED based on the current state.