The lamp test
Four wires, no library, and a sketch that lights each colour in turn and prints its name. If the light that comes on is the one the serial monitor names, the wiring is right, and every later sketch in the book will work on it.
Four wires
GND to GND, then RED, YELLOW and GREEN to three digital pins, in that order. The two NC pins stay unconnected, because nothing on the board is attached to them.
The pins are the same in every sketch in this book: D9, D10 and D11 on an Uno; GPIO 25, 26 and 27 on an ESP32; GPIO 4, 5 and 6 on an ESP32-S3; GP13, GP14 and GP15 on a Pico. Each set is consecutive, each pin can do PWM, and none is a pin the board reads at boot or uses for its flash memory.
The sketch
Three pinMode lines make the three pins outputs. Leave one out and that
light stays dark or glows faintly, while the others work, which looks like a
broken LED and is not.
flash() lights one pin for a second and prints its name. loop() calls it
for each colour in turn, then lights all three together for a second, then
none. The all-three step is there because it is the only way to check all
three loops at once, and because it shows what one pin, one
light said: nothing on
the board stops it.
What you should see
Red, yellow, green, all three, off, once a second, and in the serial monitor at 115200:
RED
YELLOW
GREEN
all three
offThe light that is on should always be the one just printed. If a name and a
light disagree, two wires are swapped. If one colour stays dark, check its
wire, then its pinMode line. When a light stays
dark has the rest.
On an ESP32, an ESP32-S3 or a Pico the lights are dimmer than on an Uno, and the green more so than the other two. Nothing is wrong. The next article is about why.
The code
No library. Three pinMode lines make the pins outputs, then each light comes on for a second with its name printed. Change the three pin numbers to match your board.
/*
Traffic Light - lamp test TK03 / /p/tk03
Wiring. Count from the square pad on the TinkerBlock board, LEDs up,
header at the bottom:
GND -> GND
NC -> nothing (both NC pins are unconnected on the board)
RED -> D9 on an Uno, GPIO 25 on an ESP32, GPIO 4 on an ESP32-S3,
GP13 on a Raspberry Pi Pico
YELLOW -> D10 on an Uno, GPIO 26 on an ESP32, GPIO 5 on an ESP32-S3,
GP14 on a Pico
GREEN -> D11 on an Uno, GPIO 27 on an ESP32, GPIO 6 on an ESP32-S3,
GP15 on a Pico
Arduino IDE
Tools > Board your board, e.g. ESP32S3 Dev Module
Tools > Port the one that appears when you plug in
Tools > USB CDC On Boot Enabled (ESP32-S3 only)
No library needed.
*/
// GPIO numbers. Uno: 9, 10, 11. ESP32: 25, 26, 27. ESP32-S3: 4, 5, 6.
// Pico: 13, 14, 15.
const int RED_PIN = 4;
const int YELLOW_PIN = 5;
const int GREEN_PIN = 6;
// Light one pin for a second and say which it was.
void flash(int pin, const char *name) {
digitalWrite(pin, HIGH);
Serial.println(name);
delay(1000);
digitalWrite(pin, LOW);
}
void setup() {
Serial.begin(115200);
pinMode(RED_PIN, OUTPUT); // each light needs its own pinMode
pinMode(YELLOW_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
}
void loop() {
flash(RED_PIN, "RED");
flash(YELLOW_PIN, "YELLOW");
flash(GREEN_PIN, "GREEN");
// All three: the board allows it, a real signal never shows it.
digitalWrite(RED_PIN, HIGH);
digitalWrite(YELLOW_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
Serial.println("all three");
delay(1000);
digitalWrite(RED_PIN, LOW);
digitalWrite(YELLOW_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
Serial.println("off");
delay(1000);
}The numbers are the GPIO numbers your board prints beside its pins: 9 is D9 on an Uno, 4 is GPIO 4 on an ESP32-S3. Open the serial monitor at 115200. If the light and the name ever disagree, fix the wiring before going further.
The same lamp test in MicroPython, for an ESP32, an ESP32-S3 or a Pico. Pin.OUT makes each pin an output, and value(1) and value(0) set it HIGH and LOW.
"""
Traffic Light - lamp test, MicroPython TK03 / /p/tk03
Wiring. Count from the square pad on the TinkerBlock board, LEDs up,
header at the bottom:
GND -> GND
NC -> nothing (both NC pins are unconnected on the board)
RED -> GPIO 25 on an ESP32, GPIO 4 on an ESP32-S3, GP13 on a Pico
YELLOW -> GPIO 26 on an ESP32, GPIO 5 on an ESP32-S3, GP14 on a Pico
GREEN -> GPIO 27 on an ESP32, GPIO 6 on an ESP32-S3, GP15 on a Pico
Thonny
Run > Configure interpreter MicroPython (ESP32) or
MicroPython (Raspberry Pi Pico)
Save it to the board as main.py to run it on every power-up.
Nothing to install: machine and time are built in.
"""
from machine import Pin
import time
# GPIO numbers. ESP32: 25, 26, 27. ESP32-S3: 4, 5, 6. Pico: 13, 14, 15.
RED_PIN = 4
YELLOW_PIN = 5
GREEN_PIN = 6
red = Pin(RED_PIN, Pin.OUT, value=0) # each light is its own output
yellow = Pin(YELLOW_PIN, Pin.OUT, value=0)
green = Pin(GREEN_PIN, Pin.OUT, value=0)
lights = [(red, "RED"), (yellow, "YELLOW"), (green, "GREEN")]
while True:
for pin, name in lights: # one at a time, and say which
pin.value(1)
print(name)
time.sleep_ms(1000)
pin.value(0)
for pin, _ in lights: # all three: allowed, never real
pin.value(1)
print("all three")
time.sleep_ms(1000)
for pin, _ in lights:
pin.value(0)
print("off")
time.sleep_ms(1000)There is no Uno here: an Uno cannot run MicroPython. The pin numbers are GPIO numbers, as in the Arduino sketch. Stop it with Ctrl-C in Thonny's shell, and the lights stay in whatever state they were in.
When it does not work
Two signal wires are swapped, or the pin numbers in the sketch are in a different order from the wires. Move the wires to match the sketch rather than the other way round, so every sketch in the book works without editing.
The three signal wires are one pin over. Shifted towards GND by one, the red wire lands on an NC pin, which goes nowhere, and the other two light the colour next door. Find the square pad, put GND on it, and count again.
Set Tools > USB CDC On Boot to Enabled and upload again. Without it the S3's USB port does not bring up a serial port at boot, so the sketch runs with nowhere to print. The lights stepping tells you the sketch itself is fine.
Any three digital pins will work. These are consecutive, they can all do PWM if you later want to dim a light, and none of them is a pin the board reads at boot or uses for its flash memory. Using the same three everywhere means the sketches need no editing.
The green draws less current than the other two and still looks as bright.
Why the green is different →Edit this page — content/books/traffic-light/the-lamp-test.mdx
Questions about this product
See what other owners have asked, and read their solutions.
Traffic Light
Loading discussions…
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.