GPIOZero¶
- GIOZero is a Python module that makes it very easy to work with the Raspberry Pi GPIO pins and to get started with physical computing and electronics.
LED¶
- Connect a LED to a GPIO pin and light, blink and pulse the LED.
PiRingo (4tronix)¶
LED |
GPIO pin |
|---|---|
LED 1 |
4 |
LED 2 |
17 |
LED 3 |
18 |
LED 4 |
27 |
LED 5 |
22 |
LED 6 |
23 |
LED 7 |
24 |
LED 8 |
25 |
LED 9 |
8 |
LED 10 |
7 |
LED 11 |
14 |
LED 12 |
15 |
Button |
GPIO pin |
|---|---|
Button 1 |
10 |
Button 2 |
9 |
from gpiozero import LEDBoard
led_circle = LEDBoard(4, 17, 18, 27, 22, 23, 24, 25, 8, 7, 14, 15)
led_circle.on() # all LED's on
led_circle[2].on() # third LED on (count from zero)
RGBLED¶
Connect a tri-colour (r,g,b) LED to the GPIO pins and create multiple colours.
##
# Colour wheel using a tri-colour LED
#
from time import sleep
from gpiozero import RGBLED
# Assign the correct pins for the three colours
led = RGBLED(green=13, blue=19, red=26)
# Function to cycle through colours
def colour_wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
return 0, 0, 0
if pos < 85:
return int(255 - pos*3)/1000, int(pos*3)/1000, 0
if pos < 170:
pos -= 85
return 0, int(255 - pos*3)/1000, int(pos*3)/1000
pos -= 170
return int(pos * 3)/1000, 0, int(255 - (pos*3))/1000
i = 0
while True:
# Run from 0 to 255
i = (i + 1) % 256
# Cycle through colours
led.color = colour_wheel(i)
sleep(0.01)
Traffic lights (Pi-Stop)¶
- Connect three (red, amber, green) LED’s to act as a traffic light signal. (See LED above using three LED’s).
Ultrasonic distance sensor¶
- Use an ultrasonic distance sensor to measure distance or detect objects.
Motor¶
- A simple motor connected to the Pi through which the speed and direction can be controlled. Please note, a H bridge will be required to control the motor.
Servo motor¶
- A servo motor connected to the Pi and controlled to rotate to various angles.