8.2 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	Output Devices
These output device component interfaces have been provided for simple use of everyday components.
Components must be wired up correctly before used in code.
Note all GPIO pin numbers use BCM numbering. See the notes page for more information.
LED
An LED (Light emitting diode) component.
Wiring
Connect the cathode (the short leg) of the LED to a ground pin, and connect the anode (the longer leg) to any GPIO pin, with a current limiting resistor in between:
Altenatively, use a breadboard to wire up the LED in the same way
Code
Ensure the LED class is imported at the top of the file:
from gpiozero import LED
Create an LED object by passing in the pin number the LED is connected to:
led = LED(17)
Initialisation options
LED(pin=None, active_high=True)
| Argument | Description | Values | Default | 
|---|---|---|---|
| pin | The GPIO pin number the LED is connected to. | Integer: 0to25 | Required | 
| active_high | Whether high or low voltage turns the LED on. | Boolean | True | 
Methods
| Method | Description | Arguments | 
|---|---|---|
| on() | Turn the LED on. | None | 
| off() | Turn the LED off. | None | 
| toggle() | Toggle the LED. If it's on, turn it off; if it's off, turn it on. | None | 
| blink() | Make the LED turn on and off repeatedly. | on_time- The amount of time (in seconds) for the LED to be on each iteration. Default:1 | 
| off_time- The amount of time (in seconds) for the LED to be off each iteration. Default:1 | ||
| n- The number of iterations.Nonemeans infinite. Default:None | ||
| background- IfTrue, start a background thread to continue blinking and return immediately. IfFalse, only return when the blink is finished (warning: the default value of n will result in this method never returning). Default:True | 
Properties
| Property | Description | Type | 
|---|---|---|
| pin | The GPIO pin number the LED is connected to. | Integer | 
| is_active | The current state of the LED ( Trueif on;Falseif off). | Boolean | 
Buzzer
A digital Buzzer component.
Wiring
Connect the negative pin of the buzzer to a ground pin, and connect the positive side to any GPIO pin:
...
Code
Ensure the Buzzer class is imported at the top of the file:
from gpiozero import Buzzer
Create a Buzzer object by passing in the pin number the buzzer is connected
to:
buzzer = Buzzer(3)
Initialisation options
Buzzer(pin=None, active_high=True)
| Argument | Description | Values | Default | 
|---|---|---|---|
| pin | The GPIO pin number the buzzer is connected to. | Integer: 0to25 | Required | 
| active_high | Whether high or low voltage turns the buzzer on. | Boolean | True | 
Methods
| Method | Description | Arguments | 
|---|---|---|
| on() | Turn the buzzer on. | None | 
| off() | Turn the buzzer off. | None | 
| toggle() | Toggle the buzzer. If it's on, turn it off; if it's off, turn it on. | None | 
| beep() | Make the buzzer turn on and off repeatedly. | on_time- The amount of time (in seconds) for the buzzer to be on each iteration. Default:1 | 
| off_time- The amount of time (in seconds) for the buzzer to be off each iteration. Default:1 | ||
| n- The number of iterations.Nonemeans infinite. Default:None | ||
| background- IfTrue, start a background thread to continue beeping and return immediately. IfFalse, only return when the blink is finished (warning: the default value of n will result in this method never returning). Default:True | 
Properties
| Property | Description | Type | 
|---|---|---|
| pin | The GPIO pin number the buzzer is connected to. | Integer | 
| is_active | The current state of the buzzer ( Trueif on;Falseif off). | Boolean | 
RGB LED
A full colour LED component (made up of Red, Green and Blue LEDs).
Wiring
Connect the common cathode (the longest leg) to a ground pin; connect each of the other legs (representing the red, green and blue components of the LED) to any GPIO pins, each with a current limiting resistor in between:
...
Code
Ensure the RGBLED class is imported at the top of the file:
from gpiozero import RGBLED
Create a RGBLED object by passing in the LED pin numbers by name:
led = RGBLED(red=2, green=3, blue=4)
or just in order (red, green, blue):
led = RGBLED(2, 3, 4)
Initialisation options
RGBLED(red=None, green=None, blue=None)
| Argument | Description | Values | Default | 
|---|---|---|---|
| red | The GPIO pin number the red LED is connected to. | Integer: 0to25 | Required | 
| green | The GPIO pin number the green LED is connected to. | Integer: 0to25 | Required | 
| blue | The GPIO pin number the blue LED is connected to. | Integer: 0to25 | Required | 
Methods
| Method | Description | Arguments | 
|---|---|---|
| on() | Turn all the LEDs on (makes white light). | None | 
| off() | Turn all the LEDs off. | None | 
| toggle() | Toggle the LED. If it's on (at all), turn it off; if it's off, turn it on. | None | 
| blink() | Make the LED turn on and off repeatedly. | on_time- The amount of time (in seconds) for the LED to be on each iteration. Default:1 | 
| off_time- The amount of time (in seconds) for the LED to be off each iteration. Default:1 | ||
| n- The number of iterations.Nonemeans infinite. Default:None | ||
| background- IfTrue, start a background thread to continue blinking and return immediately. IfFalse, only return when the blink is finished (warning: the default value of n will result in this method never returning). Default:True | 
Properties
| Property | Description | Type | 
|---|---|---|
| red | The brightness value of the red LED ( 0to1). | Integer or Float | 
| green | The brightness value of the green LED ( 0to1). | Integer or Float | 
| blue | The brightness value of the blue LED ( 0to1). | Integer or Float | 
| color | The brightness values of the three LEDs (0, 0, 0)to(1, 1, 1). | Tuple | 
Motor
Generic bi-directional motor.
Wiring
Attach a Motor Controller Board to your Pi, connect a battery pack to the Motor Controller Board, and connect each side of the motor to any GPIO pin:
...
Code
Ensure the Motor class is imported at the top of the file:
from gpiozero import Motor
Create a Motor object by passing in the pin numbers the motor is connected to:
motor = Motor(forward=17, back=18)
Initialisation options
Motor(forward=None, back=None)
| Argument | Description | Values | Default | 
|---|---|---|---|
| forward | The GPIO pin number the forward gear of the motor is connected to. | Integer: 0to25 | Required | 
| back | The GPIO pin number the reverse gear of the motor is connected to. | Integer: 0to25 | Required | 
Methods
| Method | Description | Arguments | 
|---|---|---|
| forward() | Drive the motor forwards. | speed- Speed at which to drive the motor,0to1. Default:1 | 
| backward() | Drive the motor backwards. | speed- Speed at which to drive the motor,0to1. Default:1 | 
| stop() | Stop the motor. | None | 
