0% found this document useful (0 votes)
181 views1 page

GPIO Zero Cheatsheet PDF

1) The document provides information about using GPIO Zero to control LEDs, motors, buttons, and motion sensors with the Raspberry Pi. 2) It shows how to turn an LED on or off, make it blink, or change its color. 3) It also demonstrates how to move a motor forward or backward, stop it, or reverse its direction, as well as how to detect when a button is pressed or released and trigger events based on button presses.

Uploaded by

Luis Alvarez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views1 page

GPIO Zero Cheatsheet PDF

1) The document provides information about using GPIO Zero to control LEDs, motors, buttons, and motion sensors with the Raspberry Pi. 2) It shows how to turn an LED on or off, make it blink, or change its color. 3) It also demonstrates how to move a motor forward or backward, stop it, or reverse its direction, as well as how to detect when a button is pressed or released and trigger events based on button presses.

Uploaded by

Luis Alvarez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

GPIO ZERO CHEATSHEET raspberrypi.

org/resources

LED
led.on()

from gpiozero import LED led.off()


GND
led = LED(17)
led.on() led.toggle()
GP17

led.blink()

Full Colour LED


GPIO

_
led.on()
+ _ a b c d e f g h i j +
USB X2 1 1
2 2
led.off()
GP2
3
4
3
4 from gpiozero import RGBLED
led = RGBLED(red=2, green=3, blue=4)
5 5
GP3 GND
led.color = (r, g, b)
6 6
GP4 7 7
8
9
8
9
r, g, b = 0, 0, 1
led.color = (r, g, b)
10 10
11
12
11
12 led.red = 1
13 13
14 14
15 15
16 16
17 17
18 18
19 19
20 20
21 21
22 22

Motor
23 23
24 24
25 25
21 21
22 22
GPIO 23 23
24 24

motor.forward()
25 25
+ _ a b c d e f g h i j + _

USB X2

from gpiozero import Motor motor.backward()


motor = Motor(forward=17, backward=18)
motor.forward() motor.stop()

motor.reverse()

Button
+ _ from +gpiozero
_ import Button button.wait_for_press()
a b c d e f g h i
j

1 button1 = Button(4) button.wait_for_release()


2
3
while 2
3
True:
GND 4 4if button.is_pressed: button.is_pressed
5 5
print("Button is pressed")
GP4
6 6
button.when_pressed = led.on
8else:
7 7
8
9
10
9
10
print("Button is not pressed") button.when_released = led.off
11 11
12 12
13 13
14 14
15 15
16 16

PIR Motion Sensor


17 17
18 18
19 19
20 20
GPIO 21
22
21
22
pir.wait_for_motion()
23
24
from gpiozero import MotionSensor
23
24 pir.wait_for_no_motion()
USB5V X2 25
21
pir = MotionSensor(4)
25
21
GND 22 while True:
22 pir.motion_detected
GP4

if pir.motion_detected:
23 23

pir.when_motion = motor.forward
24 24

_ print("You moved")
25 25
+ _ a b c d e f g h i j +
pir.when_no_motion = motor.backward

You might also like