Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

šŸ‘‰ Ready to explore and create with us? Click [here] and join today!

2.2 Display the Levelļƒ

In this lesson, weā€™ll learn how to control an LED Bar Graph using the Raspberry Pi Pico 2. An LED Bar Graph consists of 10 LEDs arranged in a line, typically used to display levels such as volume, signal strength, or other measurements. Weā€™ll light up the LEDs sequentially to create a level display effect.

img_led_bar_pin

What Youā€™ll Need

In this project, we need the following components.

Itā€™s definitely convenient to buy a whole kit, hereā€™s the link:

Name

ITEMS IN THIS KIT

LINK

Newton Lab Kit

450+

Newton Lab Kit

You can also buy them separately from the links below.

SN

COMPONENT

QUANTITY

LINK

1

Raspberry Pi Pico 2

1

BUY

2

Micro USB Cable

1

3

Breadboard

1

BUY

4

Jumper Wires

Several

BUY

5

Resistor

10(220Ī©)

BUY

6

LED Bar Graph

1

Circuit Diagram

sch_ledbar

In this project, each of the 10 LEDs in the LED Bar Graph is connected to the Raspberry Pi Pico 2. The anodes (positive terminals) of the LEDs are connected to GPIO pins GP6 through GP15. The cathodes (negative terminals) are connected through 220Ī© resistors to the GND (ground) pin.

Wiring Diagram

wiring_ledbar

Writing the Code

Note

  • You can open the file 2.2_display_the_level.ino from newton-lab-kit/arduino/2.2_display_the_level.

  • Or copy this code into Arduino IDE.

  • Select the Raspberry Pi Pico 2 board and the correct port, then click ā€œUploadā€.

// Define the GPIO pins connected to the LED Bar Graph
const int ledPins[] = {6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

void setup() {
  // Initialize each pin as an output
  for (int i = 0; i < 10; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  // Turn on LEDs sequentially
  for (int i = 0; i < 10; i++) {
    digitalWrite(ledPins[i], HIGH); // Turn on LED
    delay(500);                     // Wait 500 milliseconds
    digitalWrite(ledPins[i], LOW);  // Turn off LED
    delay(500);                     // Wait 500 milliseconds
  }
}

After uploading the code, the LEDs on the bar graph should light up one after another, creating a level display effect. Each LED turns on for half a second and then turns off before the next one lights up.

Understanding the Code

  1. Defining the LED Pins:

    Create an array ledPins that holds the GPIO pin numbers connected to each LED on the bar graph.

    const int ledPins[] = {6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    
  2. Initializing the Pins:

    In the setup() function, we set each pin in the ledPins array as an output.

    void setup() {
      for (int i = 0; i < 10; i++) {
        pinMode(ledPins[i], OUTPUT);
      }
    }
    
  3. Controlling the LEDs:

    In the loop() function, we use a for loop to iterate through each LED. We turn it on, wait for 500 milliseconds, turn it off, and then wait another 500 milliseconds before moving to the next LED.

    void loop() {
      for (int i = 0; i < 10; i++) {
        digitalWrite(ledPins[i], HIGH);
        delay(500);
        digitalWrite(ledPins[i], LOW);
        delay(500);
      }
    }
    

Experimenting Further

  • Reverse the Order: Modify the code to light up the LEDs in reverse order.

  • Create a Bounce Effect: After reaching the last LED, make the sequence reverse back to the first LED.

    void loop() {
      // Ascending sequence
      for (int i = 0; i < 10; i++) {
        digitalWrite(ledPins[i], HIGH);
        delay(200);
        digitalWrite(ledPins[i], LOW);
      }
      // Descending sequence
      for (int i = 8; i >= 0; i--) {
        digitalWrite(ledPins[i], HIGH);
        delay(200);
        digitalWrite(ledPins[i], LOW);
      }
    }
    
  • Adjust the Speed: Change the delay times to make the LEDs light up faster or slower.

Conclusion

In this lesson, youā€™ve learned how to control multiple LEDs using the Raspberry Pi Pico and how to create visual effects using simple programming constructs like loops and delays. This foundational knowledge is essential for more advanced projects involving LED displays and indicators.