class gpiozero.
LED
(pin, *, active_high=True, initial_value=False, pin_factory=None)
Blink.py is a while loop to blink the LED every one second.
# basic_i2c_configuration
import RPi.GPIO as GPIO
import time
from smbus import SMBus
bus = SMBus(1) # indicates /dev/ic2-1
def destroy():
GPIO.cleanup()
def setup():
while True:
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
red = 24
# GPIO.setmode(GPIO.BOARD)
# red = 18
GPIO.setup(red,GPIO.OUT)
GPIO.output(red,1)
time.sleep(1)
GPIO.output(red,0)
time.sleep(1)
def loop():
print("")
if __name__ == '__main__':
print ('Program is starting...' )
setup()
try:
loop()
except KeyboardInterrupt:
destroy()
Five methods of blinking an LED. I placed a Red LED on GPIO 24 and Blue LED on GPIO 18. I could have done the same connection through the single supply logic level converter. It could have been 5V for the LED instead of 3.3V.
# basic_i2c_configuration
import RPi.GPIO as GPIO
import time
from smbus import SMBus
bus = SMBus(1) # indicates /dev/ic2-1
def destroy():
GPIO.cleanup()
def setup():
while True:
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
red = 24
# GPIO.setmode(GPIO.BOARD)
# red = 18
GPIO.setup(red,GPIO.OUT)
GPIO.output(red,1)
time.sleep(1)
GPIO.output(red,0)
time.sleep(1)
def loop():
print("")
if __name__ == '__main__':
print ('Program is starting...' )
setup()
try:
loop()
except KeyboardInterrupt:
destroy()
michael@wpmm22:/var/www/mc.scsiraidguru.com/public_html/raspberry_pi/pi_diode/light_emitting$
michael@wpmm22:/var/www/mc.scsiraidguru.com/public_html/raspberry_pi/pi_diode/light_emitting$ ls
blink.jpg blink.py five_method_blink.py
michael@wpmm22:/var/www/mc.scsiraidguru.com/public_html/raspberry_pi/pi_diode/light_emitting$ sudo cat five_method_blink.py
from gpiozero import LED
from time import sleep
from signal import pause
print("init LCD")
led_red = LED(24)
print("Start Blinking LED 10x")
for x in range(0, 11):
print("Turn on LED")
led_red.on()
sleep(1)
print("Turn off LED")
led_red.off()
sleep(1)
print("Second method setting active_high to false blinking 10x")
led_blue = LED(18, active_high=False)
for x in range(0, 11):
print("Turn on LED")
led_blue.on()
sleep(1)
print("Turn off LED")
led_blue.off()
sleep(1)
print("third method with led.toggle for blinking 10x")
for x in range(0, 11):
led_red.toggle()
sleep(1)
print("fourth method uses led.blink to do all functions")
print("Turn on blinking")
led_red.blink(on_time=1, off_time=1, n=10, background=False)
print("turn off blinking")
print("fifth method with led.blink and pause 10x")
for x in range(0, 11):
print("Turn on LED")
led_red.blink()
pause()