Arduino-Controlled Traffic Light System with Touch Sensor Interaction
View and Edit Project In Cirkit Designer
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
- Description: A traffic light module with three LEDs (Green, Yellow, Red) and a common ground.
- Pins: Green, Yellow, Red, GND
Arduino UNO
- Description: A microcontroller board based on the ATmega328P, used for controlling the traffic light LEDs and reading the touch sensor input.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13
Touch Sensor
- Description: A capacitive touch sensor module used to change the state of the traffic light.
- Pins: IO, VCC, GND
Wiring Details
Traffic Light
- Green LED: Connected to Arduino UNO's D4 pin.
- Yellow LED: Connected to Arduino UNO's D3 pin.
- Red LED: Connected to Arduino UNO's D2 pin.
- GND: Connected to Arduino UNO's GND pin.
Arduino UNO
- 5V: Provides power to the touch sensor's VCC pin.
- GND: Common ground for the Traffic Light and Touch Sensor.
- D2: Controls the Red LED of the Traffic Light.
- D3: Controls the Yellow LED of the Traffic Light.
- D4: Controls the Green LED of the Traffic Light.
- D7: Reads the state from the Touch Sensor's IO pin.
Touch Sensor
- IO: Sends the touch state to Arduino UNO's D7 pin.
- VCC: Receives power from Arduino UNO's 5V pin.
- GND: Connected to Arduino UNO's GND pin.
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.