#################################################
## Pi Pico - 74HC595 - 14 LEDS ##################
## Version 1.10.61 ##############################
## By: Corey Beck ###############################
#################################################
#### CATHODE RGB LEDS ###########################
#################################################
from machine import Pin
import time
from utils import ShiftedRGB, ShiftedRGBValues
# Pin assignments for Raspberry Pi Pico
########################################
## Shift Register 1 ####################
####################
DATA_PIN = 16 # DS
CLOCK_PIN = 18 # SHCP
LATCH_PIN = 17 # STCP
# Q0 Pin - R Pin - RGB LED 1
# Q1 Pin - G Pin - RGB LED 1
# Q2 Pin - B Pin - RGB LED 1
# Q3 Pin - R Pin - RGB LED 2
# Q4 Pin - G Pin - RGB LED 2
# Q5 Pin - B Pin - RGB LED 2
# Q6 Pin - R Pin - RGB LED 3
# Q7 Pin - G Pin - RGB LED 3
########################################
## Shift Register 2 ####################
####################
# Q0 Pin - B Pin - RGB LED 3
# Q1 Pin - R Pin - RGB LED 4
# Q2 Pin - G Pin - RGB LED 4
# Q3 Pin - B Pin - RGB LED 4
# Q4 Pin - R Pin - RGB LED 5
# Q5 Pin - G Pin - RGB LED 5
# Q6 Pin - B Pin - RGB LED 5
# Q7 Pin - R Pin - RGB LED 6
########################################
## Shift Register 3 ####################
####################
# Q0 Pin - G Pin - RGB LED 6
# Q1 Pin - B Pin - RGB LED 6
# Q2 Pin - A Pin - LED(Yellow)
# Q3 Pin - R Pin - RGB LED 7
# Q4 Pin - G Pin - RGB LED 7
# Q5 Pin - B Pin - RGB LED 7
# Q6 Pin - R Pin - RGB LED 8
# Q7 Pin - G Pin - RGB LED 8
########################################
## Shift Register 4 ####################
####################
# Q0 Pin - B Pin - RGB LED 8
# Q1 Pin - R Pin - RGB LED 9
# Q2 Pin - G Pin - RGB LED 9
# Q3 Pin - B Pin - RGB LED 9
# Q4 Pin - R Pin - RGB LED 10
# Q5 Pin - G Pin - RGB LED 10
# Q6 Pin - B Pin - RGB LED 10
# Q7 Pin - R Pin - RGB LED 11
########################################
## Shift Register 5 ####################
####################
# Q0 Pin - G Pin - RGB LED 11
# Q1 Pin - B Pin - RGB LED 11
# Q2 Pin - R Pin - RGB LED 12
# Q3 Pin - G Pin - RGB LED 12
# Q4 Pin - B Pin - RGB LED 12
# Q5 Pin - R Pin - RGB LED 13
# Q6 Pin - G Pin - RGB LED 13
# Q7 Pin - B Pin - RGB LED 13
# Setup pins
data = Pin(DATA_PIN, Pin.OUT)
clock = Pin(CLOCK_PIN, Pin.OUT)
latch = Pin(LATCH_PIN, Pin.OUT)
# only shifts register doesn't latch
def shift_out(byte_val):
# Send 40 bits to 74HC595 (MSB first).
for i in range(39, -1, -1):
bit = (byte_val >> i) & 1
data.value(bit)
clock.value(1)
clock.value(0)
# Updates all LEDS and shifts value to register
# rgb1-13 = ShiftedRGB([Red,Green,Blue])
# default = ShiftedRGB.OFF() = RGB LED off
# led - single yellow led
def update_leds(rgb1=ShiftedRGB.OFF(), rgb2=ShiftedRGB.OFF(), rgb3=ShiftedRGB.OFF(), rgb4=ShiftedRGB.OFF(), rgb5=ShiftedRGB.OFF(), rgb6=ShiftedRGB.OFF(), rgb7=ShiftedRGB.OFF(), rgb8=ShiftedRGB.OFF(), rgb9=ShiftedRGB.OFF(), rgb10=ShiftedRGB.OFF(), rgb11=ShiftedRGB.OFF(), rgb12=ShiftedRGB.OFF(), rgb13=ShiftedRGB.OFF(), led=False):
latch.value(0)
rgbleds = (rgb1,rgb2,rgb3,rgb4,rgb5,rgb6,rgb7,led,rgb8,rgb9,rgb10,rgb11,rgb12,rgb13)
value = 0
bitindex = 0
# Have 14 LEDS 13 rgb and 1 yellow led
for xled in range(0,14,1):
if bitindex == 21:
# yellow led
value |= (int(rgbleds[xled]) << bitindex)
bitindex += 1
else:
# rgb led
value |= rgbleds[xled].bitshift(bitindex)
bitindex += rgbleds[xled].length()
######## COMMENT THIS OUT TO IGNORE PRINT ############
#print("shift out to registers: {}".format(bin(value)))
shift_out(value)
latch.value(1)
# Test sequence
# COLORS
# use these color objects to change RGB led for rgb values in update_leds(rgb1..rgb13)
# ShiftedRGB.RED()
# ShiftedRGB.GREEN()
# ShiftedRGB.BLUE()
# ShiftedRGB.YELLOW()
# ShiftedRGB.CYAN()
# ShiftedRGB.MAGENTA()
# ShiftedRGB.WHITE()
# ShiftedRGB.OFF()
# Example: to change only led 1 to green
# all of the other 13 LEDs will be off
#
# update_leds(rgb1=ShiftedRGB.GREEN())
# Example: to change led 1 to red and led 13 to green
# all of the other 12 LEDs will be off
#
# update_leds(rgb1=ShiftedRGB.RED(),rgb13=ShiftedRGB.GREEN())
#
# using utils.ShiftedRGBValues return an array of ShiftedRGB
colors = ShiftedRGBValues.toarray(shiftedrgb=True, includeoff=False)
try:
while True:
# Used to control led(yellow)
led_yellow = True
# Loop Thru Patterns
# for reverse uncomment and comment out either for loop
# -> forwards
for x in range(7, -1, -1):
# -> backwards
#for x in range(0, 7, 1):
if x == 7:
# Pattern #1
backcolor = ShiftedRGB.OFF()
for colorb in colors:
forecolor = colorb
if forecolor.bitshift() == backcolor.bitshift(): break
for ledx in range(12, -1, -1):
if led_yellow: led_yellow = False
else: led_yellow = True
patternarr = [backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor]
patternarr[ledx] = forecolor
update_leds(patternarr[0], patternarr[1], patternarr[2], patternarr[3], patternarr[4], patternarr[5], patternarr[6], patternarr[7], patternarr[8], patternarr[9], patternarr[10], patternarr[11], patternarr[12], led_yellow)
time.sleep(0.16)
elif x == 6:
# Pattern #2
backcolor = ShiftedRGB.OFF()
for colorb in colors:
forecolor = colorb
if forecolor.bitshift() == backcolor.bitshift(): break
for ledx in range(0, 13, 1):
if led_yellow: led_yellow = False
else: led_yellow = True
patternarr = [backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor]
patternarr[ledx] = forecolor
update_leds(patternarr[0], patternarr[1], patternarr[2], patternarr[3], patternarr[4], patternarr[5], patternarr[6], patternarr[7], patternarr[8], patternarr[9], patternarr[10], patternarr[11], patternarr[12], led_yellow)
time.sleep(0.16)
elif x == 5:
# Pattern #3
backcolor = ShiftedRGB.WHITE()
for colora in colors:
backcolor = colora
for colorb in colors:
forecolor = colorb
if forecolor.bitshift() == backcolor.bitshift(): break
for ledx in range(0, 13, 1):
if led_yellow: led_yellow = False
else: led_yellow = True
patternarr = [backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor]
patternarr[ledx] = forecolor
update_leds(patternarr[0], patternarr[1], patternarr[2], patternarr[3], patternarr[4], patternarr[5], patternarr[6], patternarr[7], patternarr[8], patternarr[9], patternarr[10], patternarr[11], patternarr[12], led_yellow)
time.sleep(0.16)
elif x == 4:
# Pattern #4
backcolor = ShiftedRGB.WHITE()
for colora in colors:
backcolor = colora
for colorb in colors:
forecolor = colorb
if forecolor.bitshift() == backcolor.bitshift(): break
for ledx in range(12, -1, -1):
if led_yellow: led_yellow = False
else: led_yellow = True
patternarr = [backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor, backcolor]
patternarr[ledx] = forecolor
update_leds(patternarr[0], patternarr[1], patternarr[2], patternarr[3], patternarr[4], patternarr[5], patternarr[6], patternarr[7], patternarr[8], patternarr[9], patternarr[10], patternarr[11], patternarr[12], led_yellow)
time.sleep(0.16)
elif x == 3:
# Pattern #5
for colora in colors:
for colorb in colors:
for colorc in colors:
for colord in colors:
if led_yellow: led_yellow = False
else: led_yellow = True
update_leds(colora, colora, colorb, colorb, colorc, colorc, colord, colorc, colorc, colorb, colorb, colora, colora, led_yellow)
# rbg1, rgb2, rgb3, rgb4, rgb5, rgb6, rgb7, rgb8, rgb9, rgb10, rgb11, rgb12, rgb13, led
time.sleep(0.2)
elif x == 2:
# Pattern #6
for colora in colors:
for colorb in colors:
for colorc in colors:
for colord in colors:
if led_yellow: led_yellow = False
else: led_yellow = True
update_leds(colord, colord, colorc, colorc, colorb, colorb, colora, colorb, colorb, colorc, colorc, colord, colord, led_yellow)
# rbg1, rgb2, rgb3, rgb4, rgb5, rgb6, rgb7, rgb8, rgb9, rgb10, rgb11, rgb12, rgb13, led
time.sleep(0.2)
elif x == 1:
# Pattern #7
for colora in colors:
for colorb in colors:
for colorc in colors:
for colord in colors:
for colore in colors:
for colorf in colors:
for colorg in colors:
if led_yellow: led_yellow = False
else: led_yellow = True
update_leds(colora, colora, colorb, colorb, colorc, colorc, colord, colord, colore, colore, colorf, colorf, colorg, led_yellow)
# rbg1, rgb2, rgb3, rgb4, rgb5, rgb6, rgb7, rgb8, rgb9, rgb10, rgb11, rgb12, rgb13, led
time.sleep(0.19)
else:
# Pattern #8
for colora in colors:
for colorb in colors:
for colorc in colors:
for colord in colors:
for colore in colors:
for colorf in colors:
for colorg in colors:
if led_yellow: led_yellow = False
else: led_yellow = True
update_leds(colorg, colorf, colorf, colore, colore, colord, colord, colorc, colorc, colorb, colorb, colora, colora, led_yellow)
# rbg1, rgb2, rgb3, rgb4, rgb5, rgb6, rgb7, rgb8, rgb9, rgb10, rgb11, rgb12, rgb13, led
time.sleep(0.19)
except KeyboardInterrupt:
print("error")
update_leds() # Turn off LEDS
We Use 5 Shift Registers(8bit) to
control 13 individual RGB LEDs(3bit).
and 1 LED(1bit)
The reason this is important cause
we only use 3 GP Pins of the PI Pico
to control 40 shift register Pins(40bit)
or 13 RGB LEDS and 1 LED(40 pins)