import RPi.GPIO as GPIO import time # set GPIO mode GPIO.setmode(GPIO.BCM) # set pin 18 as output pin GPIO.setup(18, GPIO.OUT) # blink LED while True: GPIO.output(18, GPIO.HIGH) time.sleep(1) GPIO.output(18, GPIO.LOW) time.sleep(1)
import RPi.GPIO as GPIO # set GPIO mode GPIO.setmode(GPIO.BCM) # set pin 23 as input pin with pull-up resistor GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) # detect button press while True: input_state = GPIO.input(23) if input_state == False: print('Button Pressed') # add button released debounce here while GPIO.input(23) == False: pass
import RPi.GPIO as GPIO import time # set GPIO mode GPIO.setmode(GPIO.BCM) # set pin 18 as output pin GPIO.setup(18, GPIO.OUT) # create PWM instance pwm = GPIO.PWM(18, 500) # set frequency to 500Hz # start PWM with duty cycle of 0% pwm.start(0) # fade LED in and out while True: for dc in range(0, 101, 5): pwm.ChangeDutyCycle(dc) time.sleep(0.1) for dc in range(100, -1, -5): pwm.ChangeDutyCycle(dc) time.sleep(0.1) # stop PWM pwm.stop()Package Library: RPi.GPIO