ESP32 LED Mini Traffic Light Tutorial + Code

Here’s a quick tutorial on how to get mini LED traffic light modules working with the ESP32. These display traffic modules have built in red, yellow (orange) and green LED lights.

Source Code

The source code is below so just copy and paste it into the Arduino IDE. Note that if you’re using jumper cables to connect the traffic light module to the Arduino board then I recommend you change the LED pins to 8 (green), 9 (yellow) and 10 (red). This avoids the red light conflicting with the Arduino’s onboard LED on pin 13.

//ESP32 mini traffic light code
//Designed so mini traffic light LED plugs into ESP32 pins 21, 22, 23 and GND
//Pins also referred to as GPIO21, GPIO22, GPIO23
 
#define GREEN_LED  21
#define YELLOW_LED  22
#define RED_LED  23
 
void setup() {
  pinMode(GREEN_LED,OUTPUT);
  pinMode(YELLOW_LED,OUTPUT);
  pinMode(RED_LED,OUTPUT);
}
 
void loop() {
  //Green light
  digitalWrite(GREEN_LED,HIGH);
  digitalWrite(YELLOW_LED,LOW);
  delay(5000);
 
  //Yellow light
  digitalWrite(GREEN_LED,LOW);
  digitalWrite(YELLOW_LED,HIGH);
  delay(2500);
 
  //Red light
  digitalWrite(YELLOW_LED,LOW);
  digitalWrite(RED_LED,HIGH);
  delay(5000);
 
  //Yellow light
  digitalWrite(RED_LED,LOW);
  digitalWrite(YELLOW_LED,HIGH);
  delay(5000);
}

The great thing about these modules is that you can make a demo traffic light Arduino application using just a single component and you don’t even need any resistors. You don’t even need any jumper cables either since the traffic light can plug directly into pins 11, 12, 13 and GND on the Arduino board!

The other cool thing about these traffic lights is as they’re 55mm tall they’re an idea scale for Lego cities and also model railways. They’re extremely cheap too – I bought a 5 pack of them for just £1.02 on AliExpress. I did have to pay VAT and postage on top of that, but I combined the order with an order for a few other electronics components at the same time. Incidentally you’ll also find these for same on Amazon, eBay and dedicated online electronics components vendors. If you’re in a hurry use Amazon although my AliExpress order only took 7 days to arrive in the UK from China.

So what are the downsides to these traffic light modules? In my particular ones the red LED is significantly brighter than the yellow and green LEDs. At least your Lego citizens are unlikely to run a red light though. I also think the yellow LED is quite feeble, and does kind of look orangey too. The green LED looks kind of yellow. Still, for the price I really can’t complain.

I’ve also been unable to find a 3D printed case for them. So they might not be suitable if you want them for a mini city or diorama. If you do find a case that fits them, then do drop a comment below.

By they way, I haven’t put any fancy code into this demo. The lights simply alternative between red, yellow and green. In the UK traffic lights flash the amber light before going green again so you might like to put a loop in to achieve this (especially if you’re using this code for a school computer science project!). Another great addition to the code would be to add a button to trigger the lights at a pedestrian crossing, and maybe use a piezzo buzzer to add on a crossing sound.

I hope you like this code and let us know in the comments section below or in the YouTube video’s comments what exciting uses you’ve thought up for them.

Leave a Reply

Your email address will not be published. Required fields are marked *