Skip to content

Instantly share code, notes, and snippets.

@ritiek
Last active February 3, 2020 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritiek/f14d9419d5182c6936bc70b5b2c28048 to your computer and use it in GitHub Desktop.
Save ritiek/f14d9419d5182c6936bc70b5b2c28048 to your computer and use it in GitHub Desktop.
Tests an LED connected via GPIO to Raspberry Pi with software defined PWM
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import sys
out_pin = int(sys.argv[1])
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(out_pin, GPIO.OUT)
time_off = 0.001
def output(out_pin, duty_cycle, time_off):
time_on = (time_off*int(duty_cycle))/(100-int(duty_cycle))
time.sleep(time_off)
GPIO.output(out_pin, GPIO.HIGH)
time.sleep(time_on)
GPIO.output(out_pin, GPIO.LOW)
time.sleep(0.001)
while True:
for duty_cycle in range(100):
output(out_pin, duty_cycle, time_off)
for duty_cycle in range(99, -1, -1):
output(out_pin, duty_cycle, time_off)
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment