I'm having trouble with the RPIO.PWM library. I'm working up to controlling a strip of Common Anode RGB LEDs but for now I'm just trying to figure out the PWM library. I have the LED Anode wired to the Pi (model B Rev 2) +3V (P1-1) and the LED Red, Grn and Blu Cathodes wired through current limiting resistors (330 ohm) to GPIO 17, 27, 21 (P1-11, 13, 15). This should result in the LED being off when the GPIO pin is high.
Here's the code I wrote
Code: Select all
## Import PWM Library.
from RPIO import PWM
## PWM Increment usec. Default = 10 usec.
PWM_INCR = 10
## Init PWM Library.
PWM.setup(PWM_INCR)
## PWM DMA Channel.
LED_DMA = 0
## PWM Cycle usec. Default = 20000 usec.
PWM_CYCLE = 20000
## Init PWM channel.
PWM.init_channel(LED_DMA, PWM_CYCLE + PWM_INCR)
## RefreshLED.
## redPWM, grnPWM and bluPWM percentage (0 (0%) to 1 (100%))
def RefreshLED(redPWM, grnPWM, bluPWM):
## LED GPIO Pins.
LED_RED_GPIO = 17
LED_GRN_GPIO = 27
LED_BLU_GPIO = 22
## Clear last PWM.
PWM.clear_channel(LED_DMA)
## Refresh LED. Inverted for common anode LED.
PWM.add_channel_pulse(LED_DMA, LED_RED_GPIO, 0, int((PWM_CYCLE - (PWM_CYCLE * redPWM)) / PWM_INCR))
PWM.add_channel_pulse(LED_DMA, LED_GRN_GPIO, 0, int((PWM_CYCLE - (PWM_CYCLE * grnPWM)) / PWM_INCR))
PWM.add_channel_pulse(LED_DMA, LED_BLU_GPIO, 0, int((PWM_CYCLE - (PWM_CYCLE * bluPWM)) / PWM_INCR))
## Test LED
## Import 'time' library. Allows us to use 'sleep'
import time
## LED 100%
RefreshLED(1, 1, 1)
time.sleep(5)
## LED 0%
RefreshLED(0, 0, 0)
time.sleep(5)
## LED 100%
RefreshLED(1, 1, 1)
time.sleep(5)
## LED 0%
RefreshLED(0, 0, 0)
time.sleep(5)
The LED lights up but it doesn't change when I run the program:
pi@raspberrypi:/home/pi$ sudo python pwmrgbled.py
Using hardware: PWM
PW increments: 10us
Initializing channel 0...
clear_channel: channel=0
add_channel_pulse: channel=0, gpio=17, start=0, width=0
init_gpio 17
add_channel_pulse: channel=0, gpio=27, start=0, width=0
init_gpio 27
add_channel_pulse: channel=0, gpio=22, start=0, width=0
init_gpio 22
clear_channel: channel=0
add_channel_pulse: channel=0, gpio=17, start=0, width=2000
add_channel_pulse: channel=0, gpio=27, start=0, width=2000
add_channel_pulse: channel=0, gpio=22, start=0, width=2000
clear_channel: channel=0
add_channel_pulse: channel=0, gpio=17, start=0, width=0
add_channel_pulse: channel=0, gpio=27, start=0, width=0
add_channel_pulse: channel=0, gpio=22, start=0, width=0
shutting down dma channel 0
clear_channel: channel=0
pi@raspberrypi:/home/pi$
I've done this before with Arduino but this is my first attempt with a Pi so be on the lookout for noob blunders. :

: