Arduino Running LED How To Make It?

Posted on

Running LED Using Arduino Uno

How to Make an Arduino Running LED – LED is a basic component in Arduino learning. Almost all electronic equipment uses LEDs.

Such as TV, Radio, SmartPone, Hard Disk, Mouse, Keyboard, Router, washing machine, and many more.

Not only on electronic devices, the use of LEDs is also in automotive such as spotlights on cars.

If you are a beginner, you should learn how to use LEDs. This article will guide you on how to control LEDs using Arduino

Turning LEDs alternately using Arduino is basic learning. For those of you who are just getting started with Arduino and are new to the program, this is the perfect tutorial.

Here we will try to turn on 5 LEDs in turn. So it will look like the led is running

At first LED 1 is on, otherwise it is off. Second, the 2nd LED is on, otherwise it is off. Third, the 3rd LED is on, otherwise it is off and so on. With a delay of 1 second, the on and off switch will rotate.

In this project I will create a loop to light the LED with 4 methods:

  • The first method is a manual method, meaning that we write the code entirely manually and there will be a default loop by arduino, without using the while, do-while and functions for functions.
  • While Loop method, more about When looping, click here.
  • The Do-While Loop method, more about the Do While loop, click here.
  • For Loop method, more about For loop, click here.

Wiring

You will use and set up 5 LEDs, as in the following image:

Arduino Running LED How To Make It?

Running LED Control Program

If You Wanto to Manual Loop Method

int led_1 = 8;
int led_2 = 9;
int led_3 = 10;
int led_4 = 11;
int led_5 = 12;
int timeDelay = 200;

void setup() {  
  for (int a = 7; a<13; a++){
    pinMode(a, OUTPUT);
  }
}

void loop() {
  // LED 1
  digitalWrite(led_1, HIGH);
  digitalWrite(led_2, LOW);
  digitalWrite(led_3, LOW);
  digitalWrite(led_4, LOW);
  digitalWrite(led_5, LOW);
  delay (timeDelay);

  // LED 2
  digitalWrite(led_1, LOW);
  digitalWrite(led_2, HIGH);
  digitalWrite(led_3, LOW);
  digitalWrite(led_4, LOW);
  digitalWrite(led_5, LOW);
  delay (timeDelay);

  // LED 3
  digitalWrite(led_1, LOW);
  digitalWrite(led_2, LOW);
  digitalWrite(led_3, HIGH);
  digitalWrite(led_4, LOW);
  digitalWrite(led_5, LOW);
  delay (timeDelay);

  // LED 4
  digitalWrite(led_1, LOW);
  digitalWrite(led_2, LOW);
  digitalWrite(led_3, LOW);
  digitalWrite(led_4, HIGH);
  digitalWrite(led_5, LOW);
  delay (timeDelay);

  // LED 5
  digitalWrite(led_1, LOW);
  digitalWrite(led_2, LOW);
  digitalWrite(led_3, LOW);
  digitalWrite(led_4, LOW);
  digitalWrite(led_5, HIGH);
  delay (timeDelay);
}

If you wanto “While” Loop Method

int led = 8;
int ledOld;
int timeDelay = 200;

void setup() {
  for (int a = 7; a < 13; a++) {
    pinMode(a, OUTPUT);
  }
}

void loop() {
  while (1) {
    digitalWrite(led, HIGH);
    ledOld = led;
    led++;
    delay(timeDelay);
    digitalWrite(ledOld, LOW);
    if (led == 13){
      led = 8;
    }
  }
}

If you want to use “Do-While” Loop Method

int led = 8;
int ledOld;
int timeDelay = 200;

void setup() {
  for (int a = 7; a < 13; a++) {
    pinMode(a, OUTPUT);
  }
}

void loop() {
  do {
    digitalWrite(led, HIGH);
    ledOld = led;
    led++;
    delay(timeDelay);
    digitalWrite(ledOld, LOW);
    if (led == 13){
      led = 8;
    }
  }
  while(1);
}

If you wanto to use “For” Loop Method

int led;
int ledOld;
int timeDelay = 200;

void setup() {
  for (int a = 7; a < 13; a++) {
    pinMode(a, OUTPUT);
  }
}

void loop() {
  for (led = 8; led < 13; led++) {
    digitalWrite(led, HIGH);
    ledOld = led;
    delay(timeDelay);
    digitalWrite(ledOld, LOW);
    if (led == 13) {
      led = 8;
    }
  }
}

In general, the programs are not much different, but the four programs above give you an idea of which method is simpler and easier to understand.

I hope this article is useful.

Leave a Reply

Your email address will not be published.