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.
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+ |
You can also buy them separately from the links below.
SN |
COMPONENT |
QUANTITY |
LINK |
---|---|---|---|
1 |
1 |
||
2 |
Micro USB Cable |
1 |
|
3 |
1 |
||
4 |
Several |
||
5 |
10(220Ī©) |
||
6 |
1 |
Circuit Diagram
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
Writing the Code
Note
You can open the file
2.2_display_the_level.ino
fromnewton-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
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};
Initializing the Pins:
In the
setup()
function, we set each pin in theledPins
array as an output.void setup() { for (int i = 0; i < 10; i++) { pinMode(ledPins[i], OUTPUT); } }
Controlling the LEDs:
In the
loop()
function, we use afor
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.