Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
π Ready to explore and create with us? Click [here] and join today!
Traffic Light Moduleο

Introductionο
The traffic light module is a small device that can display red, yellow and green lights, just like a real traffic light. It can be used to make a traffic light system model or to learn how to control LEDs with Arduino. It is featured with its small size, simple wiring, targeted, and custom installation. It can be connected PWM pin to control the brightness of the LED.
Principleο
The traffic light module can be controlled in two primary ways. The more straightforward method involves using digital inputs from the Arduino, where a HIGH or LOW signal directly turns the corresponding LED on or off. Alternatively, PWM (pulse-width modulation) can be used, especially when varying the brightness of the LED is desired. PWM is a technique where the duty cycle of a digital signal is changed to modulate the brightness of the LED. A duty cycle represents the percentage of time that a signal remains on during a specific period. For instance, a 50% duty cycle implies the signal is active for half the duration and inactive for the remainder. Adjusting the duty cycle allows for the LEDβs brightness modulation.
Module Schematic Diagramο

Usageο
Hardware components
Arduino Uno R4 or R3 board * 1
Traffic Light Module * 1
Jumper Wires
Circuit Assembly

Codeο
Code explanationο
Before any operations, we define constants for the pins where LEDs are connected. This makes our code easier to read and modify.
const int rledPin = 9; //red const int yledPin = 8; //yellow const int gledPin = 7; //green
Here, we specify the pin modes for our LED pins. They are all set to
OUTPUT
because we intend to send voltage to them.
void setup() { pinMode(rledPin, OUTPUT); pinMode(yledPin, OUTPUT); pinMode(gledPin, OUTPUT); }
This is where our traffic light cycle logic is implemented. The sequence of operations is:
Turn the green LED on for 5 seconds.
Blink the yellow LED three times (each blink lasts for 0.5 seconds).
Turn the red LED on for 5 seconds.
void loop() { digitalWrite(gledPin, HIGH); delay(5000); digitalWrite(gledPin, LOW); digitalWrite(yledPin, HIGH); delay(500); digitalWrite(yledPin, LOW); delay(500); digitalWrite(yledPin, HIGH); delay(500); digitalWrite(yledPin, LOW); delay(500); digitalWrite(yledPin, HIGH); delay(500); digitalWrite(yledPin, LOW); delay(500); digitalWrite(rledPin, HIGH); delay(5000); digitalWrite(rledPin, LOW); }
Additional Ideasο
Integrate a buzzer to give sound alerts during the change from green to red, helping visually impaired individuals.