Kit de survie : RPi.GPIO vs gpiozero

La version jpg : KS-01-Rpi-gpiozero.jpeg  

La version pdf : KS-01-Rpi-gpiozero.pdf

I. Avec une Led

RPi.GPIO

import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM) 
led = 17 
GPIO.setup(led, GPIO.OUT) 
GPIO.output(led, 1) 
sleep(1) 
GPIO.output(led, 0)
sleep(1)

gpiozero

from gpiozero import LED
from time import sleep
led = LED(17)
led.on()
sleep(1) 
 
led.off()
sleep(1)

 

II.Avec un BP

RPi.GPIO

import RPi.GPIO as GPIO  
from time import sleep
GPIO.setmode(GPIO.BCM)  
bouton = 4  
GPIO.setup(bouton, GPIO.IN, pull_up_down=GPIO.PUD_UP) 
 
while True:
    sleep(1)
    if (GPIO.input(bouton) == 0):
        print('BP On')
    else:
        print('BP Off')

 

gpiozero

from gpiozero import Button
from time import sleep
bouton = Button(4)

while True:
    sleep(1)
    if bouton.is_pressed:		
        print('BP On')
    else:
        print('BP Off')

 

III Avec les deux

RPi.GPIO

import RPi.GPIO as GPIO  
GPIO.setmode(GPIO.BCM)  
led = 17 
bouton = 4
GPIO.setup(led, GPIO.OUT) 
GPIO.setup(bouton, GPIO.IN, pull_up_down=GPIO.PUD_UP) 
 
while True:
    if (GPIO.input(bouton) == 0):
        GPIO.output(led, 1)
    else:
        GPIO.output(led, 0)

gpiozero

from gpiozero import Button
from gpiozero import LED
led = LED(17)
bouton = Button(4)
 
while True:
    if bouton.is_pressed:
        led.on()
    else:
        led.off()

 

Leave a Reply