Skip to content

Instantly share code, notes, and snippets.

@kcranley1
Last active August 29, 2015 14:09
Show Gist options
  • Save kcranley1/ec8a7594d8ab9c1f23e3 to your computer and use it in GitHub Desktop.
Save kcranley1/ec8a7594d8ab9c1f23e3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2.7
# script by Sparks N Smoke for SOFTWARE control of LEDs with Pulse Width Modulation
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.OUT) # LED Red PWM port - can use any GPIO port !
GPIO.setup(25, GPIO.OUT) # LED Green PWM port
GPIO.setup(26, GPIO.OUT) # LED Blue PWM port
frequency = 40 # set the LED pulse cycle frequency (40 Hertz)
delay = 0.02 # set no of secs to wait on duty cycle change
Red = GPIO.PWM(24, frequency) # set port 24 to 40 Hertz
Green = GPIO.PWM(25, frequency) # set port 13 to 40 Hertz
Blue = GPIO.PWM(26, frequency) # set port 18 to 40 Hertz
Red.start(0) # start with 0% duty cycle
Green.start(0)
Blue.start(0)
try:
while True:
for i in range (0, 101, 1): # 100 is the last value executed
Red.ChangeDutyCycle(i) # change duty cycle to the value of i (going UP)
sleep(delay) # linger a while at each step with RED
for i in range (100, -1, -1): # 0 is the last value executed
Red.ChangeDutyCycle(i) # value of duty cycle going DOWN
sleep(delay)
sleep(delay*5) # linger a bit longer with the LED off completely
for i in range (0, 101, 1): # now deal with GREEN
Green.ChangeDutyCycle(i)
sleep(delay)
for i in range (100, -1, -1):
Green.ChangeDutyCycle(i)
sleep(delay)
sleep(delay*5)
for i in range (0, 101, 1): # now deal with BLUE
Blue.ChangeDutyCycle(i)
sleep(delay)
for i in range (100, -1, -1):
Blue.ChangeDutyCycle(i)
sleep(delay)
sleep(delay*5)
except KeyboardInterrupt:
print " CTRL-C detected"
Red.stop()
Green.stop()
Blue.stop()
GPIO.cleanup() # Reset ports on CTRL-C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment