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 in a cycle where the touch sensor's input is used to switch between the traffic light states: off, red, yellow, and green. Each state is represented by an LED on the traffic light module. The Arduino UNO reads the input from the touch sensor and controls the state of each LED accordingly.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13.
- Purpose: Acts as the central processing unit of the circuit, reading sensor inputs and controlling the traffic light LEDs.
Touch Sensor
- Description: A capacitive touch sensor module.
- Pins: IO, VCC, GND.
- Purpose: Provides input to the Arduino UNO to change the traffic light states.
Traffic Light Module
- Description: A module with three LEDs representing the green, yellow, and red lights of a traffic light.
- Pins: Green, Yellow, Red, GND.
- Purpose: Displays the current state of the traffic light as controlled by the Arduino UNO.
Wiring Details
Arduino UNO
- 5V: Provides power to the touch sensor.
- GND: Connected to the ground pins of the touch sensor and the traffic light module to complete the circuit.
- D7: Connected to the IO pin of the touch sensor to read its state.
- D4: Controls the green LED of the traffic light module.
- D3: Controls the yellow LED of the traffic light module.
- D2: Controls the red LED of the traffic light module.
Touch Sensor
- VCC: Connected to the 5V output from the Arduino UNO.
- GND: Connected to the ground on the Arduino UNO.
- IO: Sends the touch event signal to the Arduino UNO's D7 pin.
Traffic Light Module
- Green: Controlled by the Arduino UNO's D4 pin.
- Yellow: Controlled by the Arduino UNO's D3 pin.
- Red: Controlled by the Arduino UNO's D2 pin.
- GND: Shares a common ground with the Arduino UNO.
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
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 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);
// Check for touch sensor press
if (touchState == HIGH && lastTouchState == LOW) {
state = (state + 1) % 4; // Cycle through states 0 to 3
updateLEDs();
}
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 written for the Arduino UNO and is responsible for reading the state of the touch sensor and controlling the LEDs on the traffic light module. The setup() function initializes the pins and sets the initial state of the LEDs to off. The loop() function continuously checks the state of the touch sensor and updates the LEDs accordingly. The updateLEDs() function sets the appropriate LED states based on the current value of the state variable.
