0% found this document useful (0 votes)
47 views3 pages

Try Traffic Light

This article discusses how to build an Arduino traffic light controller using an Arduino board. It explains how to connect LEDs to pins on the Arduino and write code to control the LEDs to mimic the behavior of a basic traffic light, cycling through green, yellow, and red lights with delays.

Uploaded by

Pratik Korkankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views3 pages

Try Traffic Light

This article discusses how to build an Arduino traffic light controller using an Arduino board. It explains how to connect LEDs to pins on the Arduino and write code to control the LEDs to mimic the behavior of a basic traffic light, cycling through green, yellow, and red lights with delays.

Uploaded by

Pratik Korkankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

https://www.makeuseof.

com/tag/arduino-traffic-light-controller/

1 Arduino Traffic Light: The Basics


Let’s start small. A basic, single traffic light is a good place to start. Here’s
the circuit:

Connect the anode (long leg) of each LED to digital pins eight, nine, and ten
(via a 220-ohm resistor). Connect the cathodes (short leg) to the Arduino’s
ground.

1.1.1 Code for the Arduino Traffic Light


Start by defining variables so that you can address the lights by name rather
than a number. Start a new Arduino project, and begin with these lines:

int red = 10;


int yellow = 9;
int green = 8;
Next, let’s add the setup function, where you’ll configure the red, yellow and
green LEDs to be outputs. Since you have created variables to represent the
pin numbers, you can now refer to the pins by name instead:

void setup(){
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
}

The pinMode function configures the Arduino to use a given pin as an


output. You have to do this for your LEDs to work at all. Now for the actual
logic of the traffic light. Here’s the code you need. Add this below your
variable definitions and setup function:

void loop(){
changeLights();
delay(15000);
}
void changeLights(){
// green off, yellow on for 3 seconds
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
delay(3000);
// turn off yellow, then turn red on for 5 seconds
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
delay(5000);
// red and yellow on for 2 seconds (red is already on though)
digitalWrite(yellow, HIGH);
delay(2000);
// turn off red and yellow, then turn on green
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
delay(3000);
}

Upload this code to your Arduino, and run (make sure to select the correct
board and port from the Tools > Board and Tools > Port menus). You
should have a working traffic light that changes every 15 seconds, like this
(sped up):

Check output:

Let’s break down this code. The changeLights function performs all the
hard work. This rotates the traffic light through yellow and red, then back to
green. As this gets called inside the loop function, the Arduino will run this
code forever, with a 15-second pause every time.

The changeLights function consists of four distinct steps:

 Green on, yellow of


 Yellow of, red on
 Yellow on, red on
 Green on, red of, yellow of

These four steps replicate the process used in real traffic lights. For each
step, the code is very similar. The appropriate LED gets turned on or of
using digitalWrite. This is an Arduino function used to set output pins to
HIGH (for on), or LOW (for of).

After enabling or disabling the required LEDs, the delay makes the Arduino
wait for a given amount of time. Three seconds in this case.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy