mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Update docstrings and add initial set of documentation
This commit is contained in:
196
docs/index.md
196
docs/index.md
@@ -1,8 +1,12 @@
|
||||
# gpio-zero
|
||||
# GPIO Zero
|
||||
|
||||
A simple interface to everyday GPIO components used with Raspberry Pi
|
||||
|
||||
## Why?
|
||||
## Latest release
|
||||
|
||||
The latest release is **v0.6.0 public beta 1** released on 28th September 2015.
|
||||
|
||||
## Motivation
|
||||
|
||||
The "hello world" program in Java is at least 5 lines long, and contains 11
|
||||
jargon words which are to be ignored. The "hello world" program in Python is
|
||||
@@ -39,7 +43,30 @@ red.on()
|
||||
|
||||
Any guesses how to turn it off?
|
||||
|
||||
## Implemented Components
|
||||
## Install
|
||||
|
||||
Install with pip::
|
||||
|
||||
```bash
|
||||
sudo apt-get install python-pip python3-pip
|
||||
sudo pip install gpiozero
|
||||
sudo pip-3.2 install gpiozero
|
||||
```
|
||||
|
||||
Both Python 3 and Python 2 are supported. Python 3 is recommended!
|
||||
|
||||
### Upgrade
|
||||
|
||||
Upgrade to the latest version with:
|
||||
|
||||
```bash
|
||||
sudo pip install gpiozero --upgrade
|
||||
sudo pip-3.2 install gpiozero --upgrade
|
||||
```
|
||||
|
||||
## What's included?
|
||||
|
||||
Components:
|
||||
|
||||
- LED
|
||||
- Buzzer
|
||||
@@ -49,160 +76,17 @@ Any guesses how to turn it off?
|
||||
- Temperature Sensor
|
||||
- Motor
|
||||
|
||||
## Usage
|
||||
Boards & accessories:
|
||||
|
||||
### LED
|
||||
- LED Board
|
||||
- Traffic Lights
|
||||
- PiLITEr
|
||||
- PI-TRAFFIC
|
||||
- Fish Dish
|
||||
- Traffic HAT
|
||||
|
||||
Turn an LED on and off repeatedly:
|
||||
## Getting started
|
||||
|
||||
```python
|
||||
from gpiozero import LED
|
||||
from time import sleep
|
||||
|
||||
red = LED(2)
|
||||
|
||||
while True:
|
||||
red.on()
|
||||
sleep(1)
|
||||
red.off()
|
||||
sleep(1)
|
||||
```
|
||||
|
||||
Alternatively:
|
||||
|
||||
```python
|
||||
from gpiozero import LED
|
||||
|
||||
red = LED(2)
|
||||
red.blink(1, 1)
|
||||
sleep(10)
|
||||
```
|
||||
|
||||
### Buzzer
|
||||
|
||||
Turn a buzzer on and off repeatedly:
|
||||
|
||||
```python
|
||||
from gpiozero import Buzzer
|
||||
from time import sleep
|
||||
|
||||
buzzer = Buzzer(3)
|
||||
|
||||
while True:
|
||||
buzzer.on()
|
||||
sleep(1)
|
||||
buzzer.off()
|
||||
sleep(1)
|
||||
```
|
||||
|
||||
### Button
|
||||
|
||||
Check if a button is pressed:
|
||||
|
||||
```python
|
||||
from gpiozero import Button
|
||||
|
||||
button = Button(4)
|
||||
|
||||
if button.is_active:
|
||||
print("Button is pressed")
|
||||
else:
|
||||
print("Button is not pressed")
|
||||
```
|
||||
|
||||
Wait for a button to be pressed before continuing:
|
||||
|
||||
```python
|
||||
from gpiozero import Button
|
||||
|
||||
button = Button(4)
|
||||
|
||||
button.wait_for_press()
|
||||
print("Button was pressed")
|
||||
```
|
||||
|
||||
Run a function every time the button is pressed:
|
||||
|
||||
```python
|
||||
from gpiozero import Button
|
||||
|
||||
def warning():
|
||||
print("Don't push the button!")
|
||||
|
||||
button = Button(4)
|
||||
|
||||
button.when_pressed = warning
|
||||
```
|
||||
|
||||
### Motion Sensor
|
||||
|
||||
Detect motion and light an LED when it's detected:
|
||||
|
||||
```python
|
||||
from gpiozero import MotionSensor, LED
|
||||
|
||||
pir = MotionSensor(5)
|
||||
led = LED(16)
|
||||
|
||||
pir.when_motion = led.on
|
||||
pir.when_no_motion = led.off
|
||||
```
|
||||
|
||||
### Light Sensor
|
||||
|
||||
Wait for light and dark:
|
||||
|
||||
```python
|
||||
from gpiozero import LightSensor
|
||||
|
||||
sensor = LightSensor(18)
|
||||
|
||||
while True:
|
||||
sensor.wait_for_light()
|
||||
print("It's light! :)")
|
||||
sensor.wait_for_dark()
|
||||
print("It's dark :(")
|
||||
```
|
||||
|
||||
Run a function when the light changes:
|
||||
|
||||
```python
|
||||
from gpiozero import LightSensor, LED
|
||||
|
||||
sensor = LightSensor(18)
|
||||
led = LED(16)
|
||||
|
||||
sensor.when_dark = led.on
|
||||
sensor.when_light = led.off
|
||||
```
|
||||
|
||||
### Temperature Sensor
|
||||
|
||||
Retrieve light sensor value:
|
||||
|
||||
```python
|
||||
from gpiozero import TemperatureSensor
|
||||
|
||||
temperature = TemperatureSensor(6)
|
||||
|
||||
print(temperature.value)
|
||||
```
|
||||
|
||||
### Motor
|
||||
|
||||
Drive two motors forwards for 5 seconds:
|
||||
|
||||
```python
|
||||
from gpiozero import Motor
|
||||
from time import sleep
|
||||
|
||||
left_motor = Motor(7)
|
||||
right_motor = Motor(8)
|
||||
|
||||
left_motor.on()
|
||||
right_motor.on()
|
||||
sleep(5)
|
||||
left_motor.off()
|
||||
right_motor.off()
|
||||
```
|
||||
See the [input devices](inputs.md) and [output devices](outputs.md) to get started. Also see the [boards & accessories](boards.md) page for examples of using the included accessories.
|
||||
|
||||
For common programs using multiple components together, see the [recipes](recipes.md) page.
|
||||
|
||||
Reference in New Issue
Block a user