Arduino Nano ESP32 - Traffic Light
In this tutorial, we will explore how to utilize the Arduino Nano ESP32 to control a traffic light module. In detail, we will learn:
- How to connect the traffic light module to Arduino Nano ESP32
- How to program Arduino Nano ESP32 to control RGB traffic light module
- How to program Arduino Nano ESP32 to control RGB traffic light module without using delay() function
|Arduino Nano ESP32
|USB Cable Type-C
|Traffic Light Module
|(Recommended) Screw Terminal Adapter for Arduino Nano
Or you can buy the following sensor kit:
|DIYables Sensor Kit 30 types, 69 units
Overview of Traffic Light Module
A traffic light module includes 4 pins:
- GND pin: The ground pin, connect this pin to GND of Arduino Nano ESP32.
- R pin: The pin to control the red light, connect this pin to a digital output of Arduino Nano ESP32.
- Y pin: The pin to control the yellow light, connect this pin to a digital output of Arduino Nano ESP32.
- G pin: The pin to control the green light, connect this pin to a digital output of Arduino Nano ESP32.
How It Works
This image is created using Fritzing. Click to enlarge image
How To Program For Traffic Light module
- Configure an ESP32's pins to the digital output mode by using pinMode() function
- Program to turn ON red light by using digitalWrite() function:
Arduino Nano ESP32 Code
To get started with Arduino Nano ESP32, follow these steps:
- If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
- Wire the components according to the provided diagram.
- Connect the Arduino Nano ESP32 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the Arduino Nano ESP32) board and its corresponding COM port.
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino Nano ESP32
- 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 ESP32 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().