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

GPIO Zero Cheatsheet

This document provides a cheat sheet for using the GPIO Zero library to control common electronic components connected to a Raspberry Pi such as LEDs, motors, buttons, and motion sensors. It shows how to import and initialize each component class and lists common methods to control the component such as turning an LED on or off, moving a motor forward or backward, checking if a button is pressed, and detecting motion. The cheat sheet also demonstrates connecting multiple components by triggering actions on one from events of another.

Uploaded by

j
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)
381 views1 page

GPIO Zero Cheatsheet

This document provides a cheat sheet for using the GPIO Zero library to control common electronic components connected to a Raspberry Pi such as LEDs, motors, buttons, and motion sensors. It shows how to import and initialize each component class and lists common methods to control the component such as turning an LED on or off, moving a motor forward or backward, checking if a button is pressed, and detecting motion. The cheat sheet also demonstrates connecting multiple components by triggering actions on one from events of another.

Uploaded by

j
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 = LED(17)
led.on()

GND

led.off()
led.toggle()

GP17

led.blink()

Full Colour LED


GPIO

a b c d e

USB X2

g h i

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
21
22
23
24
25

3
4
5

GND

GP4

Motor
GPIO

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
21
22
23
24
25
a b c d e

g h i

led.on()

1
2

GP2
GP3

from gpiozero import RGBLED


led = RGBLED(red=2, green=3, blue=4)
r, g, b = 0, 0, 1
led.color = (r, g, b)

led.color = (r, g, b)
led.red = 1

motor.forward()

led.off()

USB X2

from gpiozero import Motor


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

motor.backward()
motor.stop()
motor.reverse()

Button
+

a b c d e

1
2
3
4
5

GND
GP4

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
21
22
23
24
25

from
import Button
_
+gpiozero
j
button
= Button(4)
1
2
while
True:
3
4if button.is_pressed:
5
6
print("Button is pressed")
7
8else:
9
print("Button is not pressed")
10

g h i

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
21
22
23
24
25

PIR Motion Sensor


GPIO

USB5V X2
GND
GP4

a b c d e

from gpiozero import MotionSensor


pir = MotionSensor(4)
while True:
if pir.motion_detected:
_ print("You moved")
+
g h i j

button.wait_for_press()
button.wait_for_release()
button.is_pressed
button.when_pressed = led.on
button.when_released = led.off

pir.wait_for_motion()
pir.wait_for_no_motion()
pir.motion_detected
pir.when_motion = motor.forward
pir.when_no_motion = motor.backward

You might also like