PiGlow – Lesson 1

The PiGlow, a board for the RaspberryPi system that had a triad of colored LED’s. Eighteen in all, white, blue, green, yellow, orange and red. The controller, an SN3218 8-bit 18-channel PWM chip, is access via the I2C protocol so you could connect this up separately if wished. Though the simplest way is to connect it via the GPIO header.

I installed the Pimoroni library but found it difficult to understand. so I used one that was recommended on the PiGlow page. I also choose to use the Pi Zero, smaller and I wanted to see how the single core would handle the PiGlow.

In the video you will see the first version would pulse irregularly and some LED’s would flash individually. I not quite sure why but I decided to try a different method of updating the LED’s, by using a list to sudo-randomly update all the LED’s with the same brightness value. each program will be included below and I have commented nearly every line so that it is clear what is going on.

As a summery comparison:

The first version would use a loop through chosen brightness. The max is 255, You will notice that I used 100, in the second program, rather than 255. This was to avoid the retina burning brightness that 255 is on the PiGlow. Then use the modulus operator “%” to use the remainder as the led number, divide by 18. Then the brightness, divide by 100. looping through this forever.

For the second version I used two loops. The first would loop through the chosen brightness max. The second was inside the brightness loop, looping through eighteen, the number of LED’s. The number is used as the index for a list of numbers to sudo-randomize the writing of the brightness value.

I also used time to build in a pause, to allow for controlling the pulse speed. The delay was the same in each program. The apparent difference of the pulse is due to the method of writing the brightness value to the LED’s.  The first updated the brightness and wrote to a LED. The second will update the brightness and update all eighteen LED’s. for each brightness change.

Watch the short video and then review the programs below. Let me know what you think!

#!/usr/bin/python
######################################################
## Create a pulsing spiral lighting each led ##
## Version 1 using a method I saw in another script ##
## ##
######################################################

# Import needed modules
import time
from piglow import PiGlow

# An alias, so you can type piglow rather than PiGlow()
piglow = PiGlow()

q = 0.0003 # Delay for time.sleep in seconds
x = 1 # Iniialize x, 0 causes .led to turn them off
# Used to define led and brightness

y = 1 # Initialize y for main loop

piglow.all(0) # turn off all led,

while y > 0: # Begin the pulse loop

for x in range(255): # Start the brighten loop

m = (x % 19) # Make sure to only have 1-18 for led
# by dividing by 19 and using the remainder

if m == 0: # LED can’t be zero so if it is, set to 1
m= m + 1

n = (x % 255) # Make sure the brightness value does not exceed 255
# by dividing by 255 and using the remainder

piglow.led(m,n) # Set the led m=led, n=brightness

time.sleep(q) # Delay the loop by q

x = x + 1 # Add one to x and loop

while x > 0: # Start the dimming loop

m = (x % 19)

if m == 0: # LED can’t be zero so if it is set to 1
m= m + 1

n = (x % 255) # Make sure the brightness value does not exceed 255

time.sleep(q) # Delay the loop by q

piglow.led(m,n) # Set the led m=led, n=brightness

x = x – 1 # Decrease x by 1

 


#!/usr/bin/python
######################################################
## create a pulsing spiral lighting each led ##
## an alternate method that eliminate some ##
## uneven changes visible ##
## ##
######################################################

import time
from piglow import PiGlow

piglow = PiGlow()

q = 0.001 # Delay for time.sleep seconds

x = 1 # Iniialize x, 0 causes .led to turn them off
# Used to define led and brightness
maxbrite = 100 # The maximum brightness to use, absolute max is 255
y = 1 # Initialize y for main loop

m = 0 # Initialize key for led list selection loop

# LED selection list is below
# Odd then even leds
#l=[1,3,5,7,9,11,13,14,17,2,4,6,8,10,12,14,16,18]

# An alternate writing pattern that starts at center and moves
# out, around the center counter clockwise.
# Comment all but one at a time
l=[12,6,18,11,5,17,10,4,16,9,3,15,8,2,14,7,1,13

piglow.all(0) # Turn off all led, just a housekeeping line

while y > 0: # Begin the pulse loop

while x < maxbrite : # Start the brighten loop. The max is 255 for brightness of led

for m in range(18): # Loop to set the leds to current brightness
piglow.led(l[m],x) # Set the led l[m]=led, n=brightness
m = m + 1 # Move to next in list l

time.sleep(q) # Delay the loop by q

x = x + 1 # Add one to x and loop
while x > 0 : # Start the dimming loop

for m in range(18): # Loop to set the leds to current brightness
piglow.led(l[m],x) # Set the led l[m]=led, n=brightness
m = m + 1 # Move to next in list l

time.sleep(q) # Delay the loop by q
x = x – 1 # Add one x and loop

 

Posted in Programming, Python, Raspberry Pi, Tutorial | Tagged , , | Comments Off on PiGlow – Lesson 1