Circuit Documentation
Summary of the Circuit
This circuit is designed to control a traffic light using an Arduino UNO microcontroller and a joystick module. The traffic light has three LEDs (red, yellow, and green) that are controlled based on the movement of the joystick. When the joystick is moved in different directions, the corresponding LED on the traffic light is activated. The joystick module provides analog input to the Arduino, which then drives the LEDs 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.
Traffic Light
- Description: A simple traffic light module with three LEDs (Green, Yellow, Red).
- Pins: Green, Yellow, Red, GND.
Joystick Module
- Description: An input module that provides analog signals based on the X and Y movement of the joystick and a digital signal when the joystick is pressed.
- Pins: SW, VRY, VRX, VCC, GND.
Wiring Details
Arduino UNO
- 5V connected to Joystick module VCC.
- GND connected to Traffic Light GND and Joystick module GND.
- A0 connected to Joystick module VRX.
- A1 connected to Joystick module VRY.
- D2 connected to Traffic Light Red.
- D3 connected to Traffic Light Yellow.
- D4 connected to Traffic Light Green.
- D7 connected to Joystick module SW.
Traffic Light
- Green connected to Arduino UNO D4.
- Yellow connected to Arduino UNO D3.
- Red connected to Arduino UNO D2.
- GND connected to Arduino UNO GND.
Joystick Module
- VCC connected to Arduino UNO 5V.
- GND connected to Arduino UNO GND.
- VRX connected to Arduino UNO A0.
- VRY connected to Arduino UNO A1.
- SW connected to Arduino UNO D7.
Documented Code
/*
* This Arduino sketch controls a traffic light using a joystick module.
* When the joystick is moved up, the red LED turns on.
* When the joystick is moved left, the yellow LED turns on.
* When the joystick is moved right, the green LED turns on.
* When the joystick is moved down, all LEDs turn on.
*/
// Pin definitions
const int redLEDPin = 2;
const int yellowLEDPin = 3;
const int greenLEDPin = 4;
const int joystickVRXPin = A0;
const int joystickVRYPin = A1;
void setup() {
// Initialize LED pins as outputs
pinMode(redLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
// Initialize joystick pins as inputs
pinMode(joystickVRXPin, INPUT);
pinMode(joystickVRYPin, INPUT);
}
void loop() {
// Read joystick values
int vrxValue = analogRead(joystickVRXPin);
int vryValue = analogRead(joystickVRYPin);
// Determine joystick direction
if (vryValue < 300) { // Joystick moved up
digitalWrite(redLEDPin, HIGH);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
} else if (vrxValue < 300) { // Joystick moved left
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, HIGH);
digitalWrite(greenLEDPin, LOW);
} else if (vrxValue > 700) { // Joystick moved right
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, HIGH);
} else if (vryValue > 700) { // Joystick moved down
digitalWrite(redLEDPin, HIGH);
digitalWrite(yellowLEDPin, HIGH);
digitalWrite(greenLEDPin, HIGH);
} else { // Joystick in neutral position
digitalWrite(redLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
digitalWrite(greenLEDPin, LOW);
}
delay(100); // Small delay for debounce
}
This code is designed to be uploaded to the Arduino UNO microcontroller. It initializes the pins connected to the traffic light LEDs as outputs and the joystick's analog pins as inputs. In the main loop, it reads the analog values from the joystick and turns on the corresponding LED based on the joystick's position. A small delay is included to prevent debounce issues.
