/ arduino

Arduino Traffic Light

This traffic light is based on the first example. The same resistor + LED is replicated three times and attached to the different pins.


The very common logic for traffic light is "STOP" for some time, then "CAUTION" for shorter time, and then "GO" for same time as "STOP". There are some regional differences though, so please feel free to adapt it to your location an send me the code.

#define LED_RED 13
#define LED_YELLOW 12
#define LED_GREEN 11
#define TIME_GO 4000
#define TIME_CAUTION 1000
#define TIME_STOP 4000

void setup() {
  pinMode(LED_RED, OUTPUT);

void loop() {
  // start with red / stop
  digitalWrite(LED_RED, HIGH);
  digitalWrite(LED_RED, LOW);
  // yellow / caution
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_YELLOW, LOW);
  // you can go / green
  digitalWrite(LED_GREEN, HIGH);
  digitalWrite(LED_GREEN, LOW);
  // and now back you yellow
  digitalWrite(LED_YELLOW, HIGH);
  digitalWrite(LED_YELLOW, LOW);
  // and back to red on the next iteration
Alex Netkachov

Alex Netkachov

Alex likes functional programming, algorithms and code reviews. Apart from programming, his favourites are walking with his family in the parks and national trails and reading books.

Read More

Why not to stay updated if the subject is interesting? Join Telegram channel Alex@Net or follow alex_at_net on Twitter. Or just, use the comments form below.