Model traffic light using LEDs and digital I/O with GPIO ESP32

zhil
5 min readFeb 12, 2022

In THIS ARTICLE, I will show you how I designed and constructed a simple ESP32 circuit to receive, process, and display digital inputs and output, using LED and pushbuttons, to create a model traffic light. This little project is part of a course in embedded systems guided by the excellent Dr. Kusprasapta Mutijarsa, S.T, M.T. and his assistant Kak Chus, both of the Bandung Institute of Technology (ITB).

Prerequisites

  1. An ESP32 microcontroller, along with its vertebrate
  2. A breadboard
  3. A Micro-USB cord
  4. 6 male-to-male jumper wires. It is advisable to use different (and relevant) colors.
  5. A pushbutton. For aesthetical purposes, it is better to use colors other than red, yellow, or green
  6. Red, yellow, and green 3mm LED
  7. 4 resistor, 330Ω
  8. Arduino IDE installed for ESP32. I have made a detailed installation guide in my previous article: https://zhillan-arf.medium.com/blink-the-led-with-esp32-5c3f358ea2ff

Turning LED on and off using a pushbutton switch with GPIO ESP32

  1. I began by connecting the 3V3 pin with the pushbutton, resistor, and ground (GND) pin. This provides a constant electric voltage of, as the pin suggests, 3V, to the button. Later, I added a cap to make the pushbutton easier to press.

2. I then connected the button to the GPIO4 pin in a parallel arrangement. This will serve as an input — whenever the button is pressed, electric current will flow into GPIO4, which will be processed. The output is then channeled out of GPT5 and into the LED, which was then connected with the ground using another resistor. The physical circuit is finished.

It is important to be mindful of the placement of the pins and wires. I missed a pin placement by a mere column and understandably, it screwed up my whole circuit. In such cases, you should debug the circuit one component at a time.

3. For the software side, I used the code below. It stores the status of the button in the buttonState variable, which is then used to determine whether the outpin pin (GPT05) should be set into high or low.

const int buttonPin = 4; 
const int ledPin = 5;
int buttonState = 0; // variable for reading the pushbutton statusvoid setup() {
Serial.begin(115200);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
// check if the pushbutton is pressed
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Voila! After I compiled and uploaded it into the board, it works as intended. The LED turns on only when the button is pressed.

Model traffic light

With knowledge of the how-tos of ESP32 I/O, we can move on to the second project. Here, I aim to create a model traffic light using three LEDs, its supporting cables and resistors, and a pushbutton. My plan is for every time the button is pressed, the LED state must switch from red to yellow and then to green, and vice versa. In a RED state, for example, only the red LED will be active.

For this purpose, I chose:

  1. GPIO5 as the output pin for the red LED,
  2. GPIO18 for the yellow LED,
  3. GPIO19 for the green LED.

Connecting the circuit is pretty much the repeat of Process 2 of this article’s first project. The code however necessitates some funny tricks.

#define RED 0
#define YELLOW 1
#define GREEN 2
const int buttonPin = 4;
const int ledRed = 5; // the number of the LED pin
const int ledYellow = 18;
const int ledGreen = 19;
int buttonState = 0; // pushbutton status
int brightness = 0;
int LEDstate = RED;
void setup() {
// put your setup code here, to run once:
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// initialize the LED pins as an output:
pinMode(ledRed, OUTPUT);
pinMode(ledYellow, OUTPUT);
pinMode(ledGreen, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, switch state:
if (buttonState == HIGH) {
if (LEDstate == RED) {
// Switch to green
digitalWrite(ledRed, LOW);
digitalWrite(ledYellow, HIGH);
LEDstate = YELLOW;
delay(1000);
digitalWrite(ledYellow, LOW);
digitalWrite(ledGreen, HIGH);
LEDstate = GREEN;
}
else if (LEDstate == GREEN) {
// Switch to red
digitalWrite(ledGreen, LOW);
digitalWrite(ledYellow, HIGH);
LEDstate = YELLOW;
delay(1000);
digitalWrite(ledYellow, LOW);
digitalWrite(ledRed, HIGH);
LEDstate = RED;
}
// Nothing happens if LEDstate is yellow
}
}

I divided the code into two parts. The “headers” detail the constants and variables being used. The loop system contains the details of the switch. I then stored the state of both the button and the active LED in a variable respectively, and manipulate those variables using the codes above.

Voila! this is the end result.

Well, that is all from me today. Stay tuned for my next project. Bye-onara!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

zhil
zhil

Written by zhil

Five coffee a day keeps your esophagus away

No responses yet

Write a response