Raspberry PI
Warning; Use the Micro USB to Power the Raspberry port; the GPIO Pins are source from the Micro USB Supply
sudo apt install python3-gpiozero
from gpiozero import LED
from gpiozero import LED, Button
GPiozero library uses Broadcom (BCM) pin numbering rather than physical pin numbers.. The following examples will all work for an LED that is on GPIO 17 on physical pin 11:
led = LED(17)
led = LED("GPIO17")
led = LED("BCM17")
led = LED("BOARD11")
led = LED("WPI0")
led = LED("J8:11")
handy command line tool called "pinout" is part of the library and will graphically show you the GPIO pins for the board it is running on (or any board revision that you specify):
Raspberry Pi GPIO Programming with Python - Part 1: Introduction & Controlling LEDs
https://www.youtube.com/watch?v=bbI43MqPE-I
Gpiozero
A newer GPIO library for the Raspberry Pi is gpiozero [4]. Created by Ben Nuttall of the Raspberry Pi Foundation and other contributors it is released under an MIT-type free software license. https://www.ics.com/blog/control-raspberry-pi-gpio-pins-python
A newer GPIO library for the Raspberry Pi is gpiozero [4]. Created by Ben Nuttall of the Raspberry Pi Foundation and other contributors it is released under an MIT-type free software license. https://www.ics.com/blog/control-raspberry-pi-gpio-pins-python
from gpiozero import LED
from time import sleep
led = LED(24)
while True:
led.on()
sleep(1)
led.off()
sleep(1)
The above loop could also be done by simply calling:
red.blink()
#!/usr/bin/python3
from gpiozero import Button
from time import sleep
button = Button(6)
while True:
if button.is_pressed:
print("Button is pressed")
else:
print("Button is not pressed")
sleep(1)
#!/usr/bin/python3
from gpiozero import LED, Button
from signal import pause
led = LED(24)
button = Button(6)
button.when_pressed = led.on
button.when_released = led.off
pause()
3..**
from gpiozero import PWMLED
from time import pause
led =PWMLED(17)
led.pulse()
pause
2..**
from gpiozero import PWMLED
from time import sleep
led =PWMLED(17)
while True:
led.value =0 #off
sleep(1)
led.value =0.5 # halft bright
sleep(1)
led.value =1 # FULL BRIGHT
sleep(1)
1..***
from gpiozero import LED
from signal import pause
red=LED(17)
red.blink()
pause
6..**
from gpiozero import Button.LED
from signal import pause
led =LED(17)
button =Button(2)
button.when_pressed =led.on
button.when_release =led.off
pause()
5..**
from signal import pause
from gpiozero import Button
def say_hello():
print("hello")
button =Button(2)
button.when_Presses =say_hello
4..**
from gpiozero import Button
button =Button(2)
while True:
if button.is_pressed:
print("Button is pressed")
else:
print(not pressed")