# Traffic Signal

## What will we do

This example uses three LEDs to make a model traffic light signal. You can use all red LEDs if you like, but its more realistic if you use red, yellow and green.

The LEDs cycle around in the sequence red, green, yellow and then back to red again

{% hint style="success" %}
Please ensure that you have mBlock setup on you computer. Click [here](../../setting-up-mblock) to setup mBlock
{% endhint %}

## Parts List

You will need the following parts:

* **1x** arduino uno
* **1x**  arduino uno USB Cable
* **1x** Breadboard
* **4x** Jumper Wire
* **1x** Red LED&#x20;
* **1x** Yellow LED&#x20;
* **1x** Green LED&#x20;
* **3x** Resistors

You purchased your Arduino kit from [here](https://www.robofactory.co.za/arduino-kits/66-arduino-uno-r3-basic-starter-kit.html)

{% embed url="https://www.robofactory.co.za/arduino-kits/66-arduino-uno-r3-basic-starter-kit.html" %}

## Wiring it up

To add the extra LEDs and resistors, wire them up as shown in the breadboard layout below.

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-M6H9rHABIe5CWjXSbTZ%2F-M6K-wdOruxOMdb9XH89%2Farduino%20-%20traffic%20lights.png?alt=media\&token=c56fa9de-6907-483a-b542-456d5e855812)

Ensure that the LEDs are the right way around, with the longer positive leads (anodes) to the left.

## How does it work

Think of how a traffic light works.

* it starts from RED
* then turns to GREEN
* then turns YELLOW
* and back to RED

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-M6KLy8-3oUWa3-92S7q%2F-M6LMH_ZFSbZht4Bqiz2%2Fpower1.png?alt=media\&token=94406626-8015-4650-9037-ae1394c96c15)

Think about the how long the light stays on before it changes to the next colour

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-M6KLy8-3oUWa3-92S7q%2F-M6LMplfuoEatb7x9yrU%2Fpower2.png?alt=media\&token=8db1fcc5-a85c-49e3-a942-90475ec44b18)

This time that it takes before the light changes is called a WAIT in coding. So the program will **WAIT** for a specific number of seconds before the next light changes to the next colour

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-M6KLy8-3oUWa3-92S7q%2F-M6LNKe6UnKLPMT7CmnY%2Fpower3.png?alt=media\&token=2535e64d-7703-4409-9c7f-0124a9d04491)

In order to turn the light ON or OFF in code, we say that if we want the light on then we need to set it to HIGH and if we want the light OFF we setup it to LOW.

So to change the traffic lights we say

* to make the light ON, we set it to HIGH
* to make the light OFF, we set it to LOW

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-M6LNug-fXp3ELa2TqD3%2F-M6LNyHlKS2nBCXMb_6q%2Fpower4.png?alt=media\&token=7d14ff6e-e7f2-4e79-a0be-c3c0d8c11636)

If you look at the wiring diagram above, you will see that we connected it as follows

* we connected **DIGITAL PIN 7** **(D7)** to the **RED** light
* we connect **DIGITAL PIN 6 (D6)** to the **YELLOW** light
* we connect **DIGITAL PIN 5 (D5)** to the **GREEN** light

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-M6LO-6q6GR9IgAvFCkZ%2F-M6LOWuUVYq4b0Gk5g-y%2Fpower5.png?alt=media\&token=8088e2eb-a34c-4514-af62-46a18997ba8c)

So In order for the program to work we will do the following

* set **D7** to **HIGH** (red light)
* set **D6** to **LOW** (yellow light)
* set **D5** to **LOW** (green light)
* _**WAIT** for 10 seconds_
* set **D7** to **LOW** (red light)
* set **D6** to **LOW** (yellow light)
* set **D5** to **HIGH** (green light)
* _**WAIT** for 10 seconds_
* set **D7** to **LOW** (red light)
* set **D6** to **HIGH** (yellow light)
* set **D5** to **LOW** (green light)
* _**WAIT** for 1 seconds_

## Block Coding <a href="#block-coding" id="block-coding"></a>

![](https://1854716099-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LskXJjPT2bXMIodrtZq%2F-M8uIx29lznRqBVCJXZ8%2F-M8uKqqdG1OQFmdAcfUd%2Ftraffic%20light%20-%20code.png?alt=media\&token=92fcb668-4723-49b7-be81-5df5eba1ede8)

{% embed url="https://ide.mblock.cc/#/?cloudProjectId=262563" %}







