Blue Pill traffic light with a crossing button
Build it step by step
0 of 7 doneWire the programmer
SWDIO, SWCLK, GND and 3.3V.
Wire the car lights
PA1, PA2 and PA3 each through 330 ohms.
Wire the walk lights
PA4 and PA5 each through 330 ohms.
Wire the button
PA0 to GND.
Upload the sketch
Car green and wait lights come on.
Press the button
After the minimum green, cars change to amber then red.
Watch the walk phase
Walk lights, then flashing wait, then cars restart.
How do you build a blue Pill traffic light with a crossing button?
A Blue Pill runs a model traffic light and a pedestrian button that asks for a safe change, with a walk and wait pair of LEDs. You need STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), LED 5 mm red × 5, Resistor 330 Ω × 5, Tactile push button 6 mm and Breadboard 830 Tie Points. It takes 7 steps and about 140 minutes.
- Level
- Intermediate
- Time
- About 140 min
- Parts
- 6
Parts list
- Development BoardsBuy
- InterfacesBuy
- SemiconductorsBuyLED 5 mm red × 5
- PassivesBuyResistor 330 Ω × 5
- SwitchesBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| ST-Link GND | Board GND | Ground |
| ST-Link SWDIO | Board DIO | Debug data |
| ST-Link SWCLK | Board DCLK | Debug clock |
| ST-Link 3.3V | Board 3.3 pin | Power from the programmer (do not also plug in USB) |
| Car red LED | 330 ohm resistor to Blue Pill PA1 | Use a red LED |
| Car amber LED | 330 ohm resistor to Blue Pill PA2 | Use a yellow LED |
| Car green LED | 330 ohm resistor to Blue Pill PA3 | Use a green LED |
| Walk LED | 330 ohm resistor to Blue Pill PA4 | Use a green LED |
| Wait LED | 330 ohm resistor to Blue Pill PA5 | Use a red LED |
| All LED cathodes | Blue Pill GND | Return |
| Crossing button | Blue Pill PA0 and GND | Request to cross |
Code
A traffic light is a classic state machine: the lights are always in one of a few states, each lasts for a set time, and a rule decides the next state. The sketch keeps the state in one variable and checks how long it has been there using the millisecond clock, so nothing blocks and the button is always seen. A press only sets a flag, and the cars keep their green for a minimum time before the change begins, so the light cannot be flipped over and over. After amber there is a short all-red gap before the walk light, and another before the cars restart, which is how real crossings leave time for slow walkers. This is a teaching model for a desk, not a real signal, and real crossings follow strict national rules. Use the right LED colours if you have them, with one 330 ohm resistor per LED.
Runs on a virtual STM32 Blue Pill in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
const int CAR_R = PA1, CAR_A = PA2, CAR_G = PA3, WALK = PA4, WAIT = PA5, BTN = PA0;
enum State { CAR_GREEN, CAR_AMBER, ALL_RED_1, WALK_PHASE, WALK_FLASH, ALL_RED_2, CAR_RED_AMBER };
State state = CAR_GREEN;
unsigned long stateStart = 0;
bool requested = false;
const unsigned long MIN_GREEN = 6000; // cars keep green at least this long after a request
const unsigned long AMBER = 2000, ALL_RED = 1000, WALK_TIME = 6000, FLASH_TIME = 3000, RED_AMBER = 1500;
void setLights(bool cr, bool ca, bool cg, bool walk, bool wait) {
digitalWrite(CAR_R, cr); digitalWrite(CAR_A, ca); digitalWrite(CAR_G, cg);
digitalWrite(WALK, walk); digitalWrite(WAIT, wait);
}
void go(State s) { state = s; stateStart = millis(); }
void setup() {
int outs[] = {CAR_R, CAR_A, CAR_G, WALK, WAIT};
for (int i = 0; i < 5; i++) pinMode(outs[i], OUTPUT);
pinMode(BTN, INPUT_PULLUP);
go(CAR_GREEN);
}
void loop() {
if (digitalRead(BTN) == LOW) requested = true;
unsigned long t = millis() - stateStart;
switch (state) {
case CAR_GREEN:
setLights(0, 0, 1, 0, 1);
if (requested && t >= MIN_GREEN) go(CAR_AMBER);
break;
case CAR_AMBER:
setLights(0, 1, 0, 0, 1);
if (t >= AMBER) go(ALL_RED_1);
break;
case ALL_RED_1:
setLights(1, 0, 0, 0, 1);
if (t >= ALL_RED) go(WALK_PHASE);
break;
case WALK_PHASE:
setLights(1, 0, 0, 1, 0);
requested = false;
if (t >= WALK_TIME) go(WALK_FLASH);
break;
case WALK_FLASH:
setLights(1, 0, 0, 0, (t / 250) % 2); // wait LED flashes as the warning
if (t >= FLASH_TIME) go(ALL_RED_2);
break;
case ALL_RED_2:
setLights(1, 0, 0, 0, 1);
if (t >= ALL_RED) go(CAR_RED_AMBER);
break;
case CAR_RED_AMBER:
setLights(1, 1, 0, 0, 1);
if (t >= RED_AMBER) go(CAR_GREEN);
break;
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Idle | Car green and wait lit |
| Button pressed early | Change waits for the minimum green |
| Amber | Lasts two seconds |
| Walk phase | Cars red, walk light on |
| Warning | Wait LED flashes before cars restart |
If it does not work
LED never lights
Check polarity and the pin names.
Does not respond to the button
Check PA0 to GND.
Phases too fast
Raise the time constants at the top.
Two LEDs on together
Check each has its own pin.
Upload fails
Check the ST-Link wires and boot jumpers.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the blue Pill traffic light with a crossing button?
STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), LED 5 mm red × 5, Resistor 330 Ω × 5, Tactile push button 6 mm and Breadboard 830 Tie Points.
How long does the blue Pill traffic light with a crossing button take to build?
About 140 minutes for a intermediate maker, in 7 steps.
How do I know the blue Pill traffic light with a crossing button works?
Idle: Car green and wait lit. Button pressed early: Change waits for the minimum green. Amber: Lasts two seconds.
LED never lights: what should I check?
Check polarity and the pin names.
Does not respond to the button: what should I check?
Check PA0 to GND.
Phases too fast: what should I check?
Raise the time constants at the top.
Two LEDs on together: what should I check?
Check each has its own pin.
