traffic light/Lighting it/03. The lamp test
Lighting it · 03 of 8

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

Four wires
Your board
Wires
4
RED, YELLOW, GREEN
4, 5, 6
Logic
3.3 V
GND to GND, then RED, YELLOW and GREEN to GPIO 4, GPIO 5, GPIO 6. None of them is a pin the ESP32-S3 reads at boot or uses for flash, so the block cannot stop the board starting. There is no supply pin: each signal pin powers its own LED.

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
off

The 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.ino
/*
  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.

When it does not work

The monitor says RED and the yellow lights.

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.

Every colour is one step out, and one stays dark.

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.

The serial monitor stays empty on my ESP32-S3.

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.

Why these pins and not the ones another tutorial used?

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.

Where this goes next

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

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

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.

Browse Modules and blocks on the forum