2.4 LED Bar Graph

Overview

In this lesson, you will learn something about LED Bar Graph. Generally, LED Bar Graph works as a battery level indicator, Audio equipment, industrial control panel. If we want, we can also find its other application.

Fritzing Circuit

In this example, we use digital pins 2~11 to drive the LED Bar Graph. LED Bar Graph has ten separate LEDs inside and each LED has two pins. The left pins 1~10 of LED Bar Graph are connected with the digital pins 2~11 respectively; the right side pins 11~20 are separately extended to same side of these 220ohm resistors whose other sides are identically connected to GND.

../_images/image82.png

Schematic Diagram

../_images/image427.png

Code

Note

  • You can open the file 2.4_ledBarGraph.ino under the path of sunfounder_vincent_kit_for_arduino\code\2.4_ledBarGraph directly.

  • Or copy this code into Arduino IDE 1/2.

  • Or click Open Code to open it in Web Editor.

  • Then Upload the Code to the board.

Uploaded the codes to the Mega2560 board, you can see that the LEDs on the LED Bar Graph flash in sequence.

Code Analysis

The codes in setup() use the for loop to initialize pins 2~11 to output mode in turn.

for(int i=2;i<=11;i++)
{
    pinMode(i,OUTPUT);
}

The for loop is used in loop() to make the LED flash(turn on 0.5s, then turn off 0.5s) in sequence.

for(int i=2;i<=11;i++)
{
    digitalWrite(i,HIGH);
    delay(500);
digitalWrite(i,LOW);
    delay(500);
}

Refer to Part 1-1.2 Digital Write for more details about controlling the LED by using digital pins.

1.2 Digital Write

Phenomenon Picture

../_images/image84.jpeg