Easy
Getting Started with PiGlow
Learn how to use PiGlow
So you've bought a PiGlow, or you're thinking of picking one up? I'm sure you've seen one by now- welding goggles in place - and know what to expect from the little swirl of 18 blindingly bright LEDs. This guide will walk you through installing the software necessary to get PiGlow up an running, verifying it's installed properly and taking your first steps writing your very own PiGlow code in Python.
Before we start
Before installing PiGlow, it's good practise to update the software on your Pi. If you already know how to do this, go right ahead, otherwise you can read our guide to "Keeping your Pi Up-to-date".
Setting up PiGlow
We've made PiGlow easy to get up and running with a one-liner install script. That's one line that you have to type, or copy and paste, in order to run all the necessary steps required for the install. Once your Pi is up-to-date just open LXTerminal, type in the following and press the Enter key:
curl https://get.pimoroni.com/piglow | bash
Verify that PiGlow is installed and working correctly
With LXTerminal still open, navigate to the PiGlow examples folder and run one of the Python scripts you find there, you can CD ( change directory ) into the PiGlow examples folder like so:
cd ~/Pimoroni/piglow
You'll find all of our examples in ~/Pimoroni, the ~ character ( tilde ) is a handy shortcut in Linux that always points to your home directory ( Which is usually /home/pi ).
Once you're in the examples folder, run an example like so:
sudo python legs.py
You can also see what examples, or subfolders, are available simply by typing:
ls
A brief introduction to PiGlow
Still with me? Still looking at LXTerminal? Good! Now let's fire up an interactive Python shell and learn some of the basics of using PiGlow.
First we'll start Python with "super user" privileges, because your Pi will need these to talk to PiGlow:
sudo python
You should see a short message with the Python version number and some other information, and then a new sort of command prompt that looks like this:
>>>
To use PiGlow, we'll first need to import its library:
>>> import piglow
Now we can turn LEDs on, off, or set them to a brightness between 0 and 255. PiGlow has 2 main ways of locating LEDs. You can either address one by its index, from 0 to 17, or by its Ring and Leg. Rings are the 6 different rings of LEDs from the center of the PiGlow radiating out to the edges, and Legs are the three individual Legs ( or arms if you want ) of the spiral.
The main function in PiGlow world, is, however, set. This sets one or more LEDs to one or more values and can be called in a variety of ways:
piglow.set(0,255) # Set LED 0 to full brightness
piglow.set(0,[255,255,255]) # Set LEDs 0, 1 and 2 to full brightness
piglow.set([0,2,4],[255,255,255]) # Set LEDs 0, 2 and 4 to full brightness
Either one or both parameters can be a single number, or a list. Lists are great because we can generate them all kinds of ways- the most useful being Python's range() function.
Using Ranges
You can produce 18 values from off, to full brightness easily with Python ranges:
values = list( range(0, 255, int( 255 / 18.0 ) ) )
piglow.set(0, values)
It's just a range from 0 to 255 with a step size of 255 divided by the number of LEDs on PiGlow!
A Dash Of Modulus
Also handy when dimming LEDs is the modulus operator, this is a simple mathematical operator that gives you the remainder of dividing by a number. It's great for telling if numbers are odd or even, but also fantastic at clamping to a range like 0 to 255. Here's why:
1 % 2 = 1 # Odd
2 % 2 = 0 # Even
3 % 2 = 1 # Odd
4 % 2 = 0 # Even
256 % 256 = 0
254 % 256 = 254
2 % 256 = 2
18723 % 256 = 35
-23123 % 256 = 173
Try as we might, if we only care about the remainder we get when dividing the brightness value by 256 ( there are 256 possible values in the range 0 to 255 ) then we'll never get a number greater than 255 or less than 0!
So we can:
brightness = 0
while True:
brightness++
piglow.set(0, [brightness % 256] * 18)
time.sleep(0.01)