Getting a PiGlow to work with a Raspberry Pi 2

I really like the idea of this PiGlow product which you can get for $7 on Amazon:

image

This is a little card with 18 LED’s on it, which plugs into the GPIO ports on your Raspberry Pi. You can then programmatically turn the LED’s on and off for whatever reason you’d like! So, how do you use it?

Why are you writing a blog post about this?
I spent probably 8 hours now, trying to get this to work with a Raspberry Pi 2. In typical Linux/open source fashion, information is disorganized, chaotic, and kind of a train wreck, in general. I cobbled together information from a ton of sources and finally got it working, so I wanted to write this down in one place. There might be other ways to get this working – but this is one way that I know, reproducibly works.

Setting up your Pi, from scratch:
This all came about because I had Pi’s which already had software installed. When I tried the “typical” sample to get this working, it didn’t. So, I re-formatted my SD card and started from a fresh copy of Raspbian. In case you want to do that, see below:

To format your own SD card:

  1. Download the latest OS (Raspian) from here: https://www.raspberrypi.org/downloads/
  2. Download Win32DiskImager from here: http://sourceforge.net/projects/win32diskimager/
  3. Plug your SD card into your computer (if your computer doesn’t have a reader, you can buy one for ~$10)
  4. In the Win32DiskImager, point to your Raspian “image”, and the drive where your SD card is plugged in and “Write” the image to the card.
  5. When done, put the SD card in the Pi – unplug, then plug in the power again to boot it.
  6. Connect a monitor via HDMI – or just connect over the network with SSH.

On the Pi, when you first boot up, if you connect to it over the network with an SSH client (here’s what I recommend), you’ll see this:

image

So, run that command:

$ sudo raspi-config

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

and you’ll see the configuration tool:

image

The errors are next to the things I typically change. For example, I usually run Pi’s without a monitor plugged in. This means I don’t need the “Windows”-like login. I want it to quickly boot to console mode:

image

Under Advanced options, here are the settings I typically change here:

image

Again, I rarely use the Pi with a Windowing environment, so I give the GPU the minimum amount of memory:

image

Next, if you are going to use the PiGlow, you MUST enable the I2C interface. I2C is a intra-device protocol, a common way for the Pi to talk to other devices that are connected to it (see here for more):

image

image

After that, you should probably reboot:

$ sudo reboot

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Cleaning up and updating the Pi:
One last thing I like to do on the Pi is to have a script which gets it updated. To start, open an editor and create a file:

$ pico ./pi-update.sh

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Then, copy and paste these contents into the file:

#!/bin/bash

# Re-sync repository information.
sudo apt-get update -y

# Upgrade all existing packages/software.
sudo apt-get upgrade -y

# Remove automatically-installed dependencies which are longer needed.
sudo apt-get autoremove -y

# Clears out the package download cache, freeing up disk space.
sudo apt-get autoclean -y

# Update the Raspberry Pi firmware to the latest.
sudo rpi-update

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Exit and save the changes to the file. Then, to be able to execute it, mark it as executable:

$ chmod +x ./pi-update.sh

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Finally, to run it, just type:

$ ./pi-update.sh

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

After you run that once, you probably want to reboot one more time (sudo reboot).

Handle the prerequisites for the PiGlow:
OK, at this point we have a fresh Pi that started from a clean image – and it’s now all patched too. To just have access to the GPIO ports directly, you can get away with just doing this:

# Install this SMBus functionality (whatever that is)?
sudo apt-get install python-smbus

# Add: i2c-dev and i2c-bcm2708 on separate lines to 
# /etc/modules, so they will load on boot.
$ sudo pico /etc/modules

That /etc/modules file should now look like this:

image
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

After a reboot, you should be able to run any Python example that uses SMBus. For example, from:

https://github.com/pimoroni/piglow/blob/master/examples/piglow-example.py

where you’d see code like this:

image

For example, if you create that piglow-example.py file (or upload it with SCP), you can now execute the Python file like this:

$ sudo python ./piglow-example.py

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

and voila, you’ll see the PiGlow light up VERY brightly.

Working with PyGlow/PiGlow:
That’s well and great, but code like the above is not particularly noob or child-friendly. Luckily, someone created a Python module which makes it super-easy to interact with the PiGlow. The trick, as it turns out, is getting THAT to work. Assuming you have everything done from the above, below is what else was needed:

Install “pip”, the Python Package Manager:

$ sudo apt-get install python-pip

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Now, with pip, install the “sn3218” driver, which is used to interface with the LED controller on the PiGlow:

$ sudo pip install sn3218

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Next, you can download the PyGlow module with a command like this (details from here: https://github.com/benleb/PyGlow):

$ sudo pip install git+https://github.com/benleb/PyGlow.git

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Finally, now you can use the “PyGlow” module from Python. Let’s start with the interactive Python REPL – run “sudo python”:

image

after running the code highlighted in yellow, you should see the blue lights on the PiGlow come up full brightness. How do you know the syntax, and that “2” is blue? All of that is well-documented here: https://github.com/benleb/PyGlow

Now that we have this module working, go take a look at the samples that exist for it: https://github.com/benleb/PyGlow/tree/master/examples

In particular, I liked the cpu.py. This monitors CPU utilization on the Pi and lights up LED’s based on utilization. For this example, we have to install another Python module called “psutil”, or process utility:

$ sudo apt-get install python-psutil

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Now, if you create or upload that cpu.py file to your Pi, you can run it with something like this:

$ sudo python ./cpu.py

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Then, in another window run something that will trigger some CPU activity for a few seconds, like:

$ find / *e

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

That spends a minute look for files that end in “e”, starting from the root of the drive. And you should see the LED’s of your PiGlow turning on and off based on CPU utilization.

Bottom line:
This took a LOT of work to figure out – despite being a relatively silly thing to pursue. However, I think it’s worth it because especially for noobs or children, look how easy this code is!

Even if you claim to not know the Python programming language, you can kind of figure out what this code does?

image

So, that’s the point. Congratulations, you’re now a Python programmer! Even better, you are a Python program who can now easily interact with 18 colored LED’s – could you have some fun playing around with that?

So what are you waiting for – go get yourself a Raspberry Pi 2, and pick up a PiGlow while you’re at it and see if you too, can get this working!

Posted in Linux, Raspberry Pi, Uncategorized
9 comments on “Getting a PiGlow to work with a Raspberry Pi 2
  1. […] Getting a PiGlow to work with a Raspberry Pi 2https://blog.robseder.com/2015/04/12/getting-a-piglow-to-work-with-a-raspberry-pi-2/ […]

    Like

  2. Mario says:

    This is a great tutorial, however I’m struggling to figure out how to physically attach the piglow? There are too many pins on the Raspberry Pi 2 for the piglow.

    Like

    • Robert Seder says:

      Mario,

      Connect it so the PiGlow hangs over the Raspberry Pi (NOT so it hangs off the side). As far as aligning the pins, plug it in so that the PiGlow is flush with the end of the Pi. There will be unused pins towards the middle of the Pi, but the plugged-in part goes flush to the end. I hope that helps

      -Rob

      Like

  3. philmiami says:

    Hello,
    I am using a Pi 2 Model B (got it from Amazon Canakit). I am using it for Pi-Hole (ad blocker as DNS). It has the DietPi w/Pihole image on the microSD card and is connected to my router. It performs it job at blocking ads on IPv4 (have not seen or figured how to enable IPv6 to get it to block ads BUT anyway) I wanted to know how can I get the PiGlow to work on a DietPi image?
    I don’t know how to give it the whole Raspi image and getting the PiHole to work is like a long long process. BUT it works so now I wanted to get the PiGlow working. To just do as this python script is, the more the CPU works (all 4 cores) then the brighter it will get. So could someone help me please? I am NOT a programmer, so has to be like picture diagrams for changing a light bulb ok. Thank you. philmiami

    Like

    • Robert Seder says:

      Hey Phil,

      Looks like a pretty cool project! With the PiGlow – I’m guessing the answer is: not easily. Even in the ideal scenario of a supported operating system and an available module, you still have to know a little bit of programming. However, with that said, Python runs on virtually everything. This means that the PiGlow module will likely work anywhere Python does. So – it would take some work to get it all working, but there is no turn-key solution that I know of. The PiGlow seems to just be for tinkering, as there is not really a commercial product built around it. I hope that helps and good luck!

      -Rob

      Like

  4. Manu K. says:

    Hey Rob, thanks for that gread manual!

    Does ur piglow make a sound when brightness is high? Mine does, and its loud.. like a tinnitus

    I’m wondering if mine is broken? The LEDs work just fine

    Manu

    Like

    • Robert Seder says:

      Manu,

      Thanks! Yeah, it sounds like yours might be defective a little bit. I have a couple, they make a very quiet high-pitch noise when on full brightness, but it’s almost not noticeable!

      -Rob

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 2 other followers

%d bloggers like this: