Traffic light - mcworkshop

Vai ai contenuti

Traffic light

The electrical connections

The Arduino output pins support a maximum current of 40 mA. For this reason I decided not to put the LEDs in parallel, but to dedicate a Pin for each of them. In image number 3, I gave each of the LEDs a name. You will use the name of each of the LEDs as the name of the variable contained in the Sketch. The calculation of the resistance must be made based on the color of the LED. LEDs do not all work with the same voltage. The maximum current, in theory, should not exceed 20 mA.
I pin di uscita Arduino supportano una corrente massima di 40 mA. Per questo motivo ho deciso di non mettere i LED in parallelo, ma di dedicare un Pin per ciascuno di essi.  Nell'immagine numero 3 ho dato un nome a ciascuno dei LED. Userai il nome di ciascuno dei LED come nome della variabile contenuta nello Sketch. Il calcolo della resistenza deve essere effettuato in base al colore del LED. I LED non funzionano tutti con la stessa tensione. La corrente massima, in teoria, non dovrebbe superare in 20 mA.

We will use the following instruction

int = converts a value to the int data type.
int var = val;
var - your int variable name
val - the value you assign to that variable
What name will we give to our variables? Six LEDs, six variables.

RL, YL, GL, RR, YR, GR. What do these names mean?
RL = Red Left = red left led

The first line we will write will be: int RL = 7;
7 is a number Pin.

pinMode(pin, mode)
pin: the number of the pin whose mode you wish to set
Our line will be: pinMode (RL, OUTPUT);

digitalWrite(pin, value)
pin: the pin number
value: HIGH or LOW
Our line will be: digitalWrite (RL, HIGH);
or                        digitalWrite (RL, LOW);

ms: the number of milliseconds to pause
Our line will be: delay (1000);

You can download the sketch here:

// Traffic Light
// September 2018. MC Workshop
// Open source. Have a good fun!
int RL = 7; // int + variable name + = + pin number + ;
int YL = 6;
int GL = 5;
int RR = 4;
int YR = 3;
int GR = 2;
void setup() {
 pinMode (RL, OUTPUT); // pinMode + ( + variable number + , + type pin + ) + ;
 pinMode (YL, OUTPUT);
 pinMode (GL, OUTPUT);
 pinMode (RR, OUTPUT);
 pinMode (YR, OUTPUT);
 pinMode (GR, OUTPUT);
// put all the low output pins. The tension will be zero.
void loop() {
 digitalWrite (RL, LOW); // digitalWrite + ( + variable name + , + pin high or low + ) + ;
 digitalWrite (YL, LOW);
 digitalWrite (GL, LOW);
 digitalWrite (RR, LOW);
 digitalWrite (YL, LOW);
 digitalWrite (GR, LOW);
 // let's start the traffic light
 //we turn on the two green LEDs
 digitalWrite (GL, HIGH);
 digitalWrite (GR, HIGH);
 //the LEDs remain on for 4 seconds
 delay (4000);
 //yellow LEDs on and green LEDs on
 digitalWrite (YL, HIGH);
 digitalWrite (YR, HIGH);
 delay (2000);
 digitalWrite (YL, LOW);
 digitalWrite (GL, LOW);
 digitalWrite (YR, LOW);
 digitalWrite (GR, LOW);
 digitalWrite (RL, HIGH);
 digitalWrite (RR, HIGH);
 delay (4000);

Video and pictures

Torna ai contenuti