Skip to content
03 30 minutes

Traffic lights

Three LEDs, three pins, one shared ground, and a function that keeps order

Traffic lights project
Illustration of the finished idea. Build from the diagram and connection table, not the photo.

Mission goal: Drive red, yellow and green LEDs from GP16, GP17 and GP18, each with its own resistor, and run a believable traffic light sequence using a helper function.

Section 1

Understand

New ideas

  • Several circuits sharing one ground
  • Writing a function to avoid repeating yourself
  • Turning everything off before turning one thing on
  • Why each LED needs its own resistor
  • 1Three LEDs, three pins, each with its own resistor.
  • 2All grounds meet at one shared ground (pin 38).
  • 3One function turns everything off, then lights only the chosen colour.

Safety first

  • Unplug the USB cable while you add the two extra LEDs.
  • Count the physical pins twice. GP18 is physical 24, and physical 23 next to it is ground.
  • Three LEDs means three chances to fit an LED backwards. Check all three long legs before powering up.