
- RGB Blue LED glowing PWM 7
- RGB_blue.jpg (24.06 KiB) Viewed 3027 times
The LEDs are just too bright normally... so, I decided to use PWM to control the intensity, as well as the color, of this cool RGB lamp/
I have included an updated version of
binary.py and
RGB_blink.sh below. I have selected channel 0 PWM GPIO18 (pin12) as the source pin. I moved the common anode wire from Vcc (3v3) to the PWM pin GPIO18.
Now, the PWM (pulse width modulation) duty cycle controls the source +voltage on the common anode thereby allowing us to dim the LED by reducing the duty_cycle. I have set the frequency to 440 (concert A) and the initial duty_cycle is passed to the
RGB_blink.sh script as a parameter.
The ballast resistors are 330 ohms for all three LEDs; consequently the Blue LED was considerably more 'dim' (less 'bright') than the other two... so, easily fixed, when the Blue LED is lit the c_on() function increases the duty_cycle by +25... now all three LEDs light with the same brightness !
I like the Blue LED in this setup... so I had it 'pose' for the pic above so I could get the focus right...
binary.py
Code: Select all
#################################################################
## binary.py header for BreadBoard (8) bit LED binary display
# v0.02c
#
# Mark H. Harris
# 5-31-2016
# Rochester, MN
#
#################################################################
##
# Import the GPIO library and set the mode to "board numbering"
#
from time import sleep
from sys import argv
import RPi.GPIO as gpio
gpio.setmode(gpio.BOARD)
# Define the binary pin dictionarys REV {reverse} STD {standard}
# note 1: GPIO #s in these dictionarys are text labels only.
# {index:[pin#,"GPIO#"]}
bin_rom_STD = {0:[37,"GPIO26"], 1:[35,"GPIO19"], 2:[33,"GPIO13"], 3:[32,"GPIO12"], 4:[18,"GPIO24"], 5:[15,"GPIO22"], 6:[13,"GPIO27"], 7:[11,"GPIO17"]}
bin_rom_REV = {7:[37,"GPIO26"], 6:[35,"GPIO19"], 5:[33,"GPIO13"], 4:[32,"GPIO12"], 3:[18,"GPIO24"], 2:[15,"GPIO22"], 1:[13,"GPIO27"], 0:[11,"GPIO17"]}
binH_rom_STD = {0:[18,"GPIO24"], 1:[15,"GPIO22"], 2:[13,"GPIO27"], 3:[11,"GPIO17"]}
binL_rom_STD = {0:[37,"GPIO26"], 1:[35,"GPIO19"], 2:[33,"GPIO13"], 3:[32,"GPIO12"]}
binH_rom_REV = {3:[18,"GPIO24"], 2:[15,"GPIO22"], 1:[13,"GPIO27"], 0:[11,"GPIO17"]}
binL_rom_REV = {3:[37,"GPIO26"], 2:[35,"GPIO19"], 1:[33,"GPIO13"], 0:[32,"GPIO12"]}
# Set the pinblock for PWM on GPIO18
#
gpio.setup(12, gpio.OUT)
p = gpio.PWM(12, 440)
p.stop()
# Set the pinblock for input on GPIO16
#
button1=36
gpio.setup(button1, gpio.IN, pull_up_down=gpio.PUD_UP)
# Set the pinblock for output on all eight dictionary pins
#
for n in range(8):
gpio.setup(bin_rom_STD[n][0],gpio.OUT)
## FUNCTION DEFINTIONS ##########################################
#
def push_button():
if gpio.input(button1):
return False
else:
return True
def led_on(pin):
gpio.output(pin, True)
def led_off(pin):
gpio.output(pin, False)
def test_leds(orient):
if (orient=="REV"):
pins=bin_rom_REV
else:
pins=bin_rom_STD
for n in range(8):
led_on(pins[n][0])
sleep(.68)
for n in range(7,-1,-1):
led_off(pins[n][0])
sleep(.68)
def bin_nibble_display(nval,high_low,orient):
if (orient=="REV"):
if (high_low=="H"):
pins=binH_rom_REV
else:
pins=binL_rom_REV
else:
if (high_low=="H"):
pins=binH_rom_STD
else:
pins=binL_rom_STD
for n in range(3,-1,-1):
if (nval & 2**n != 0):
led_on(pins[n][0])
else:
led_off(pins[n][0])
def nl(val):
bin_nibble_display(val,"L","REV")
def nh(val):
bin_nibble_display(val,"H","REV")
def binary_display(nval,orient):
if (orient=="REV"):
pins=bin_rom_REV
else:
pins=bin_rom_STD
for n in range(7,-1,-1):
if (nval & 2**n != 0):
led_on(pins[n][0])
else:
led_off(pins[n][0])
def bd(val):
binary_display(val,"REV")
def all_off():
nl(0x0)
nh(0x0)
def all_on():
bd(0xff)
def p_count(orient,t_delay):
button=False
for n in range(1,256,1):
binary_display(n,orient)
sleep(t_delay)
if push_button():
button=True
break
return button
def t_count(orient,t_delay):
for n in range(1,256,1):
binary_display(n,orient)
sleep(t_delay)
if push_button():
sleep(2)
break
def counter(maxVal,orient,t_delay):
for n in range(1,maxVal,1):
binary_display(n,orient)
sleep(t_delay)
def count(orient,t_delay):
for n in range(1,256,1):
binary_display(n,orient)
sleep(t_delay)
def walk(orient,t_delay):
for n in range(8):
binary_display(2**n,orient)
sleep(t_delay)
all_off()
def dsp_rom(high_low,orient):
if (orient=="REV"):
if (high_low=="H"):
pins=binH_rom_REV
else:
pins=binL_rom_REV
else:
if (high_low=="H"):
pins=binH_rom_STD
else:
pins=binL_rom_STD
for n in range(4):
print(pins[n][1]," BRD#:",pins[n][0]," index:",n)
def dsp_bin_rom():
print("L------------------------------------------")
dsp_rom("L","REV")
print("H------------------------------------------")
dsp_rom("H","REV")
def end():
gpio.cleanup()
quit()
RGB_blink.sh
Code: Select all
#!/usr/bin/python
from binary import *
from time import sleep
import signal, os
Red = binH_rom_STD[1][0]
Green = binH_rom_STD[2][0]
Blue = binH_rom_STD[3][0]
colors = [Red, Green, Blue]
def c_on(color, duty_cycle):
if(color==Blue):
p.ChangeDutyCycle(duty_cycle+25)
else:
p.ChangeDutyCycle(duty_cycle)
led_off(color)
def c_off(color, duty_cycle):
led_on(color)
def c_none(colors):
for color in colors:
c_off(color, 50)
c_none(colors)
def rgb_blink(colors, duty_cycle):
for color in colors:
c_on(color, duty_cycle)
sleep(0.713)
c_off(color, duty_cycle)
sleep(0.140)
def blinker(duty_cycle):
p.start(duty_cycle)
while(1):
rgb_blink(colors, duty_cycle)
sleep(0.77)
def handler(signum, frame):
c_none(colors)
sleep(.47)
for n in range(7):
c_on(Red, 50)
sleep(.256)
c_off(Red, 50)
sleep(.256)
signal.signal(signal.SIGHUP, handler)
try:
duty_cycle=int(argv[1])
blinker(duty_cycle)
except KeyboardInterrupt:
print(" ")
print("blinker ended by interrupt, bye!")
except:
print(" ")
print("unmonitored exception in rgb_blink()")
print("check args: ./RGB_blink.sh <duty_cycle> ie: ./RGB_blink.sh 12")
finally:
p.stop()
gpio.cleanup()
marcus