# Blink an LED on pin 18. # Connect a low-ohm (like 360 ohm) resistor in series with the LED. import RPi.GPIO as GPIO import time # A variable so we can change the PIN number for this script in once place # if we move the LED to a different pin. PIN = 18 # Set the pin to do output GPIO.setmode(GPIO.BCM) GPIO.setup(PIN, GPIO.OUT) # Loop forever blinking our led while True: # Switch the pin off for half of a second GPIO.output(PIN, 0) time.sleep(.5) # Now turn it back on GPIO.output(PIN, 1) time.sleep(.5) import time import RPi.GPIO as GPIO # Set our pin to output mode GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT) # Set the PWM frequency in Hz (cycles per second) p = GPIO.PWM(12, 60) # channel=12 frequency=60Hz # Start doing PWM p.start(0) try: # Loop forever while True: # Increase the duty cycle (percentage of time the pin is on) from # 1% to 100%. This will cause the LED to get brighter for dc in range(0, 101, 1): p.ChangeDutyCycle(dc) time.sleep(0.05) # Now decrease the duty cycle (time the pin is on) back to 1. This will # cause the LED to dim for dc in range(100, -1, -1): p.ChangeDutyCycle(dc) time.sleep(0.05) except KeyboardInterrupt: pass p.stop() GPIO.cleanup() #!/usr/bin/python3 import RPi.GPIO as GPIO import time import sys from range_finder import RangeFinder # Set our pin to output mode GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT) # Set the PWM frequency in Hz (cycles per second) p = GPIO.PWM(12, 60) # channel=12 frequency=60Hz # Start doing PWM p.start(0) if __name__ == '__main__': sensor = RangeFinder() print('Running main') while True: percent = sensor.average_distance_percent print(100-percent) if percent == 100: p.ChangeDutyCycle(1) elif percent < 100: p.ChangeDutyCycle(100-percent) time.sleep(.1) import RPi.GPIO as GPIO import time # A variable so we can change the PIN number for this script in once place # if we move the LED to a different pin. PIN = 7 # Set the pin to do output GPIO.setmode(GPIO.BCM) GPIO.setup(PIN, GPIO.OUT) # Loop forever blinking our led while True: # Switch the pin off for half of a second GPIO.output(PIN, 0) time.sleep(5) # Now turn it back on GPIO.output(PIN, 1) time.sleep(5) GPIO.cleanup() import RPi.GPIO as GPIO import time # A variable so we can change the PIN number for this script in once place # if we move the LED to a different pin. PIN = 7 # Set the pin to do output GPIO.setmode(GPIO.BCM) GPIO.setup(PIN, GPIO.OUT) GPIO.output(PIN, 1) #GPIO.cleanup()