My Account

Wish List (0)

The PiBorg store is closed and we are unable to ship orders.
You will not be able to checkout any orders at this time.

LEDBorg - Examples - Colour Selector

Written by in Build, LEDBorg - Build on .

This example uses the standard GTK colour selection dialog to provide a user-friendly control for selecting LedBorg colours.
The colour of the LedBorg changes as you change the selection on the dialog, as a bonus most of the GUI code is done for us in this example ^_^

You can download the code directly to your Raspberry Pi using:
cd ~/ledborg
wget -O
Or save the text file from the link on your Raspberry Pi as

Make the script executable using
chmod +x
ensure you have pygtk installed using
sudo apt-get -y install python-gtk2
and run using
gksudo ./

You will need to run this script in a graphical environment, otherwise you will get an error message.

Full code listing

#!/usr/bin/env python
# coding: latin-1

# Import library functions we need
import wiringpi2 as wiringpi
import pygtk
import gtk

# Setup software PWMs on the GPIO pins
LED_MAX = 100
wiringpi.softPwmCreate(PIN_RED,   0, LED_MAX)
wiringpi.softPwmCreate(PIN_GREEN, 0, LED_MAX)
wiringpi.softPwmCreate(PIN_BLUE,  0, LED_MAX)
wiringpi.softPwmWrite(PIN_RED,   0)
wiringpi.softPwmWrite(PIN_GREEN, 0)
wiringpi.softPwmWrite(PIN_BLUE,  0)

# A function to set the LedBorg colours
def SetLedBorg(red, green, blue):
    wiringpi.softPwmWrite(PIN_RED,   int(red   * LED_MAX))
    wiringpi.softPwmWrite(PIN_GREEN, int(green * LED_MAX))
    wiringpi.softPwmWrite(PIN_BLUE,  int(blue  * LED_MAX))

# A function to turn the LedBorg off
def LedBorgOff():
    SetLedBorg(0, 0, 0)
# A function to handle the colour selection dialog change event
def ColourChangedEvent(widget):
    global colourSelectionDialog

    # Get current colour
    colour = colourSelectionDialog.colorsel.get_current_color()

    # Read the red, green, and blue levels to use
    red = colour.red_float
    green = colour.green_float
    blue = colour.blue_float

    # Set the LedBorg colours
    SetLedBorg(red, green, blue)

# Create colour selection dialog
global colourSelectionDialog
colourSelectionDialog = gtk.ColorSelectionDialog("Select LedBorg colour")

# Get the ColorSelection widget
colourSelection = colourSelectionDialog.colorsel

# Connect to the "color_changed" signal to our function
colourSelection.connect("color_changed", ColourChangedEvent)

# Show the dialog
response =

# Turn LedBorg off after we are finished
Last update: Oct 27, 2017

Related Article

Related Products


Leave a Comment

Leave a Reply

The product is currently Out-of-Stock. Enter your email address below and we will notify you as soon as the product is available.