Traffic lightTinkerPieCircuit tutorial 15 of 24 · Intermediate · 20 minutes
Control three LEDs in a sequence, with one shared resistor, to build a traffic light.
Open this circuit in TinkerPieCircuit (free, runs in your browser)
What you will learn
- Controlling three outputs
- Sharing a resistor between LEDs
- Timing a sequence with delay
Parts
Steps
Add the LEDs
Add three LEDs and set their colours in the Part tab: red, yellow and green.
Wire the signals
Wire
S1to the red LED (+),S2to yellow (+) andS3to green (+).Share the resistor
Join the (−) legs of all three LEDs to one end of the 220 Ω resistor. Wire the other end to
GND. Only one LED is on at a time, so one resistor is enough.Write the sequence
Red for 2 seconds, then green for 2 seconds, then yellow for 0.8 seconds, then repeat.
Press Start
Watch the lights change in order.
The code
function setup() {
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
}
function loop() {
digitalWrite(S1, HIGH);
delay(2000);
digitalWrite(S1, LOW);
digitalWrite(S3, HIGH);
delay(2000);
digitalWrite(S3, LOW);
digitalWrite(S2, HIGH);
delay(800);
digitalWrite(S2, LOW);
}
Copy this into the Code tab. It is Arduino-style JavaScript. The real board uses the same pin names (S1, P10…) in TinkerPie Studio.
How it works
Each LED has its own pin, so the program chooses which one lights. The shared resistor protects whichever LED is on.
← Why LEDs need a resistor · All 24 circuit tutorials · Light alarm for a secret box →