Arduino UNO R4 - Traffic Light
In this guide, we will learn how to control a traffic light module using Arduino UNO R4. We will cover:
- How to connect the traffic light module to Arduino UNO R4
- How to program Arduino UNO R4 to control traffic light module
- How to program Arduino UNO R4 to control traffic light module without using the delay() function

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables STEM V4 IoT Starter Kit (Arduino included) | |
| 1 | × | DIYables STEM V4B IoT Starter Kit (Arduino included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Traffic Light Module
Pinout
A traffic light module has 4 pins:
- GND pin: This is the ground pin. Connect it to the GND on the Arduino UNO R4.
- R pin: This pin controls the red light. Connect it to a digital output on the Arduino UNO R4.
- Y pin: This pin controls the yellow light. Connect it to a digital output on the Arduino UNO R4.
- G pin: This pin controls the green light. Connect it to a digital output on the Arduino UNO R4.

How It Works
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
Jumper wires are good for a quick test, but not for a final build. Simply add a Proto Screw Terminal Block Shield or a Screw Shield V2 to the Arduino UNO R4. This is how it looks with the Screw Shield V2:

This image is created using Fritzing. Click to enlarge image
See The best way to supply power to the Arduino Uno R4 and other components.
How To Program For Traffic Light module
- Set the pins of an Arduino UNO R4 as digital outputs by using the pinMode() function.
- Program to activate the red light using the digitalWrite() function:
Arduino UNO R4 Code
Detailed Instructions
Follow these instructions step by step:
- If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
- Connect the traffic light module the Arduino Uno R4 according to the provided diagram.
- Connect the Arduino Uno R4 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
- Copy the code and open it in Arduino IDE.
- Click the Upload button in Arduino IDE to upload the code to Arduino UNO R4.
- Check out the traffic light module.
Traffic lights work differently depending on their design used in each area. The information given here offers a basic idea of how traffic lights help control traffic.
The code shown above lets you control each light separately. Now, we will improve the code to make it work better.
Arduino UNO R4 Code Optimization
- Let's make the code better by adding a function to control the light.
- We can make the code better by using a for loop.
- Let's make the code better by using the millis() function instead of delay().