LED Bar Graph

There are different types of LED Bar Graph. The one shown here is a 10 Segment LED Bar Graph.

Hardware

Component List

  • Raspberry Pi 3 B+ x 1
  • GPIO Extension Board and GPIO Cable x 1 (If you do not have an Extension Board, connect Raspberry Pi and breadboard directly with jumper wires.)
  • Breadboard x 1
  • 10 Segment LED Bar Graph x 1
  • Resistor 220Ω x 10 (Check your LED Bar Graph specification for the correct resistor to use.)
  • Jumper Wire x 11

Schematic Diagram

Breadboard Diagram

Note that on the diagram, pin 1 of the LED Bar Graph is on the lower-left corner.

In this circuit, the power for the LED is from 3.3V pin. When the GPIO is low, the light will be on. When the GPIO is high, the light will turn off.

Program

Create a Console Project.

Add NuGet package Unosquare.Raspberry.IO.

Main Program: Program.cs

The program will turn on/off the lights on the LED Bar Graph one by one.

using Unosquare.RaspberryIO;
using Unosquare.RaspberryIO.Gpio;

...

GpioPin[] pins = new GpioPin[]{
    Pi.Gpio.Pin29,  // pin 40, GPIO21
    Pi.Gpio.Pin28,  // pin 38, GPIO20
    Pi.Gpio.Pin27,  // pin 36, GPIO16
    Pi.Gpio.Pin26,  // pin 32, GPIO12
    Pi.Gpio.Pin11,  // pin 26, CS1
    Pi.Gpio.Pin10,  // pin 24, CS0
    Pi.Gpio.Pin06,  // pin 22, GPIO25
    Pi.Gpio.Pin05,  // pin 18, GPIO24
    Pi.Gpio.Pin04,  // pin 16, GPIO23
    Pi.Gpio.Pin01}; // pin 12, GPIO18

// turn on one by one
for (int pin = 0; pin < pins.Length; pin++)
{
    pins[pin].PinMode = GpioPinDriveMode.Output;
    pins[pin].Write(false);
    System.Threading.Thread.Sleep(500);
}

System.Threading.Thread.Sleep(1500);

// turn off one by one
for (int pin = 0; pin < pins.Length; pin++)
{
    pins[pin].Write(true);
    System.Threading.Thread.Sleep(500);
}

Unosquare.Raspberry.IO uses the pin number the same as Wiring Pi. At Terminal, enter command gpio readall to see pin number mapping. Use the number in column wPi to address the pins.

Leave a Reply