Skip to content
Advertisements

Arduino LED blink, fade and “Traffic lights” project

Hi everyone in this tutorial i’m gonna show you how to do a simple LED blink and fade with an Arduino board, I’m also showing how to create a simple traffic lights project.

 

Codes:

Code 1:  LED Blink Code

//Simple LED blinking program

//SurtrTech Youtube Channel

const int L1 = 2; //Declaring LED pin

void setup()

{

pinMode(L1, OUTPUT); //Setting pin mode

}

void loop()

{

digitalWrite(L1, HIGH); //Turn on led

delay(1000); // On time in miliseconds

digitalWrite(L1, LOW); //Turn off led

delay(1000); //Off time

}

Code 2: LED fading

//Simple LED blinking program

//SurtrTech Youtube Channel

const int L1 = 3;  // Led pin (must use a pwm output)

void setup() {




  pinMode(L1,OUTPUT);

}

void loop() {




  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) { // A loop that goes from 0 to 255 which is 0V to 5V in an pwm output (3)

   analogWrite(L1, fadeValue); // The led receive the value from 0 to 255

   delay(100); //Time in miliseconds

  }




  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) { // Same loop but from highest value to lowest value

  analogWrite(L1, fadeValue); // The led receive the value from 255 to 0

   delay(100);

  }

}

Code 3: Traffic lights

//Traffic lights using 6 LEDs

//SurtrTech Youtube Channel

const int L1 = 2; // Declaring 6 LEDs output 2,3 and for are for the light1 and

const int L2 = 3; // 5,6 and 7 are for the light 2

const int L3 = 4;

const int L4 = 5;

const int L5 = 6;

const int L6 = 7;

void setup()

{

pinMode(L1, OUTPUT);  //Red 1

pinMode(L2, OUTPUT);  //Orange 1

pinMode(L3, OUTPUT);  //Green 1

pinMode(L4, OUTPUT);  //Red 2

pinMode(L5, OUTPUT);  //Orange 2

pinMode(L6, OUTPUT);  //Green 2

}

void loop()

{

digitalWrite(L1, HIGH); // You chose which two you want to turn on as you like, and you must turn off the others

digitalWrite(L6, HIGH); // And you repeat

digitalWrite(L2, LOW);

digitalWrite(L5, LOW);

digitalWrite(L3, LOW);

digitalWrite(L4, LOW);

delay(4000); // Time in miliseconds

digitalWrite(L1, HIGH);

digitalWrite(L6, LOW);

digitalWrite(L2, LOW);

digitalWrite(L5, HIGH);

digitalWrite(L3, LOW);

digitalWrite(L4, LOW);

delay(2000);

 digitalWrite(L1, HIGH);

digitalWrite(L6, LOW);

digitalWrite(L2, LOW);

digitalWrite(L5, LOW);

digitalWrite(L3, LOW);

digitalWrite(L4, HIGH);

delay(1000);

digitalWrite(L1, LOW);

digitalWrite(L6, LOW);

digitalWrite(L2, LOW);

digitalWrite(L5, LOW);

digitalWrite(L3, HIGH);

digitalWrite(L4, HIGH);

delay(4000);

digitalWrite(L1, LOW);

digitalWrite(L6, LOW);

digitalWrite(L2, HIGH);

digitalWrite(L5, LOW);

digitalWrite(L3, LOW);

digitalWrite(L4, HIGH);

delay(2000);

digitalWrite(L1, HIGH);

digitalWrite(L6, LOW);

digitalWrite(L2, LOW);

digitalWrite(L5, LOW);

digitalWrite(L3, LOW);

digitalWrite(L4, HIGH);

delay(1000);

}
Advertisements

Yassine View All

Electronics, Automation engineering student... I try to share my little experience with other electronics amateur.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: