Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Physical computing with GPIO Zero - oggcamp
Search
Ben Nuttall
November 01, 2015
Programming
0
270
Physical computing with GPIO Zero - oggcamp
Presentation given at oggcamp 2015 on the new GPIO Zero Python library
Ben Nuttall
November 01, 2015
Tweet
Share
More Decks by Ben Nuttall
See All by Ben Nuttall
Numeronyms are obnoxious
bennuttall
0
150
Live Highlights in BBC iPlayer
bennuttall
0
42
Rapid prototyping in BBC News with Python and AWS
bennuttall
0
110
Rapid prototyping in BBC News with Python and AWS
bennuttall
0
64
Running a Python Package Index for Raspberry Pi
bennuttall
0
69
From Acorns to Raspberries
bennuttall
0
53
Innovation in the newsroom
bennuttall
0
69
Innovation in the newsroom - MOS Running Order Manager
bennuttall
0
130
How to market your open source project
bennuttall
0
180
Other Decks in Programming
See All in Programming
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
Remix on Hono on Cloudflare Workers
yusukebe
1
280
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
860
距離関数を極める! / SESSIONS 2024
gam0022
0
280
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
230
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.1k
Contemporary Test Cases
maaretp
0
130
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
Featured
See All Featured
Side Projects
sachag
452
42k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Documentation Writing (for coders)
carmenintech
65
4.4k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
GraphQLとの向き合い方2022年版
quramy
43
13k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Transcript
Physical computing with GPIO Zero Ben Nuttall Raspberry Pi Foundation
UK Charity 1129409
Ben Nuttall • Education Developer Advocate at the Raspberry Pi
Foundation – Software & project development – Learning resources & teacher training – Outreach • Hobbyist turned employee • Based in Cambridge (but from the North) • @ben_nuttall on Twitter
Python – RPi.GPIO import RPi.GPIO as GPIO from time import
sleep GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) led = 17 GPIO.setup(led, GPIO.OUT) while True: GPIO.output(led, True) sleep(1) GPIO.output(led, False) sleep(1)
Python – GPIO Zero from gpiozero import LED from time
import sleep led = LED(17) while True: led.on() sleep(1) led.off() sleep(1)
LED from gpiozero import LED from time import sleep led
= LED(17) led.on() # on led.off() # off led.toggle() # on>off or off>on led.blink() # flash on/off continuously
LED + Button from gpiozero import LED, Button led =
LED(17) button = Button(3) button.when_pressed = led.on button.when_released = led.off
Source + Values from gpiozero import LED, Button led =
LED(17) button = Button(3) led.source = button.values
LED - PWM from gpiozero import PWMLED from time import
sleep led = PWMLED(17) led.on() # on led.off() # off led.value = 0.5 # half brightness
Motion sensor from gpiozero import LED, MotionSensor led = LED(2)
sensor = MotionSensor(3) sensor.when_motion = led.on sensor.when_no_motion = led.off
Light sensor from gpiozero import LED, LightSensor led = LED(2)
sensor = LightSensor(3) while True: sensor.wait_for_light() print("It's light!") sensor.wait_for_dark() print("It's dark")
Traffic Lights from gpiozero import TrafficLights lights = TrafficLights(9, 10,
11) lights.on() # all on lights.off() # all off lights.red.on() # red on lights.toggle() # swap state of all lights
Traffic Lights sequence lights.green.on() lights.amber.off() lights.red.off() while True: sleep(10) lights.green.off()
lights.amber.on() sleep(1) lights.amber.off() lights.red.on() sleep(10) lights.amber.on() sleep(1) lights.green.on() lights.amber.off() lights.red.off()
TrafficHat from gpiozero import TrafficHat th = TrafficHat() th.lights.red.on() th.lights.amber.on()
th.button.when_pressed = th.on th.button.when_released = th.off
TrafficHat - PWM from gpiozero import TrafficHat th = TrafficHat(pwm=True)
th.lights.red.value = 0.2 th.lights.amber.value = 0.4 th.lights.green.value = 0.8
Push button stop motion from gpiozero import Button from picamera
import PiCamera button = Button(4) with PiCamera() as camera: camera.start_preview() frame = 1 while True: button.wait_for_press() camera.capture('/home/pi/frame%03d.jpg' % frame) frame += 1
Full colour LED from gpiozero import RGBLED led = RGBLED(2,
3, 4) led.red.on() # full red led.color = (1, 0, 1) # purple led.blue = 0.3 # dim the blue value to 0.3 # now (1, 0, 0.3)
Motor from gpiozero import Motor from time import sleep motor
= Motor(forward=17, backward=18) while True: motor.forward() sleep(5) motor.backward() sleep(5)
Robot from gpiozero import Robot from time import sleep robot
= Robot(left=(17, 18), right=(22, 23)) while True: robot.forward() sleep(10) robot.left() sleep(1)
Button controlled Robot from gpiozero import RyanteckRobot, Button robot =
RyanteckRobot() left = Button(26) right = Button(16) fw = Button(21) bw = Button(20) fw.when_pressed = robot.forward fw.when_released = robot.stop left.when_pressed = robot.left left.when_released = robot.stop right.when_pressed = robot.right right.when_released = robot.stop bw.when_pressed = robot.backward bw.when_released = robot.stop
Analogue - potentiometers from gpiozero import MCP3008 pot = MCP3008()
while True: print(pot.value)
Dial up the brightness! from gpiozero import PWMLED, MCP3008 led
= PWMLED(2) pot = MCP3008() while True: led.value = pot.value
Dial up the brightness! from gpiozero import PWMLED, MCP3008 led
= PWMLED(2) pot = MCP3008() led.source = pot.values
Analogue - potentiometers from gpiozero import RGBLED, MCP3008 led =
RGBLED(red=2, green=3, blue=4) red_pot = MCP3008(channel=0) green_pot = MCP3008(channel=1) blue_pot = MCP3008(channel=2) while True: led.red = red_pot.value led.green = green_pot.value led.blue = blue_pot.value
Analogue - potentiometers from gpiozero import RGBLED, MCP3008 led =
RGBLED(red=2, green=3, blue=4) red_pot = MCP3008(channel=0) green_pot = MCP3008(channel=1) blue_pot = MCP3008(channel=2) led.red.source = red_pot.values led.green.source = green_pot.values led.blue.source = blue_pot.values
GPIO Music Box from gpiozero import Button import pygame.mixer from
pygame.mixer import Sound pygame.mixer.init() def play(pin): sound = sound_pins[pin] print("playing note from pin %s" % pin) sound.play() sound_pins = { 2: Sound("samples/drum_tom_mid_hard.wav"), 3: Sound("samples/drum_cymbal_open.wav"), } buttons = [Button(pin) for pin in sound_pins] for button in buttons: sound = sound_pins[button.pin] button.when_pressed = sound.play
Analogue - potentiometers from gpiozero import PWMLED, MCP3008 led =
PWMLED(2) pot = MCP3008() led.source = pot.values
GPIO Zero - beta • www.pythonhosted.org/gpiozero – Installation instructions –
Documentation – Examples • GitHub Issues – Suggestions – Feedback • Contact me –
[email protected]
– @ben_nuttall on Twitter • #gpiozero on Twitter
CamJam EduKit • £5 starter kit (kit 1) • £7
sensors kit (kit 2) • £17 robotics kit (kit 3) • Free worksheets • Very reusable
The MagPi
The MagPi
Physical computing with GPIO Zero Ben Nuttall Raspberry Pi Foundation
UK Charity 1129409