Back to Top

WEB & IT SERVICES

Quality Web & IT Services, based in Doncaster, Yorkshire. Telephone +44 (0)7578 180443

headerfacebook headertwitter

Python Code for the Raspberry Pi

I have written a few simple Python programs to control the add-on Pimoroni PiGlow board.  These are ideal for educational and fun use and are all free!

 

PiGlow is a small add on board for the Raspberry Pi that provides 18 individually controllable LEDs.  The PiGlow is available from

PiGlow

Installation instructions, documentation and examples are available from

https://github.com/pimoroni/piglow#setting-up-your-raspberry-pi

https://github.com/boeerb/PiGlow (Jason Barnett)

https://github.com/benleb/PYGlow (Ben Lebherz)

PyGlow is a small Python module for the PiGlow.  The Ben Lebherz fork of pyglow.py is used for this project.

https://github.com/benleb/PYGlow

 

Keylights

keylights.py is a simple Python program I have written to illuminate the LEDs on the PiGlow using the keyboard keys.  The code is fully commented and is ideal for educational purposes in schools and basic Python coding.  It is also fun watching all the different coloured lights as you press the keyboard keys!  It is free of charge to use and modify.

 

The PyGlow module can control the LED colours directly by using a number or colour name (see the code for the z key which illuminates all the white LEDs).  I have defined the indvidual LEDs of the same colour by using all_white_leds = [6,12,18] # All white LEDs.  This makes it easier if you wish to change the individual colours of LEDs as a group.  For example, traffic_light_colours = [1,2,4,7,8,10,13,14,16].

 

Click here to download keylights.py as a Zip archive.

 

Notes

The very nature of this program is that it flashes LED lights on the PiGlow.  Do not use this program if flashing lights would present any issues.  Keylights.py uses the curses Python library.  The curses library supplies a terminal-independent keyboard-handling facility for text-based terminals and provides fairly basic functionality.  The library works on key press events only and allowance has been made for this.

 

The timing of the LEDs and brightness can be changed by altering the below settings.

led_on_time = 0.2 # LED on time (key pressed)  The amount of time the LED will illuminate after each key press.

led_brightness = 250 # Brightness of LEDs (0-250) The brightness of the LEDs (default setting is the maximum brightness)

led_off_time = 0.0 # LED off time (key held down) The amount of time the LED will be extinguished if a key is pressed and held down.  For example, setting the led_on_time and led_off_time to 1.0 will cause the LED(s) to pulse 1 second on and 1 second off, if the keyboard key is held down.  These timings will likely be affected if the Raspberry Pi has a number of other tasks running at the same time.

Have fun!

 

The keylights.py script is show below.  This can be copied and pasted directly into a suitable editor such as Notepad++.

 

#!/usr/bin/env python

##

## Keylights (keylights.py)  A Python program to illuminate LEDs

## on the PiMoroni PiGlow, using keyboard key presses.

## Copyright (C) 2014  George W. Jopling

##

## This program is free software: you can redistribute it and/or modify

## it under the terms of the GNU General Public License as published by

## the Free Software Foundation, version 3 of the License.

##

## This program is distributed in the hope that it will be useful,

## but WITHOUT ANY WARRANTY; without even the implied warranty of

## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

## GNU General Public License for more details.

##

## You should have received a copy of the GNU General Public License

## along with this program.  If not, see http://www.gnu.org/licenses/.

##

## http://www.georgejopling.com

##

##***********************************************************************

## PyGlow

## Python module to control Pimoroni’s PiGlow

## (http://shop.pimoroni.com/products/piglow)

##

## For further information, documentation and examples see:

## https://github.com/boeerb/PiGlow (Jason Barnett)

## https://github.com/benleb/PYGlow (Ben Lebherz)

##

## The PyGlow.py used is the Ben Lebherz fork

##***********************************************************************

# Imports

from pyglow import PyGlow

from time import sleep

import curses # Use the Curses library for keyboard handling

stdscr = curses.initscr() # Initialise the library

curses.noecho() # Switch off echoing characters to the Terminal

curses.cbreak() # Enter cbreak mode (characters read one by one)

pyglow = PyGlow() # PyGlow

# Change the below settings to suit your preferences

# ***************************************************************

led_on_time = 0.2 # LED on time (key pressed)

led_brightness = 250 # Brightness of LEDs (0-250)

led_off_time = 0.0 # LED off time (key held down)

# ***************************************************************

# Group LEDs

all_red_leds = [1,7,13] # All red LEDs

all_amber_leds = [2,8,14] # All amber LEDs

all_yellow_leds = [3,9,15] # All yellow LEDs

all_green_leds = [4,10,16] # All green LEDs

all_blue_leds = [5,11,17] # All blue LEDs

all_white_leds = [6,12,18] # All white LEDs

pyglow.all(0) # Extinguish all LEDs

# Display the keyboard keys used on the Terminal screen

stdscr.addstr(0,0, “Keylights”,

        curses.A_REVERSE)

stdscr.addstr(1,0, “Keys: w,e,r,t,y,u,i,o,p”,

        curses.A_REVERSE)

stdscr.addstr(2,0, “Keys: s,d,f,g,h,j,k,l,;”,

        curses.A_REVERSE)

stdscr.addstr(3,0, “Keys: z,x,c,v,b,n,m,,./,spacebar”,

        curses.A_REVERSE)

stdscr.addstr(4,0, “q or ctr+c to Quit)”,

        curses.A_REVERSE)

stdscr.refresh()

def flush_buffer (): # Function to flush the curses input buffer after keypress

        curses.flushinp() # Flush input buffer

        sleep(led_off_time) # Pause LED off time

        return

try:

    while True: # Begin loop (repeat until Ctr + C  or q keypress)  

        c = stdscr.getch() # Get keyboard key press    

        # Arm 1 individual LEDs on PyGlow

        if c == ord(‘e’): # Check for letter e keypress

            pyglow.led (1,led_brightness) # Illuminate red

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (1,0) # Extinguish red

            flush_buffer()

        elif c == ord(‘r’): # Check for letter r keypress

            pyglow.led (2,led_brightness) # Illuminate amber

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (2,0) # Extinguish amber

            flush_buffer()

        elif c == ord(‘t’): # Check for letter t keypress

            pyglow.led (3,led_brightness) # Illuminate yellow

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (3,0) # Extinguish yellow

            flush_buffer()

        elif c == ord(‘y’): # Check for letter y keypress

            pyglow.led (4,led_brightness) # Illuminate green

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (4,0) # Extinguish green

            flush_buffer()

        elif c == ord(‘u’): # Check for letter u keypress

            pyglow.led (5,led_brightness) # Illuminate blue

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (5,0) # Extinguish blue

            flush_buffer()

        elif c == ord(‘i’): # Check for letter i keypress

            pyglow.led (6,led_brightness) # Illuminate white

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (6,0) # Extinguish white

            flush_buffer()

        # Arm 2 individual LEDs on PyGlow

        elif c == ord(‘d’): # Check for letter d keypress

            pyglow.led (7,led_brightness) # Illuminate red

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (7,0) # Extinguish red

            flush_buffer()

        elif c == ord(‘f’): # Check for letter f keypress

            pyglow.led (8,led_brightness) # Illuminate amber

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (8,0) # Extinguish amber

            flush_buffer()

        elif c == ord(‘g’): # Check for letter g keypress

            pyglow.led (9,led_brightness) # Illuminate yellow

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (9,0) # Extinguish yellow

            flush_buffer()

        elif c == ord(‘h’): # Check for letter h keypress

            pyglow.led (10,led_brightness) # Illuminate green

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (10,0) # Extinguish green

            flush_buffer()

        elif c == ord(‘j’): # Check for letter j keypress

            pyglow.led (11,led_brightness) # Illuminate blue

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (11,0) # Extinguish blue

            flush_buffer()

        elif c == ord(‘k’): # Check for letter k keypress

            pyglow.led (12,led_brightness) # Illuminate white

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (12,0) # Illuminate white

            flush_buffer()

        # Arm 3 individual LEDs on PyGlow

        elif c == ord(‘c’): # Check for letter c keypress

            pyglow.led (13,led_brightness) # Illuminate red

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (13,0) # Extinguish red

            flush_buffer()

        elif c == ord(‘v’): # Check for letter v keypress

            pyglow.led (14,led_brightness) # Illuminate amber

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (14,0) # Extinguish amber

            flush_buffer()

        elif c == ord(‘b’): # Check for letter b keypress

            pyglow.led (15,led_brightness) # Illuminate yellow

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (15,0) # Extinguish yellow

            flush_buffer()

        elif c == ord(‘n’): # Check for letter n keypress

            pyglow.led (16,led_brightness) # Illuminate green

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (16,0) # Extinguish green

            flush_buffer()

        elif c == ord(‘m’): # Check for letter m keypress

            pyglow.led (17,led_brightness) # Illuminate blue

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (17,0) # Extinguish blue

            flush_buffer()

        elif c == ord(‘,’): # Check for , keypress

            pyglow.led (18,led_brightness) # Illuminate white

            sleep(led_on_time) # Pause led_on_time

            pyglow.led (18,0) # Extinguish white

            flush_buffer()

        # colours

        elif c == ord(‘w’): # Check for w keypress

            pyglow.set_leds (all_red_leds,led_brightness) # Illuminate all red LEDs

            pyglow.update_leds() # update LEDs

            sleep(led_on_time) # Pause led_on_time

            pyglow.set_leds (all_red_leds,0) # Extinguish all red LEDs

            pyglow.update_leds()# update LEDs

            flush_buffer()

        elif c == ord(‘s’): # Check for s keypress

            pyglow.set_leds (all_amber_leds,led_brightness) # Illuminate all amber LEDs

            pyglow.update_leds() # update LEDs

            sleep(led_on_time) # Pause led_on_time

            pyglow.set_leds (all_amber_leds,0) # Extinguish all amber LEDs

            pyglow.update_leds()# update LEDs

            flush_buffer()

        elif c == ord(‘x’): # Check for x keypress

            pyglow.set_leds (all_yellow_leds,led_brightness) # Illuminate all yellow LEDs

            pyglow.update_leds() # update LEDs

            sleep(led_on_time) # Pause led_on_time

            pyglow.set_leds (all_yellow_leds,0) # Extinguish all yellow LEDs

            pyglow.update_leds()# update LEDs

            flush_buffer()

        elif c == ord(‘o’): # Check for o keypress

            pyglow.set_leds (all_green_leds,led_brightness) # Illuminate all green LEDs

            pyglow.update_leds() # update LEDs

            sleep(led_on_time) # Pause led_on_time

            pyglow.set_leds (all_green_leds,0) # Extinguish all green LEDs

            pyglow.update_leds()# update LEDs

            flush_buffer()

        elif c == ord(‘l’): # Check for l keypress

            pyglow.set_leds (all_blue_leds,led_brightness) # Illuminate all blue LEDs

            pyglow.update_leds() # update LEDs

            sleep(led_on_time) # Pause led_on_time

            pyglow.set_leds (all_blue_leds,0) # Extinguish all blue LEDs

            pyglow.update_leds()# update LEDs

            flush_buffer()

        elif c == ord(‘.’): # Check for . keypress

            pyglow.set_leds (all_white_leds,led_brightness) # Illuminate all white LEDs

            pyglow.update_leds() # update LEDs

            sleep(led_on_time) # Pause led_on_time

            pyglow.set_leds (all_white_leds,0) # Extinguish all white LEDs

            pyglow.update_leds()# update LEDs

            flush_buffer()    

        elif c == ord(‘ ‘): # Check for space bar keypress

            pyglow.all (led_brightness) # Set all LEDs

            pyglow.update_leds()# Illuminate all LEDs on PiGlow

            sleep(led_on_time) # Pause led_on_time

            pyglow.all(0)

            pyglow.update_leds()# Extinguish all LEDs on PiGlow

            flush_buffer()

        # Arms

        elif c == ord(‘p’): # Check for p keypress

            pyglow.arm(1,led_brightness) # Illuminate arm 1 LEDs

            pyglow.update_leds() # Update LEDs

            sleep(led_on_time) # Pause led_on_time

            pyglow.arm(1,0) # Extinguish arm 1 LEDs

            pyglow.update_leds() # Update LEDs

            flush_buffer()

        elif c == ord(‘;’): # Check for ; keypress

            pyglow.arm(2,led_brightness) # Illuminate arm 2 LEDs

            pyglow.update_leds() # Illuminate white

            sleep(led_on_time) # Pause led_on_time

            pyglow.arm(2,0) # Extinguish arm 1 LEDs

            pyglow.update_leds() # Update LEDs

            flush_buffer()

        elif c == ord(‘/’): # Check for / keypress

            pyglow.arm(3,led_brightness) # Illuminate arm 3 LEDs

            pyglow.update_leds()# Illuminate white

            sleep(led_on_time) # Pause led_on_time

            pyglow.arm(3,0) # Extinguish arm 1 LEDs

            pyglow.update_leds() # Update LEDs

            flush_buffer()

        

        # PyGlow Colour

        elif c == ord(‘z’): # Check for z keypress

            pyglow.color(1,led_brightness) # Illuminate white LEDs using PyGlow method

            pyglow.update_leds() # Update LEDs

            sleep(led_on_time) # Pause led_on_time

            pyglow.color(1,0) # Extinguish white LEDs

            pyglow.update_leds() # Update LEDs

            flush_buffer()

        # Quit                      

        elif c == ord(‘q’):

            pyglow.all(0) # Extinguish all LEDs

            curses.nocbreak() # Leave cbreak mode

            curses.echo() # Switch on echoing characters to the terminal

            curses.endwin() # De-initialise curses library.  Normal terminal

            break # Quit program

            

except KeyboardInterrupt: # Catch Ctrl + C keypress

    pyglow.all(0) # Extinguish all LEDs

    curses.nocbreak() # Leave cbreak mode

    curses.echo() # Switch on echoing characters to the terminal

    curses.endwin() # De-initialise curses library.  Normal terminal  

 

3 Way Junction Python Script

 

3way_junction.py is a simple Python program I have written to simulate a 3 way LED traffic light controlled road junction on the PiGlow.  The code is fully commented and is ideal for educational purposes in schools for teaching road safety and basic Python coding.  It is free of charge to use and modify.

 

PyGlow is a small Python module for the PiGlow.  The Ben Lebherz fork of pyglow.py is used for this project.  https://github.com/benleb/PYGlow

Please contact me if you would like any support or assistance in your school (Doncaster and surrounding areas, UK) using the Raspberry Pi and PiGlow.

Click here to download 3way_junction.py as a Zip archive.

 

The 3way_junction.py script is show below.  This can be copied and pasted directly into a suitable editor such as Notepad++.

 

## PyGlow

## Python module to control Pimoroni’s PiGlow

## (http://shop.pimoroni.com/products/piglow)

##

## For further information, documentation and examples see:

## https://github.com/pimoroni/piglow#setting-up-your-raspberry-pi

## https://github.com/boeerb/PiGlow (Jason Barnett)

## https://github.com/benleb/PYGlow (Ben Lebherz)

##

## 3way_junction.py – George Jopling

## Simulated 3 way LED traffic light controlled road junction

## (http://www.georgejopling.com)(@GeorgeJoplingIT)

## For educational purposes.

##

# Imports.  (PyGlow.py is the Ben Lebherz fork)

from pyglow import PyGlow

from time import sleep

from random import randint

pyglow = PyGlow()

pyglow.all(0) # Extinguish all LEDs

pyglow.led (1,220) # Illuminate red LED on Arm 1

pyglow.led (7,220) # Illuminate red LED on Arm 2

pyglow.led (13,220) # Illuminate red LED on Arm 3

try:

    while True: # Begin loop (repeat until Ctr + C keypress)

        # Junction 1 (using Arm 1 LEDs on PiGlow)

        sleep(2.5) # Pause on red for 2.5 seconds

        pyglow.led (2,220) # Illuminate amber

        sleep(2) # Pause on red and amber for 2 seconds

        pyglow.led (1,0) # Extinguish red

        pyglow.led (2,0) # Extinguish amber

        pyglow.led (4,220) # Illuminate green

        # Simulate ‘traffic volume’ random timings at junction

        random_delay = randint(7,15) # Generate random pause (7-15) seconds

        sleep(random_delay) # Pause random_delay time

        pyglow.led (4,0) # Extinguish green

        pyglow.led (2,220) # Iluminate amber

        sleep(3) # Pause on amber for 3 seconds

        pyglow.led (2,0) # Extinguish amber

        pyglow.led (1,220) # Illuminate red

        # Junction 2 (using Arm 2 LEDs on PiGlow

        sleep(2.5) # Pause on red for 2.5 seconds

        pyglow.led (8,220) # Illuminate amber

        sleep(2) # Pause on red and amber for 2 seconds

        pyglow.led (7,0) # Extinguish red

        pyglow.led (8,0) # Extinguish amber

        pyglow.led (10,220) # Illuminate green

        # Simulate ‘traffic volume’ random timings at junction

        random_delay = randint(7,15) # Generate random pause (7-15) seconds

        sleep(random_delay) # Pause random_delay time

        pyglow.led (10,0) # Extinguish green

        pyglow.led (8,220) # Illuminate amber

        sleep(3) # Pause on amber for 3 seconds

        pyglow.led (8,0) # Extinguish amber

        pyglow.led (7,220) # Illuminate red

        # Junction 3 (using Arm 3 LEDs on PiGlow)

        sleep(2.5) # Pause on red for 2.5 seconds

        pyglow.led (14,220) # Illuminate amber

        sleep(2) # Pause on red and amber for 2 seconds

        pyglow.led (13,0) # Extinguish red

        pyglow.led (14,0) # Extinguish amber

        pyglow.led (16,220) # Illuminate green

        # Simulate ‘traffic volume’ random timings at junction

        random_delay = randint(7,15) # Generate random pause (7-15) seconds

        sleep(random_delay) # Pause random_delay time

        pyglow.led (16,0) # Extinguish green

        pyglow.led (14,220) # Illuminate amber

        sleep(3) # Pause on amber for 3 seconds

        pyglow.led (14,0) # Extinguish amber

        pyglow.led (13,220) # Illuminate red

except KeyboardInterrupt: # Catch Ctrl + C keypress

    pyglow.all(0) # Extinguish all LEDs then terminate

 

Below is a video of the 3-way junction script working.