Top Banner
Raspberry Pi & GPIO Zero
37

Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Jun 27, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Raspberry Pi & GPIO Zero

Page 2: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

TopicsWhat is the Raspberry Pi?

What are GPIOs?

Physical computing starter kits

What is GPIO Zero?

GPIO Zero documentation and tutorials

GPIO Zero examples

Page 3: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Martin Wimpress

1972 Vintage Human Meatbag

Page 4: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Software Engineer

Page 5: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Ubuntu Desktop Team

Page 6: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Raspberry Pi Insider

Page 7: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

A complete GPIO newbie!

Absolutely the worst person to give this talk!

Page 8: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

What is the Raspberry Pi?

Page 9: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

What is the Raspberry Pi?

“The Raspberry Pi is a series of credit card-sized single-board computers developed in the United

Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in

schools and in developing countries.”

- Wikipedia

Page 10: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

The Raspberry Pi line-up?

● Raspberry Pi Model A 1 & 1+● Old and crusty● $20 - $25

● Raspberry Pi Model B 1, 1+, 2 & 3● New hotness!● $25 - $35

● Raspberry Pi Compute Module 1 & 3● Specialist

● Raspberry Pi Zero● Adorable● $5

Page 11: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Which Raspberry Pi to buy?

● Raspberry Pi Model A 1 & 1+● Old and crusty● $20 - $25

● Raspberry Pi Model B 1, 1+, 2 & 3● New hotness!● $25 - $35

● Raspberry Pi Compute Module 1 & 3● Specialist

● Raspberry Pi Zero● Adorable● $5

Page 12: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

What do you get for $35?

● Broadcom BCM2837 ARMv8-A (64/32-bit)● 1.2 GHz 64-bit quad-core ARM Cortex-A53● 1 GB RAM (shared with GPU)

● Broadcom VideoCore IV GPU● OpenGL ES 2.0● H.264/MPEG-4 decoder and encoder● HDMI 1.3

● 4× USB 2.0 ports & 1× 10/100 Ethernet port● 2.4 GHz WiFi 802.11n & Bluetooth 4.1● 15-pin MIPI camera interface (CSI) connector● 17× GPIO

Page 13: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

What are GPIOs?

● General Purpose Input-Output connector● 40-pin GPIO header on the Raspberry Pi● Enables the connection and control of electronic

components with a program.

Page 14: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

40-pin GPIO header

Page 15: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Physical computing kits

● Ben Nuttal recommends CamJam Edu Kits● Joint venture between The Pi Hut and the Cambridge Raspberry Jam● https://thepihut.com/collections/camjam-edukit

“Low-cost way to introduce kids (and adults!) to the world of electronics and programming.”

Page 16: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

What is GPIO Zero?

“A simple interface to everyday GPIO components used with Raspberry Pi.”

- Ben Nuttal

Page 17: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Why GPIO Zero?

● Python● The preferred language of the Raspberry Pi Foundation

● GPIO was complex● Connecting electronics required lots of code just to get set up

● GPIO Zero handles the boilerplate for you ● You focus on controlling the physical devices● Fewer lines of code● Easier for newcomers to understand

● GPIO Zero uses Broadcom (BCM) pin numbering● As opposed to physical (BOARD) numbering

Page 18: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

BCM vs BOARD numbering

“The BCM numbering bears no relation to their actual position on the Raspberry Pi GPIO header!”

- Tim Cox

Page 19: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

40-pin GPIO header

Page 20: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Raspberry Pi Pinout

“The comprehensive GPIO Pinout guide for the Raspberry Pi.”

- https://pinout.xyz/

Page 21: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

GPIO Ruler

“Quick reference for GPIO hacking on the Raspberry Pi in Python”

- http://rasp.io/gpioruler/

Page 22: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Pibow 3 Coupé

https://shop.pimoroni.com/collections/raspberry-pi/products/pibow-coupe-for-raspberry-pi-3

Page 23: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

GPIO Documentation & Tutorials

● The Documentation● http://gpiozero.readthedocs.io/● https://github.com/RPi-Distro/python-gpiozero

● The Book● http://bennuttall.com/simple-electronics-gpio-zero-book/● https://www.raspberrypi.org/magpi/gpio-zero-essentials/● https://www.raspberrypi.org/magpi-issues/Essentials_GPIOZero_v1.pdf

● Physical Computing with Python● https://www.raspberrypi.org/learning/physical-computing-with-python/

Page 24: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

The MagPi

● The Official Raspberry Pi Magazine● Free to download or purchase the print copy● https://www.raspberrypi.org/magpi/

Page 25: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

GPIO examples

● You will need:– A Raspberry Pi

– SD card of “Raspbian Jessie with PIXEL”● https://www.raspberrypi.org/downloads/raspbian/

– Power supply

– Monitor

– A keyboard and mouse

– Python3

Page 26: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Turn a LED on and off repeatedly

from gpiozero import LED

from time import sleep

red = LED(17)

while True:

red.on()

sleep(1)

red.off()

sleep(1)

Page 27: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Check if a Button is pressed

from gpiozero import Button

button = Button(2)

while True:

if button.is_pressed:

print("Button is pressed")

else:

print("Button is not pressed")

Page 28: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Button controlled LED

from gpiozero import LED, Button

from signal import pause

led = LED(17)

button = Button(2)

button.when_pressed = led.on

button.when_released = led.off

pause()

11

55

1010

1515

2020

2525

3030

A

A

B

B

C

C

D

D

E

E

F

F

G

G

H

H

I

I

J

J

Rasp

berry

Pi M

odel 2

v1.1

© R

asp

berry

Pi 2

014

http

://ww

w.rasp

berryp

i.org

Pow

er

HD

MI

Audio

USB 2x USB 2x

ETHERNET

DSI (DISPLAY)

CSI (CAMERA)

GPIO

Page 29: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Button controlled LED

from gpiozero import LED, Button

from signal import pause

led = LED(17)

button = Button(2)

led.source = button.values

pause()

11

55

1010

1515

2020

2525

3030

A

A

B

B

C

C

D

D

E

E

F

F

G

G

H

H

I

I

J

J

Rasp

berry

Pi M

odel 2

v1.1

© R

asp

berry

Pi 2

014

http

://ww

w.rasp

berryp

i.org

Pow

er

HD

MI

Audio

USB 2x USB 2x

ETHERNET

DSI (DISPLAY)

CSI (CAMERA)

GPIO

Page 30: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Button controlled LED

from gpiozero import LED, Button

from signal import pause

led = LED(17)

button = Button(2)

led.source = button.values

pause()

11

55

1010

1515

2020

2525

3030

A

A

B

B

C

C

D

D

E

E

F

F

G

G

H

H

I

I

J

J

Rasp

berry

Pi M

odel 2

v1.1

© R

asp

berry

Pi 2

014

http

://ww

w.rasp

berryp

i.org

Pow

er

HD

MI

Audio

USB 2x USB 2x

ETHERNET

DSI (DISPLAY)

CSI (CAMERA)

GPIO

Page 31: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Button controlled camerafrom gpiozero import Button

from picamera import PiCamera

from datetime import datetime

from signal import pause

button = Button(2)

camera = PiCamera()

def capture():

datetime = datetime.now().isoformat()

camera.capture('/home/pi/%s.jpg' % datetime)

button.when_pressed = capture

pause()

Page 32: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Motion detector

from gpiozero import MotionSensor, LED

from signal import pause

pir = MotionSensor(4)

led = LED(16)

pir.when_motion = led.on

pir.when_no_motion = led.off

pause()

11

55

1010

1515

2020

2525

3030

A

A

B

B

C

C

D

D

E

E

F

F

G

G

H

H

I

I

J

J

Rasp

berry

Pi Mod

el 2

v1

.1©

Rasp

berry

Pi 20

14

http

://ww

w.rasp

berrypi.o

rg

Pow

er

HD

MI

Au

dio

USB 2x USB 2x

ETHERNET

DSI (DISPLAY)

CSI (CAMERA)

GPIO

Page 33: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Distance detector

from gpiozero import DistanceSensor

from time import sleep

sensor = DistanceSensor(23, 24)

while True:

print('Distance to nearest object is', sensor.distance, 'm')

sleep(1)

11

55

1010

1515

2020

2525

3030

A

A

B

B

C

C

D

D

E

E

F

F

G

G

H

H

I

I

J

J

Rasp

berry

Pi Mod

el 2

v1

.1©

Rasp

berry

Pi 20

14

http

://ww

w.rasp

berrypi.o

rg

Pow

er

HD

MI

Au

dio

USB 2x USB 2x

ETHERNET

DSI (DISPLAY)

CSI (CAMERA)

GPIO

Page 34: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

What can GPIO control?

● Input devices

– Button

– Line Sensor (TRCT5000)

– Motion Sensor (D-SUN PIR)

– Light Sensor (LDR)

– Distance Sensor (HC-SR04)

– DigitalInputDevice

– SmoothedInputDevice

● Output devices

– LED

– PWMLED

– RGBLED

– Buzzer

– Motor

– Servo

– AngularServo

– DigitalOutputDevice

– PWMOutputDevice

Page 35: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

What can GPIO control?

● LEDBoard

● LEDBarGraph

● ButtonBoard

● TrafficLights

● LedBorg

● PiLITEr

● PiLITEr Bar Graph

● PI-TRAFFIC

● TrafficLightsBuzzer

● Fish Dish

● Traffic HAT

● Robot

● Ryanteck MCB Robot

● CamJam #3 Kit Robot

● Energenie

● SnowPi

Page 36: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output

Summary

The Raspberry Pi 3 is a small inexpensive computer

General Purpose Input-Output enables the connection and control of electronic components with a program.

CamJam Edu Kits are great for newcomers

GPIO Zero is a simple interface for GPIO components used with Raspberry Pi.

Lots of GPIO Zero documentation and tutorials

Page 37: Raspberry Pi & GPIO Zero - Basingstoke Makerspace · – SD card of “Raspbian Jessie with PIXEL ... The Raspberry Pi 3 is a small inexpensive computer General Purpose Input-Output