Using Bar Graph Displays with the Raspberry Pi

Bar Graph Displays with the Raspberry PiUsing Bar Graph Displays is really (really!) easy!

Regular readers will know that I recently completed a Kickstarter campaign for a little Pi Zero sized prototyping board. Shameless plug over!

I designed the ProtoZero to fit my projects on the Raspberry Pi Zero, and as part of the campaign I’ve been looking at what kind of components I can fit on the board. One of the components I wanted to try out was the bar graph display.

At first glance online I wasn’t sure quite how they worked, what the pinout looked like and/or if they needed some kind of IC to drive them. Turns out they’re dead easy to use – exactly the same as having 10 LEDs lined up together. I found that surprising as you don’t see these being used a lot, but maybe that’s just a lack of awareness or understanding.

Let me show you how I wired mine up, coded the display and then transferred it to my ProtoZero prototyping board…

Bar Graph display breadboard prototype

My Bar Graph display breadboard prototype

The Bar Graph Display

This display is a really great idea. There are so many projects that you might want a row of LEDs to show readings for – temperature, sound, humidity, unread emails, hours until the next event etc. Whilst wiring up a line of LEDs is perfectly fine, these little displays give your project that cool ‘retro space movie control panel’ look to them.

They use exactly the same number of pins as 10 LEDs, so there aren’t any savings in terms of connections, however they do use a little less room on the breadboard as LEDs need space for their large heads.

I got mine from eBay. Two of these displays cost me £0.99p including postage, which of course at that price is from China. Expect a month or so for delivery. Alternatively order something like this from Adafruit on Amazon at a higher price but much faster delivery.

Wiring

Wiring one of these displays is, you guessed it, the same as wiring 10 individual LEDs.

One side has the row of 10 positive (anode) connections (usually the longer leg on your LED), and the other side is the negative ground connections (cathode). Each anode pin is attached to a GPIO pin, and each cathode pin is connected to a resistor and then to the Pi’s ground pin.

These displays have the same length legs, but you can tell which side has the cathode/ground connections as one of the display’s edges will have a slight chamfer. Check your data sheet as well of course!

Bar Graph Display datasheet

Buy from a decent supplier and you’ll get a datasheet showing the chamfer and pin numbers like this (image courtesy of SparkFun)

Resistance is Futile Essential

As these displays are simply 10 LEDs in a sexy package, you still need to have resistors connected.

This is the tricky part. Most of you will know that the Raspberry Pi can only handle so much current over its 3.3V/GPIO line. As far as I remember, it’s something like 16mA max per individual GPIO pin, with a total max current of 50mA across all GPIO pins combined.

That’s not a lot of juice to share, although a lot of people go over these limits and appear to have no issues.

Maths…or something?

I’m absolutely terrible with this side of electronics, struggling to get the maths right and even know how to work it out in the first place. I simply did not listen enough at school – take note kids. Don’t do drugs, don’t skip school and don’t settle for “Average”.

My simple brain tells me that I have 10 LEDs and a max current of 50Ma across all of them. This means I need to add appropriate resistors that ensure each LED can’t take more than 5mA of current (50 divided by 10).

But what are the LED segments drawing?

We need to know their forward voltage and forward current to have a decent stab at using the right resistors. Unfortunately that’s not available for these displays, being the ultra-cheap Chinese type.

Luckily I found a similar display online that states a normal forward voltage of 3.2v and a normal forward current of 20mA…that’ll do then!

So I have 3.3V going in to each LED from my GPIO. Each LED segment draws 20Ma at 3.2V. So what resistor do I need?

Use an LED Resistor Calculator

I know I know, I should just do the maths in my head or on a piece of paper. Sorry world, Average Man is terrible at maths. Even if I learnt the calculation I’d probably just forget it the next day.

I get around my lack of education with online LED calculators, and I’m a big fan of led.linear1.org. It’s a great online tool for working out which resistors you need, and can also help confirm any manual calculations you may have made.

I popped in the specs of my setup and it pumped out a pretty diagram showing me I should use 22ohm resistors for each LED. That seemed very low to me, but the internet never lies right? Even if I can’t quite find the right resistor, I at least know my limit now.

LED calculator diagram

I used led.linear1.org to work out the resistance for each LED segment

Resistor Networks

One last word with resistors in this project. I came across resistor networks by chance when looking through RS’s site after recently winning a soldering iron from them. On closer inspection they’re perfect for projects like this.

Rather than wire 10 individual LEDs to the same ground pin/lane 10 times, resistor networks pretty much package up a number of resistors in a single component with a common ground pin. This means whilst you still have to solder/connect a leg to each LED segment, you only have to connect the final ground pin once for each network.

network resistors

The different formats do the same thing, but network resistors are much tidier

That’s a much simpler way of doing things so you can see why resistor networks caught my eye. I’m using 2x 6-pin networks, notice the end pins are connected to ground. Less connections, much tidier.

Resistor Network

These resistor networks need just two ground connections to provide resistance for all 10 pins

If the image isn’t clear enough, here’s a little diagram I drew up:

Resistor Network diagram

Resistor networks are great for simplifying multiple ground connections

Simple Code

Of course none of this is worth doing without some code to run on it.

I made a little LED scanner script with help from a couple of my pals from Twitter (@Gadgetoid and @RasPiTV). They taught me how to write Python that uses ‘ranges’ which until now I hadn’t learnt how to use.

It uses the GPIO to light up each segment one by one in order, then starts at the beginning again. It’s a simple script just to test the circuit works:

#!/usr/bin/python
 
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

leds = [3,5,7,10,11,13,15,16,18]

for led in leds:
    GPIO.setup(led, GPIO.OUT, initial=0)

time.sleep(1)

while True:

    for led in leds:
       GPIO.output(led, 1) # ON
       time.sleep(0.1)
       GPIO.output(led, 0) # OFF

You can download the code over on PasteBin. Here’s a video of the scanner in action:

Making Something With It

I decided to use the display with the DS18B20 temperature sensor that I already had from my CamJam Edukit. The original sensor came in a waterproof probe housing so I ordered another one from eBay that was ready for a PCB (In the image below I use a module version just for testing).

I had some example code for it thanks to the example worksheets, so I fitted the sensor into my breadboard and wrote a script that would light each of the 10 segments for a change in temperature between 21 and 30 degrees.

I also added a separate 5mm LED each side to indicate the last change in temperature, positive or negative.

Temperature sensor prototype

My larger prototype with the temperature sensor. I used individual LEDs here , but replaced with the network resistors later on.

Once it was all working I soldered the project together on one of my ProtoZero boards (shameless plug #2) and ran it on my shiny new Pi Zero:

ProtoZero Bar Graph Soldering

A complex soldering job, but easy enough if you plan and take your time

ProtoZero Pi Zero Bar Graph Project

The finished project: Here the temperature has just decreased, lighting the blue LED

You can find the Python script for this project over on PasteBin.

Summary

What started off as just a bit of curiosity about bar graph displays ended up becoming a temperature monitoring project fully soldered to a PCB.

Being as simple as a collection of LEDs, these little modules are a really fun way of making cool looking projects. As each segment is controlled by GPIO pins, you can can make the code as complex and advanced as you want to have the display representing all manner of data.

I might come back to these displays in the future and join a few together, but I’ll have power and current issues to consider. Another challenge for another day!

Until next time…

4 Comments on "Using Bar Graph Displays with the Raspberry Pi"

  1. Clare Macrae | 28/01/2016 at 08:54 | Reply

    Thanks for all your articles showing projects with ProtoZero…

    It feels like all the ones I’ve seen jump from ‘long, clear, detailed explanation of each step on the breadboard’ to ‘here’s a one-line caption on a photo of the finished thing on ProtoZero’

    I would love to see an article showing how you plan and implement going from breadboard to ProtoZero – e.g. what’s your thinking process with layout, how do you make it look so tidy, what tips do you have… why are there resistors on the diagonal corners etc…

    I’m sure there are many things that you’ve figured out, through practice and experience, that us backers would benefit from!

    • Hi Clare

      I know what you mean, but I guess that’s because this post was more about the bar graph display than the ProtoZero. That last bit is just a quick example of how it could put into a project.

      I think you’re on to something re a breadboard layout/planning blog though. It’ll be hard to get across (might need a few videos) but definitely worthwhile for people new to them.

      I’ll get on it – thanks! 🙂

  2. Tony Goodhew | 12/05/2016 at 16:25 | Reply

    Very neat project on your ProtoZero (and I was a backer). I looked at your code and thought it could be shorter. Here is a procedure to draw the graph given the temp:
    def showTemp(t): # LED bar ON for 21 to 30 inclusive
    graphVal = t – 20
    for i in range(10):
    GPIO.output(LEDs[I],0) # All LEDs OFF
    if ((t > 20) & (t < 31)):
    for i in range(graphVal):
    GPIO.output(LEDs[i],1)

    I used 330 Ohm resistors on the LEDs, as normal, and they are bright enough.
    LEDs = [17,18,27,22,23,24,25,5,6,12] # Pins left to right – yours may be different
    for n in range(10):
    GPIO.setup(LEDs[n], GPIO.OUT)

    will set them up.

    If you would like the full sketch ask by email to arduinolink (AT) gmail.com

  3. Tony Goodhew | 12/05/2016 at 16:30 | Reply

    Sorry it zapped the indents – send me an email

Leave a comment

Your email address will not be published.


*