Arduino Nano - Traffic Light
This tutorial instructs you how to use an Arduino Nano to control a traffic light module. Specifically, we'll cover the following aspects:
- Establishing the connection between the traffic light module and the Arduino Nano
- Programming the Arduino Nano to oversee the RGB traffic light module
- Implementing Arduino Nano programming to regulate the RGB traffic light module without relying on the delay() function.
Hardware Preparation
1 | × | Arduino Nano | |
1 | × | USB A to Mini-B USB cable | |
1 | × | Traffic Light Module | |
1 | × | Jumper Wires | |
1 | × | (Optional) 9V Power Adapter for Arduino Nano | |
1 | × | (Recommended) Screw Terminal Expansion Board for Arduino Nano |
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
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 comprises four pins:
- GND pin: Connect this pin to the GND of Arduino Nano.
- R pin: Controls the red light; connect this pin to a digital output of Arduino Nano.
- Y pin: Manages the yellow light; connect this pin to a digital output of Arduino Nano.
- G pin: Governs the green light; connect this pin to a digital output of Arduino Nano.
How It Works
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
How To Program For Traffic Light module
- Configure an Arduino Nano's pins to the digital output mode by using pinMode() function
- Program to turn ON red light by using digitalWrite() function:
Arduino Nano Code
Detailed Instructions
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino Nano
- Check out the traffic light module
It's important to note that the exact workings of a traffic light can vary depending on the specific design and technology used in different regions and intersections. The principles described above provide a general understanding of how traffic lights operate to manage traffic and enhance safety on the roads.
The code above demonstrates individual light control. Now, let's enhance the code for better optimization.
Arduino Nano Code Optimization
- Let's improve the code by implementing a function for light control.
- Let's improve the code by using a for loop.
- Let's improve the code by using millis() function intead of delay().