Arduino LED blink, fade and “Traffic lights” project
Hello there,
This is an easy tutorial and project, it would be very nice to start your Arduino journey with it, it uses the simplest parts and functions to get you started.
In this project I’m using LEDs and Resistors with an Arduino board, UNO as usual. You’ll learn how to use LEDs for blinking and fading or simple light up, then as a project a traffic light.
Wiring
A simple LED is wired like below, you can add as many as you can, just declare the correct pin and don’t forget the LED poles (Anode and Cathode), anode = the longest “rod” is always with the (+) and the cathode with the GND.
For the traffic light project, read the code it will help you to set the LEDs pins, the GND is common and you can adjust the pins as you want.
Codes:
Code 1: LED Blink Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
//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); } |
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.