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

f

g h i

j

led.on()

_

+

j

1
2

GP2
GP3

f

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

f

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

f

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