LED control 1.0

Note

🌟 Welcome to the SunFounder Facebook Community! Whether you’re into Raspberry Pi, Arduino, or ESP32, you’ll find inspiration, help ideas here.

  • ✅ Be the first to get free learning resources.

  • ✅ Stay updated on new products & exclusive giveaways.

  • ✅ Share your creations and get real feedback.

  • 👉 Need faster updates or support? Click [here] join our Facebook community

  • 👉 Or join our WhatsApp group: Click [here]

Kit purchase

Looking for parts? Check out our all-in-one kits below — packed with components, beginner-friendly guides, and tons of fun.

../_images/elite_explore_kit.png

Name

Includes Arduino board

PURCHASE LINK

Ultimate Sensor Kit

Arduino Uno R4 Minima

BUY

Elite Explorer Kit

Arduino Uno R4 WiFi

BUY

3 in 1 Ultimate Starter Kit

Arduino Uno R4 Minima

BUY

Universal Maker Sensor Kit

×

BUY

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.

Required Components

In this project, we need the following components:

SN

COMPONENT INTRODUCTION

QUANTITY

PURCHASE LINK

1

Arduino UNO R4 Minima

1

BUY

2

USB Type-C cable

1

3

Breadboard

1

BUY

4

Wires

Several

BUY

5

1kΩ resistor

Several

BUY

6

Button

2

BUY

7

LED

Several

BUY

Wiring

../_images/LED_Control1.0_bb.png

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
  }
}