Skip to content

Latest commit

 

History

History
189 lines (130 loc) · 6.56 KB

recipes_advanced.rst

File metadata and controls

189 lines (130 loc) · 6.56 KB

Advanced Recipes

gpiozero

The following recipes demonstrate some of the capabilities of the GPIO Zero library. Please note that all recipes are written assuming Python 3. Recipes may work under Python 2, but no guarantees!

LEDBoard

You can iterate over the LEDs in a LEDBoard object one-by-one:

examples/led_board_3.py

LEDBoard also supports indexing. This means you can access the individual LED objects using leds[i] where i is an integer from 0 up to (not including) the number of LEDs:

examples/led_board_4.py

This also means you can use slicing to access a subset of the LEDs:

examples/led_board_5.py

LEDBoard objects can have their LED objects named upon construction. This means the individual LEDs can be accessed by their name:

examples/led_board_6.py

LEDBoard objects can also be nested within other LEDBoard objects:

examples/led_board_7.py

Who's home indicator

Using a number of green-red LED pairs, you can show the status of who's home, according to which IP addresses you can ping successfully. Note that this assumes each person's mobile phone has a reserved IP address on the home router.

examples/whos_home_leds.py

Alternatively, using the STATUS Zero board:

examples/whos_home_status.py

Travis build LED indicator

Use LEDs to indicate the status of a Travis build. A green light means the tests are passing, a red light means the build is broken:

examples/led_travis.py

Note this recipe requires travispy. Install with sudo pip3 install travispy.

Button controlled robot

Alternatively to the examples in the simple recipes, you can use four buttons to program the directions and add a fifth button to process them in turn, like a Bee-Bot or Turtle robot.

examples/robot_buttons_2.py

Robot controlled by 2 potentiometers

Use two potentiometers to control the left and right motor speed of a robot:

examples/robot_pots_1.py

To include reverse direction, scale the potentiometer values from 0->1 to -1->1:

examples/robot_pots_2.py

Note

Please note the example above requires Python 3. In Python 2, zip doesn't support lazy evaluation so the script will simply hang.

BlueDot LED

BlueDot is a Python library an Android app which allows you to easily add Bluetooth control to your Raspberry Pi project. A simple example to control a LED using the BlueDot app:

examples/bluedot_led.py

Note this recipe requires bluedot and the associated Android app. See the BlueDot documentation for installation instructions.

BlueDot robot

You can create a Bluetooth controlled robot which moves forward when the dot is pressed and stops when it is released:

examples/bluedot_robot_1.py

Or a more advanced example including controlling the robot's speed and precise direction:

examples/bluedot_robot_2.py

Controlling the Pi's own LEDs

On certain models of Pi (specifically the model A+, B+, and 2B) it's possible to control the power and activity LEDs. This can be useful for testing GPIO functionality without the need to wire up your own LEDs (also useful because the power and activity LEDs are "known good").

Firstly you need to disable the usual triggers for the built-in LEDs. This can be done from the terminal with the following commands:

$ echo none | sudo tee /sys/class/leds/led0/trigger
$ echo gpio | sudo tee /sys/class/leds/led1/trigger

Now you can control the LEDs with gpiozero like so:

examples/led_builtin.py

To revert the LEDs to their usual purpose you can either reboot your Pi or run the following commands:

$ echo mmc0 | sudo tee /sys/class/leds/led0/trigger
$ echo input | sudo tee /sys/class/leds/led1/trigger

Note

On the Pi Zero you can control the activity LED with this recipe, but there's no separate power LED to control (it's also worth noting the activity LED is active low, so set active_high=False when constructing your LED component).

On the original Pi 1 (model A or B), the activity LED can be controlled with GPIO16 (after disabling its trigger as above) but the power LED is hard-wired on.

On the Pi 3 the LEDs are controlled by a GPIO expander which is not accessible from gpiozero (yet).