Traffic Signals

The Arduino code that you can use to build a scale model two road intersection with traffic lights and pedestrian crossing warning signs..

There are six traffic signals in the code.  Two are for overhead signals and four are street level mounted.

How To Make Traffic And Pedestrian Warning Signals With Arduino
How To Make Traffic And Pedestrian Warning Signals With Arduino

You can use this code in different ways.  If you wish to have only two traffic signals and a pedestrian crossing, load the entire code but hook up only those LEDs that you need.

Materials Required For Arduino Traffic Signals

LEDS –  Size depends on scale.  For HO and OO scales, 3mm are appropriate.  Colored SMD Leds may be best for TT and N gauge.

Magnet wire – Use a very, very fine wire so that you can run it through the mounting pole.

You will need at least an Uno.  You may, of course, use a mega.

 

 

The Arduino Sketch For Traffic Signals With Pedestrian Crossing Lights.

You can set the timing of the lights by changing the number of milliseconds in these code lines. I have them set at a rather fast pace so I don’t waste time as I go through the installation and checkout process.

Copying from a web page results in Unicode characters being used.  Arduino IDE requires ASCI.     Copy this, paste it in Notepad and save as a text file. That results in ASCI. That text can be added to the IDE and then save that as an ino file.

 

[code]
//intersection 4 street level signals plus 4 pedestrian crossing lights flash 7 times
// by Gary Granai 1 June, 2021
// www.steamtraininfo.com
// free to use as long as these credits remain as they are.

//variables

int redDelay = 5000;
int yellowDelay = 450;
int maingreenDelay=7000;
int crossinggreenDelay=5000;

void setup() {

//main road lights
pinMode(13, OUTPUT); //main road red
pinMode(12, OUTPUT); //main road yellow
pinMode(11, OUTPUT); //main road green

pinMode(4, OUTPUT); //main road red
pinMode(3, OUTPUT); //main road yellow
pinMode(2, OUTPUT); //main road green
pinMode(22, OUTPUT); //main road pedestrian light red
pinMode(24, OUTPUT); //main road pedestrian light green
pinMode(34, OUTPUT); //main road pedestrian light red
pinMode(36, OUTPUT); //main road pedestrian light green

//crossing road lights
pinMode(10, OUTPUT); //crossing road red
pinMode(9, OUTPUT); //crossing road yellow
pinMode(8, OUTPUT); //crossing road green

pinMode(7, OUTPUT); //crossing road red
pinMode(6, OUTPUT); //crossing road yellow
pinMode(5, OUTPUT); //crossing road green

pinMode(26, OUTPUT); //crossing road pedestrian light red
pinMode(28, OUTPUT); //crossing road pedestrian light green
pinMode(30, OUTPUT); //crossing road pedestrian light red
pinMode(32, OUTPUT); //crossing road pedestrian light green

}

void loop() {

//image 1 main road traffic, cross road stopped

digitalWrite(11, HIGH); //main road green on
digitalWrite(13, LOW); //main road red off
digitalWrite(10, HIGH); //crossing road red on
digitalWrite(4, LOW); //main road red off
digitalWrite(7, HIGH); //cross road red on
digitalWrite(2, HIGH); //main road green on

digitalWrite(24, LOW); //main road pedestrian green light off
digitalWrite(22, HIGH); //main road pedestrian red light on
digitalWrite(34, LOW); //main road pedestrian green light off
digitalWrite(36, HIGH); //main road pedestrian red light on

digitalWrite(28, HIGH); //crossing pedestrian green light on
digitalWrite(26, LOW); //cross pedestrian red light off
digitalWrite(30, HIGH); //crossing pedestrian green light on
digitalWrite(32, LOW); //cross pedestrian red light off

delay(maingreenDelay);

//Image 2 Main road yellow goes on. Flashing of pedestrian crossing.

//begin for function

{
for(int i = 0; i < 7; i++){
// Flash sequence 1

digitalWrite(10, HIGH); //crossing road red on
digitalWrite(11, LOW); //main road green off
digitalWrite(12, HIGH); //main road yellow on

digitalWrite(7, HIGH); //crossing road red on
digitalWrite(2, LOW); //main road green off
digitalWrite(3, HIGH); //main road yellow on

digitalWrite(24, LOW); //main pedestrian green off
digitalWrite(22, HIGH); //main pedestrian red on
digitalWrite(28, HIGH); //crossing road pedestrian road green light on
digitalWrite(26, LOW); //crossing road pedestrian red light off

digitalWrite(34, LOW); //main pedestrian green off
digitalWrite(36, HIGH); //main pedestrian red on
digitalWrite(30, HIGH); //crossing road pedestrian road green light on
digitalWrite(32, LOW); //crossing road pedestrian red light off

delay(yellowDelay);

// Flash sequence 2

digitalWrite(10, HIGH); //crossing road red on
digitalWrite(11, LOW); //main road green off
digitalWrite(12, HIGH); //main road yellow on

digitalWrite(7, HIGH); //crossing road red on
digitalWrite(2, LOW); //main road green off
digitalWrite(3, HIGH); //main road yellow on

digitalWrite(24, LOW); //main pedestrian green off
digitalWrite(22, HIGH); //main pedestrian red on
digitalWrite(28, LOW); //crossing road pedestrian road green light off
digitalWrite(26, LOW); //crossing road pedestrian red light off

digitalWrite(30, LOW); //crossing road pedestrian road green light off
digitalWrite(32, LOW); //crossing road pedestrian red light off
digitalWrite(34, LOW); //main pedestrian green off
digitalWrite(36, HIGH); //main pedestrian red on

delay(yellowDelay);

}
//end for function

//image 3 All traffic stopped

digitalWrite(13, HIGH); //main road red on
digitalWrite(10, HIGH); //crossing road red on
digitalWrite(12, LOW); //main road yellow off

digitalWrite(4, HIGH); //main road red on
digitalWrite(7, HIGH); //crossing road red on
digitalWrite(3, LOW); //main road yellow off

digitalWrite(24, LOW); //main pedestrian green off
digitalWrite(22, HIGH); //main pedestrian red on
digitalWrite(28, LOW); //crossing road pedestrian road green light off
digitalWrite(26, HIGH); //crossing road pedestrian red light on

digitalWrite(34, LOW); //main pedestrian green off
digitalWrite(36, HIGH); //main pedestrian red on
digitalWrite(30, LOW); //crossing road pedestrian road green light off
digitalWrite(32, HIGH); //crossing road pedestrian red light on

delay(redDelay);

 

 

// change travel lanes

// Image 4 traffic starts on crossing road

digitalWrite(13, HIGH); //main road red on
digitalWrite(8, HIGH); //crossing road green on
digitalWrite(10, LOW); //crossing road red off

digitalWrite(4, HIGH); //main road red on
digitalWrite(5, HIGH); //crossing road green on
digitalWrite(7, LOW); //crossing road red off

digitalWrite(24, HIGH); //main pedestrian green light on
digitalWrite(22, LOW); //main pedestrian red light off
digitalWrite(28, LOW); //crossing road pedestrian green light off
digitalWrite(26, HIGH); //crossing road pedestrian red light on

digitalWrite(34, HIGH); //main pedestrian green light on
digitalWrite(36, LOW); //main pedestrian red light off
digitalWrite(30, LOW); //crossing road pedestrian green light off
digitalWrite(32, HIGH); //crossing road pedestrian red light on

delay(crossinggreenDelay);

//image 5 Crossing road yellow goes on. pedestrin green starts flashing

//begin for function

{
for(int i = 0; i < 7; i++){
//Crossing Road Flash Sequence 1

digitalWrite(13, HIGH); //main road red on
digitalWrite(8, LOW); //crossing road green off
digitalWrite(9, HIGH); //crossing road yellow on

digitalWrite(4, HIGH); //main road red on
digitalWrite(5, LOW); //crossing road green off
digitalWrite(6, HIGH); //crossing road yellow on

digitalWrite(24, HIGH); //main pedestrian green light on
digitalWrite(22, LOW); //main pedestrian red light off
digitalWrite(28, LOW); //crossing road pedestrian green light off
digitalWrite(26, HIGH); //crossing road pedestrian red light on

digitalWrite(34, HIGH); //main pedestrian green light on
digitalWrite(36, LOW); //main pedestrian red light off
digitalWrite(30, LOW); //crossing road pedestrian green light off
digitalWrite(32, HIGH); //crossing road pedestrian red light on

delay(yellowDelay);

//Crossing Road Flash Sequence 2

digitalWrite(13, HIGH); //main road red on
digitalWrite(8, LOW); //crossing road green off
digitalWrite(9, HIGH); //crossing road yellow on

digitalWrite(4, HIGH); //main road red on
digitalWrite(5, LOW); //crossing road green off
digitalWrite(6, HIGH); //crossing road yellow on

digitalWrite(24, LOW); //main pedestrian green light off
digitalWrite(22, LOW); //main pedestrian red light off
digitalWrite(28, LOW); //crossing road pedestrian green light off
digitalWrite(26, HIGH); //crossing road pedestrian red light on

digitalWrite(34, LOW); //main pedestrian green light off
digitalWrite(36, LOW); //main pedestrian red light off
digitalWrite(30, LOW); //crossing road pedestrian green light off
digitalWrite(32, HIGH); //crossing road pedestrian red light on

delay(yellowDelay);

}
//end for function

//image 6 all traffic stopped

digitalWrite(13, HIGH); //main road red on
digitalWrite(10, HIGH); //crossing road red on
digitalWrite(9, LOW); //crossing road yellow off

digitalWrite(4, HIGH); //main road red on
digitalWrite(7, HIGH); //crossing road red on
digitalWrite(6, LOW); //crossing yellow off

digitalWrite(24, LOW); //main pedestrian green light off
digitalWrite(22, HIGH); //main pedestrian red light on
digitalWrite(28, LOW); //crossing road pedestrian green light off
digitalWrite(26, HIGH); //crossing road pedestrian red light on

digitalWrite(34, LOW); //main pedestrian green light off
digitalWrite(36, HIGH); //main pedestrian red light on
digitalWrite(30, LOW); //crossing road pedestrian green light off
digitalWrite(32, HIGH); //crossing road pedestrian red light on

delay(redDelay);

}
}
}

[/code]

 

 

 

Wiring Instructions

The wiring is simple but rather fiddly if you do not give yourself roon on your circuit  board.

The positive side of all LEDs go to these respective numbers on your circuit board.

main road lights
main road red – pin 13
main road yellow – pin 12
main road green – pin 11

main road red – pin 4
main road yellow – pin 3
main road green – pin 2
main road pedestrian light red – pin 22
main road pedestrian light green – pin 24
main road pedestrian light red – pin 34
main road pedestrian light green – pin 36

crossing road lights
crossing road red- pin 10
crossing road yellow -pin 9
crossing road green – pin 8

crossing road red – pin 7
crossing road yellow – pin 6
crossing road green  – pin 5

crossing road pedestrian light red – pin 26
rossing road pedestrian light green – pin 28
crossing road pedestrian light red – pin 30
crossing road pedestrian light green – pin 32

 

All LEDs require a 200 ohm resistor on either the cathod or anond side.

All anodes are connected to a common ground which is connected to the Ardino ground.

 

Video Demonstration

 

Traffic And Pedestrian Signals With Arduino
Click on image to watch video

This video shows you how the code works and what you can do with it.

 

 

 

 

 

 

This tutorial is part of our series Arduino Projects. There is a video playlist at Playlist

 

Tell others about this information by using one of these services. Thank you for the support.
Scroll to top