Arduino UNO Controlled Traffic Light System with Touch Sensor Interaction
View and Edit Project In Cirkit Designer
Traffic Light Control System Documentation
Summary
The circuit described herein is designed to control a traffic light system using a touch sensor. The system operates on a simple state machine logic, where each touch toggles the traffic light through a sequence of states: all off, red on, yellow on, green on, and then back to all off. An Arduino UNO microcontroller is used to read the state of the touch sensor and control the traffic light LEDs accordingly.
Component List
Touch Sensor
- Name: Touch Sensor
- Pins: IO, VCC, GND
- Description: A sensor that detects touch or proximity to trigger an event.
- Purpose: To provide user input for changing the state of the traffic light.
Traffic Light
- Name: Traffic Light
- Pins: Green, Yellow, Red, GND
- Description: A set of LEDs representing the green, yellow, and red lights of a standard traffic signal.
- Purpose: To visually represent the traffic light state as controlled by the Arduino UNO.
Arduino UNO
- Name: Arduino UNO
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13
- Description: A microcontroller board based on the ATmega328P.
- Purpose: To process the input from the touch sensor and control the output to the traffic light LEDs.
Wiring Details
Touch Sensor
- IO: Connected to Arduino UNO Digital Pin 7 (D7)
- VCC: Connected to Arduino UNO 3.3V
- GND: Connected to Arduino UNO Ground (GND)
Traffic Light
- Green: Connected to Arduino UNO Digital Pin 4 (D4)
- Yellow: Connected to Arduino UNO Digital Pin 3 (D3)
- Red: Connected to Arduino UNO Digital Pin 2 (D2)
- GND: Connected to Arduino UNO Ground (GND)
Arduino UNO
- D7: Connected to Touch Sensor IO
- 3.3V: Connected to Touch Sensor VCC
- GND: Connected to both Touch Sensor GND and Traffic Light GND
- D4: Connected to Traffic Light Green
- D3: Connected to Traffic Light Yellow
- D2: Connected to Traffic Light Red
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 the touch sensor
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
int state = 0; // Variable to hold the current state
int lastTouchState = LOW; // Variable to hold the last touch state
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() {
int touchState = digitalRead(touchPin);
if (touchState == HIGH && lastTouchState == LOW) {
state = (state + 1) % 4;
updateLEDs();
delay(200); // Debounce delay
}
lastTouchState = touchState;
}
void updateLEDs() {
switch (state) {
case 0:
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
break;
case 1:
digitalWrite(redPin, HIGH);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
break;
case 2:
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, HIGH);
digitalWrite(greenPin, LOW);
break;
case 3:
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
break;
}
}
This code is saved as sketch.ino and is intended to be uploaded to 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 updates the LEDs based on the current state. The updateLEDs() function is responsible for turning the appropriate LEDs on or off depending on the value of the state variable.