RGB-LED Lesson 0 – Extra Credit Solution

Extra Credit:

The following exercises can be performed using both Bash and Python.

To create a loop using Bash, the following code can be used:

for i in 1 2 3 4 5 6 7 8 9 10
do
  #write output
  echo "$RGB_ENABLE" > /sys/class/gpio/gpio$LED_RED/value
  echo "$RGB_DISABLE" > /sys/class/gpio/gpio$LED_RED/value
done

To create a loop using Python, the following code can be used:

for i in range(10):
  led_activate(LED1,RGB_RED)
  time.sleep(5)
  led_deactivate(LED1,RGB_RED)
  time.sleep(1)

1. Try extending each of the examples to cycle through the colours and each LED in turn.

See below.

2. Try using a combination of RGB to create more colours.

You can combine the RGB values to create the following colours (just enable more than one colour at a time).

RGB-LED Colours Table

RGB-LED Colours Table

Colour Cycle Solution:

1. Try extending each of the examples to cycle through the colours and each LED in turn.

There are several ways to achieve this and any number of patterns you could make, however, to illustrate I shall produce the following sequence (you may notice this “almost” matches the sequence shown in the Demo Video):

LED1: Red > Green > Blue > White

LED2: Red > Green > Blue > White

LED3, LED4, LED5 etc.

All LEDs: Red > Green > Blue > White

Demo video (with a similar sequence shown):

Loopy Solution:

Rather than show the code yet, first lets look at what we need to do:

# We are going to loop through each LED 1 to 5 to do the first part.

  • For each LED:
    • For each colour
      • We need to enable the LED and the corresponding colour.
      • Then wait for as long as we want it lit.
      • Then disable the LED and the corresponding colour.
    • Do the above for the next colour
    • Enable the LED and each of the colours.
    • Wait again
    • Disable the LED and the colours.
  • Do the above for the next LED

# We are going to loop through each of the colours to do the second part.

  • For each colour:
    • Enable all of the LEDs and the corresponding colour.
    • Wait
    • Disable all of the LEDs and the corresponding colour.
  • Do the above for all of the colours

# The last part (all LEDs white) we just switch everything on!

  • Enable all of the LEDs and all of the colours
  • Wait
  • Disable all of the LEDs and all of the colours.

The best way to achieve all of that, is to gradually build up each section, testing as you go.  It may look rather complicated, but if you break the problem down, you should be able to identify each step in the process.  In many cases here, we are disabling LEDs and re-enabling the LED almost immediately afterwards, it may seem inefficient but it avoids potential problems if you decide to swap the sequence around.

As you will find out in the next lesson, complex sequences can be produced by creating smaller functions to perform repeated and common operations.

For Bash (rgbledcycle.sh):

#!/bin/sh
# Bash uses BCM GPIO numbers (i.e. the pin names of the Broadcom Chip itself)
# These are detailed in the Raspberry Pi wiki pages.
# Therefore to make life easier we will map them to the Raspberry Pi GPIO
# Pin Numbers.
PIN12=18
PIN16=23
PIN18=24
PIN22=25
PIN7=4
PIN11=17
PIN13_REV1=21
PIN13_REV2=27
PIN15=22

#Setup Pins
LED01=$PIN12
LED02=$PIN16
LED03=$PIN18
LED04=$PIN22
LED05=$PIN7
LED_RED=$PIN11
LED_GREEN=$PIN13_REV2 #Rev2.0 (Ensure you use correct one for your board)
LED_BLUE=$PIN15

#Setup Active states
#Common Cathode RGB-LEDs (Cathode=Active Low)
LED_ENABLE=0
LED_DISABLE=1
RGB_ENABLE=1
RGB_DISABLE=0

PEROID=0.5s

f_ledcycle(){
  echo "Cycle colour on each LED"
  for val_led in $LED01 $LED02 $LED03 $LED04 $LED05
  do
    for val_rgb in $LED_RED $LED_GREEN $LED_BLUE
    do
      #Switch LED on
      echo "$RGB_ENABLE" > /sys/class/gpio/gpio$val_rgb/value
      echo "$LED_ENABLE" > /sys/class/gpio/gpio$val_led/value
      #Wait
      sleep $PEROID
      #Switch LED off
      echo "$LED_DISABLE" > /sys/class/gpio/gpio$val_led/value
      echo "$RGB_DISABLE" > /sys/class/gpio/gpio$val_rgb/value
    done
    #Switch on all RGB (white)
    echo "$LED_ENABLE" > /sys/class/gpio/gpio$val_led/value
    for val_rgb in $LED_RED $LED_GREEN $LED_BLUE
    do
      echo "$RGB_ENABLE" > /sys/class/gpio/gpio$val_rgb/value
    done
    sleep $PEROID
    #Switch off
    echo "$LED_DISABLE" > /sys/class/gpio/gpio$val_led/value
    for val_rgb in $LED_RED $LED_GREEN $LED_BLUE
    do
      echo "$RGB_DISABLE" > /sys/class/gpio/gpio$val_rgb/value
    done
  done
  echo "Cycle colour on all LEDs"
  for val_rgb in $LED_RED $LED_GREEN $LED_BLUE
  do
    for val_led in $LED01 $LED02 $LED03 $LED04 $LED05
    do
      #Switch LED on
      echo "$RGB_ENABLE" > /sys/class/gpio/gpio$val_rgb/value
      echo "$LED_ENABLE" > /sys/class/gpio/gpio$val_led/value
    done
    #Wait
    sleep $PEROID
    for val_led in $LED01 $LED02 $LED03 $LED04 $LED05
    do
      #Switch LED off
      echo "$LED_DISABLE" > /sys/class/gpio/gpio$val_led/value
      echo "$RGB_DISABLE" > /sys/class/gpio/gpio$val_rgb/value
    done
  done
  #Switch on all LEDs and all RGB (white)
  for val_led in $LED01 $LED02 $LED03 $LED04 $LED05
  do
    #Switch LED on
    for val_rgb in $LED_RED $LED_GREEN $LED_BLUE
    do
      echo "$RGB_ENABLE" > /sys/class/gpio/gpio$val_rgb/value
    done
    echo "$LED_ENABLE" > /sys/class/gpio/gpio$val_led/value
  done
  #Wait
  sleep $PEROID
  for val_led in $LED01 $LED02 $LED03 $LED04 $LED05
  do
    #Switch LED off
    for val_rgb in $LED_RED $LED_GREEN $LED_BLUE
    do
      echo "$RGB_DISABLE" > /sys/class/gpio/gpio$val_rgb/value
    done
    echo "$LED_DISABLE" > /sys/class/gpio/gpio$val_led/value
  done
}

#Set up GPIO to Outputs
# Set up GPIO Pin, to output and put in disable state
for val_led in $LED01 $LED02 $LED03 $LED04 $LED05
do
  echo "$val_led" > /sys/class/gpio/export
  echo "out" > /sys/class/gpio/gpio$val_led/direction
  echo "$LED_DISABLE" > /sys/class/gpio/gpio$val_led/value
done
for val_rgb in $LED_RED $LED_GREEN $LED_BLUE
do
  echo "$val_rgb" > /sys/class/gpio/export
  echo "out" > /sys/class/gpio/gpio$val_rgb/direction
  echo "$RGB_DISABLE" > /sys/class/gpio/gpio$val_rgb/value
done
# Setup End
############################################################

# Call LED Cycle fuction
f_ledcycle

############################################################
# Clean Up
for val_all in $LED01 $LED02 $LED03 $LED04 $LED05 $LED_RED $LED_GREEN $LED_BLUE
do
  echo "$val_all" > /sys/class/gpio/unexport
done
#End

For Python:

#!/usr/bin/python
import time
import RPi.GPIO as GPIO
from array import *

# RGB LED Module (TEST)

#Setup Active states
#Common Cathode RGB-LEDs (Cathode=Active Low)
LED_ENABLE = 0
LED_DISABLE = 1
RGB_ENABLE = 1
RGB_DISABLE = 0

#LED CONFIG - Set GPIO Ports
LED1 = 12 #B4
LED2 = 16 #B18
LED3 = 18 #B23
LED4 = 22 #B24
LED5 = 7 #B25
LED = [LED1,LED2,LED3,LED4,LED5]
RGB_RED = 11 #B22
RGB_GREEN = 13 #B21 Rev1 B27 Rev2
RGB_BLUE = 15 #B17
RGB = [RGB_RED,RGB_GREEN,RGB_BLUE]

peroid = 0.5 #light for 1/2 second

def led_setup():
  #Set up the wiring
  GPIO.setmode(GPIO.BOARD)
  # Setup Ports
  for val in LED:
    GPIO.setup(val, GPIO.OUT)
  for val in RGB:
    GPIO.setup(val, GPIO.OUT)

def led_activate(led,colour):
  GPIO.output(led,LED_ENABLE)
  GPIO.output(colour,RGB_ENABLE)

def led_deactivate(led,colour):
  GPIO.output(led,LED_DISABLE)
  GPIO.output(colour,RGB_DISABLE)

def led_clear():
  for val in LED:
    GPIO.output(val, LED_DISABLE)
  for val in RGB:
    GPIO.output(val, RGB_DISABLE)

def led_cycle():
  #Cycle each colour on each led in turn
  for val_led in LED:
    for val_rgb in RGB:
      led_activate(val_led,val_rgb)
      time.sleep(peroid)
      led_deactivate(val_led,val_rgb)
    #All RGB (white)
    for val_rgb in RGB:
      led_activate(val_led,val_rgb)
    time.sleep(peroid)
    for val_rgb in RGB:
      led_deactivate(val_led,val_rgb)

  time.sleep(peroid)

  #Cycle each colour on all the leds
  for val_rgb in RGB:
    for val_led in LED:
      led_activate(val_led,val_rgb)
    time.sleep(peroid)
    for val_led in LED:
      led_deactivate(val_led,val_rgb)
  #All RGB (white)
  for val_led in LED:
    for val_rgb in RGB:
      led_activate(val_led,val_rgb)
  time.sleep(peroid)
  for val_led in LED:
    for val_rgb in RGB:
      led_deactivate(val_led,val_rgb)

def main():
  led_setup()
  led_clear()
  led_cycle() #Cycle through the colours
  led_clear()
  GPIO.cleanup()

main()
#End

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.