Running led Arduino basic code using Loop

Posted on

Running 5 LED using Arduino Uno

Do you remember the first time you saw a flashing LED light on an electronic device? It felt like a magical gate to the world of technology was opening. In this article, we’ll dive deeper into the magic by exploring how to create a running LED Arduino Uno with basic code.

What is Running LED?

Running LED is an electronics project in which a series of LEDs are lit in sequence, creating an interesting visual effect. This is one of the most popular Arduino projects for beginners, as it is easy to understand and implement.

History and Meaning

LED technology has developed rapidly since its discovery in 1962. Initially, LEDs were only available in red, but are now available in a variety of colors and shapes. Running LEDs have become a symbol of creativity and expression in the world of electronics, allowing enthusiasts to create various beautiful patterns and animations.

What will we do?

in this article, we will try to turn on 5 LEDs alternately so that it looks like the LEDs are running.

Draft:

  • LED 1 is turned on, apart from this LED, everything is turned off.
  • The 2nd LED is on, other than that all are turned off.
  • The 3rd LED lights up, another turns off
  • and so on with a delay of 1 second, the switch on and off will cycle.

In the project, I will create a loop to turn on the LED with 4 methods:

  1. Using the manual method means we write the code entirely manually and there will be a default loop by the Arduino, without using the while, do-while, and for functions.
  2. While method
  3. Do-While Loop Method
  4. For Loop method

Wiring

However, you must use and arrange 5 LEDs, as in the following circuit:

Running led Arduino basic code using (While, Do-While & For) Loop

Program Code Using Manual Loop

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

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

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

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

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

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

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

Program Code Using Manual “While” Loop

int led = 8;
int led_lama;
int waktu_tunda = 200;

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

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

Program Code Using a “Do-While” Loop

int led = 8;
int led_lama;
int waktu_tunda = 200;

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

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

Program Code Using the “For” Loop

int led;
int led_lama;
int waktu_tunda = 200;

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

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

FAQ About Running LED

Q: What materials are needed to make Running LED?

A: Materials needed:

  • Arduino Uno
  • LED (quantity as needed)
  • Resistor (100-450 ohms for each LED)
  • Breadboard
  • Jumper cable

Q: What best resistance value for a LED?

A: You can get the formula in the Calculate LED Resistor article

Q: How does Running LED work?

A: Running LED works by turning on the LEDs one by one in sequence. This is achieved by using loops and conditional statements in the Arduino code.

Q: What are the types of Running LEDs?

A: There are various types of Running LEDs, including:

  • Running LED is simple: LEDs are turned on one by one in sequence.
  • Running LED with effects: LEDs are lit with a certain pattern or animation.
  • Running LED with sensors: LEDs are turned on based on input from sensors, such as light sensors or motion sensors.

Tips

  • Use a breadboard to assemble your LED Running project.
  • Make sure to use a resistor to protect the LED from damage.
  • Use code that is easy to understand and comment.
  • Test your code carefully before connecting it to the LED.
  • Start with a simple LED Running project.
  • Experiment with different patterns and animations.
  • Use sensors to create interactive Running LEDs.
  • Join online communities for inspiration and help.

Other sources for your support of your Arduino projects

Many news sources and online platforms provide information and tutorials about Running LED. Some popular platforms include:

  • Arduino Project Hub: Menyediakan berbagai proyek Running LED dengan panduan dan kode yang mudah diikuti.
  • URL Hackster.io: Platform komunitas bagi para peminat elektronik untuk berbagi proyek dan ide, termasuk Running LED.
  • YouTube: Berbagai video tutorial tentang Running LED dengan berbagai tingkat kesulitan.