Arduino Project

LED control 1.0

Course Introduction

In this lesson, you will use Arduino together with two buttons and a row of LEDs to create a directional light-flow effect.

Pressing the green button makes the LEDs light up in a forward flowing pattern, while pressing the red button triggers a backward flow.

Each button controls the direction of the LED animation, allowing you to switch between the two lighting modes at any time.

Note

If this is your first time working with an Arduino project, we recommend downloading and reviewing the basic materials first.

1.1 Install Arduino IDE(Important)
1.2 Introduction of Arduino IDE

Required Components

In this project, we need the following components:

SN

COMPONENT INTRODUCTION

QUANTITY

PURCHASE LINK

1

Arduino UNO R4 Minima

1

2

USB Type-C cable

1

×

3

Breadboard

1

BUY

4

Wires

Several

5

1kΩ resistor

Several

6

Button

2

7

LED

Several

Wiring

8.png__PID:919c812f-123f-4689-b297-05ad92627388

Common Connections:

LED
Connect the LEDs cathode to the negative power bus on the breadboard, and the LEDs anode to 1kΩ resistor then to 5 to 10 on the Arduino.

Button
Connect to breadboard’s negative power bus. Connect to 3 , 12 on the Arduino.

Writing the Code

Note
You can copy this code into Arduino IDE.
Don’t forget to select the board(Arduino UNO R4 Minima) and the correct port before clicking the Upload button.


// Pins for buttons
const int buttonGreen = 3;   // Green button = forward
const int buttonRed   = 12;  // Red button = backward

// LED pins from 5 to 10
const int ledPins[] = {5, 6, 7, 8, 9, 10};
const int ledCount = 6;

int delayTime = 80;   // Speed of flowing

void setup() {
  // LEDs as output
  for (int i = 0; i < ledCount; i++) {
    pinMode(ledPins[i], OUTPUT);
  }

  // Buttons use internal pull-up
  pinMode(buttonGreen, INPUT_PULLUP);
  pinMode(buttonRed, INPUT_PULLUP);
}

void loop() {

  // Read buttons (LOW = pressed)
  bool greenPressed = (digitalRead(buttonGreen) == LOW);
  bool redPressed   = (digitalRead(buttonRed) == LOW);

  // ----- Forward Flow: 5 → 10 -----
  if (greenPressed) {
    for (int i = 0; i < ledCount; i++) {
      digitalWrite(ledPins[i], HIGH);   // Light current LED
      delay(delayTime);

      if (i > 0) {
        digitalWrite(ledPins[i - 1], LOW); // Turn off previous LED
      }
    }
    digitalWrite(ledPins[ledCount - 1], LOW); // Turn off last LED
  }

  // ----- Backward Flow: 10 → 5 -----
  if (redPressed) {
    for (int i = ledCount - 1; i >= 0; i--) {
      digitalWrite(ledPins[i], HIGH);   // Light current LED
      delay(delayTime);

      if (i < ledCount - 1) {
        digitalWrite(ledPins[i + 1], LOW); // Turn off previous LED
      }
    }
    digitalWrite(ledPins[0], LOW); // Turn off last LED
  }
}