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!

Lesson 29: Traffic Light Module

In this lesson, you will learn how to use Arduino to control a mini LED traffic light. We’ll cover programming the Arduino Uno to cycle through green, yellow, and red lights, simulating a real traffic signal. This project is ideal for beginners as it provides practical experience in coding light sequences and timing controls on the Arduino platform.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Arduino UNO R3 or R4

BUY

Traffic Light Module

BUY

Wiring

../_images/Lesson_29_traffic_light_circuit_uno_bb.png

Code

Code Analysis

  1. 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
  1. 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);
}
  1. 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);
}