Raspberry Pi Zero Introduction
Raspberry Pi Zero Introduction
FREE
CA
RASPBERRY PI ZERO:
ACCESS TO TOOLS
M
LE
B
U
N
D
FOR SU
LE
BSCRIB E
RS
PAGE 42
THIS MONTH:
8 INTRODUCING THE TINY $5 PI ZERO
Eben Upton
Find out what the Zero is and what you can do with it
DESIGN
PUBLISHING
DISTRIBUTION
SUBSCRIPTIONS
CONTRIBUTORS
raspberrypi.org/magpi
The MagPi magazine is published by Raspberry Pi (Trading) Ltd., Mount Pleasant House, Cambridge, CB3 0RN. The
publisher, editor and contributors accept no responsibility in respect of any omissions or errors relating to goods, products
or services referred to or advertised in the magazine. Except where otherwise noted, content in this magazine is licensed
under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0). ISSN: 2051-9982.
December
March 2015
Contents
raspberrypi.org/magpi
COVER FEATURE
8
RASPBERRY PI ZERO
Learn all about this evolutionary leap and how you can get started with your Pi Zero today!
TUTORIALS
> PIPE TEMPERATURE MONITOR
50
92
54
56
60
88
THIS MONTH IN
RASPBERRY PI
62
Make much more than just music with the aid of Sam Aaron
December 2015
raspberrypi.org/magpi
Contents
SUBSCRIBE TODAY!
to get a free Pi Zero
cable bundle!
42
95
Astro Pis mission aboard the ISS is starting. We find out more
and show you how to join in the fun with the Sense HAT
70
96
COMMUNITY
YOUR PROJECTS
> EVENTS
88
90
92
46
> PI-TOP
SEEMORE
This Pi-powered parallel processing sculpture will take your breath away
MAGIC MIRROR
44
raspberrypi.org/magpi
REVIEWS
82
86
December 2015
Tutorial
WALKTHROUGH
NEVER
BEFORE
PRINTED
All the best articles
from issues 31-35
December 2015
raspberrypi.org/magpi
THE
Ocial
12.99
Tutorial
200 pages of
Raspberry Pi
RASPBERRY PI
PROJECTS BOOK
Amazing hacking and making projects
from the makers of
magazine
Inside:
How to get started with Raspberry Pi
The most inspirational community projects
Essential tutorials, guides and ideas
Expert reviews and buying advice
able
Availw
no
SWAG.RASPBERRYPI.ORG
and from all good newsagents
raspberrypi.org/magpi
December 2015
Feature
RASPBERRY PI
ZER0
A new revolution in
computing is upon us
30m
December 2015
mm
65
MicroSD card
slot for storing
the operating
system, files,
anddocuments
Mini-HDMI port
for digital sound
and 1080p video
Broadcom BCM
2835 systemon-a-chip, the
same chip
that powered
the original
Raspberry Pi
raspberrypi.org/magpi
RASPBERRY PI ZERO
Feature
CONTENTS
40-pin
unpopulated
GPIO port for
connecting to
your electronic
circuits
Unpopulated
RUN mode pins
which can be
used to add a
reset button to
the Pi Zero
Unpopulated
RCA composite
video out pins so
you can connect
to an old TV
SIZE MATTERS
How big is a Pi Zero? Weve been
comparing them to all sorts of things
Power and
activity LED this
lets you know the
Pi Zero is on and
working away
> 10
> 12
> 16
> 18
Micro USB port
for connecting to
keyboards, mice,
WiFi, USB sticks,
and more
THE SPECS
CPU: BCM 2835 (same as the original
ESSENTIAL TIPS
Learn five techniques that will make
using Pi Zero much easier
> 20
QUICK PROJECTS
Some quick taster projects that
improve traditional Raspberry Pi uses
> 22
RAM: 512MB
> 23
raspberrypi.org/magpi
ZERO PROJECTS
Get started straight away with your Pi
Zero with these great projects
> 24
3D PRINTED CASE
Learn how to 3D print your own custom
MagPi case for your new Pi Zero
> 41
December 2015
Feature
PI ZERO IN PICTURES
PI
ZER
0
IN PICTURES
Weve been so enamoured with the size of the Raspberry Pi Zero
thateveryoneheregotintoasmallcompetitiontoshowofftheir
best size comparison photos!
Send us your best comparison shots on Twitter to @TheMagP1
RASPBERRY PI
ZER0
10
December 2015
raspberrypi.org/magpi
RASPBERRY PI ZERO
RASPBERRY PI ZERO:
$5 3X
IN NUMBERS
143
1080P
Above A LEGO Minifigure is just a bit taller than the Zero is wide.
Its about one Minifigure wide and under two Minifigures long.
Also, they can easily carry one!
2.5KG
The Raspberry Pi Zero can easily play 1080p video, even
though its so tiny and draws very little power
Above A classic,
were working
on making a fully
working Pi Zero
battery and
everything in an
Altoids mint tin. You
can fit six in one!
Below Putting
a Raspberry Pi
Zero in a pack of
cards sounds like
proper James Bond
territory, although
youd lose the
cards you need to
play poker
8128 MFLOPS
A 2.5kg box of Zeros contains 8,128 MFLOPs of processing power, which makes it
more powerful than 50 Cray-1 supercomputers!
512MB
It also launches
with twice as
much RAM as
the very first
Raspberry Pi did
(it 256MB)
160mA
Hooked up to a 1080p TV with a mouse and keyboard attached, the Pi Zero
draws a tiny 160mA. The electricity bill will be the last thing on your mind!
raspberrypi.org/magpi
December 2015
11
Feature
INTERVIEW
WHEN WE LAUNCHED
RASPBERRY PI,
WE CHANGED THE
OF HARDWARE...
WITH THE ZERO,
WE WANTED TO
DO IT AGAIN
INTERVIEW
12
EBEN UPTON
MIKE STIMSON
December 2015
raspberrypi.org/magpi
RASPBERRY PI ZERO
Feature
RASPBERRY PI
ZER0
raspberrypi.org/magpi
December 2015
13
Feature
INTERVIEW
To take a Raspberry Pi
Model A and make it as
cheap and small as possible
RASPBERRY PI
ZER0
THE
DESIGN
Thefirstandfinaldesign,as
created by Mike: The biggest
differencebetweenthefinal
and early version is obviously
the height. When we looked
at the layout, there seemed
to be an awful lot of empty
space relatively speaking,
of course.
FROM PI TO ZERO
29
FEB RASPBERRY PI
2012
RELEASED
14
December 2015
01
JUL A NEW OPERATING
2012
SYSTEM
A couple of months or so
later, people begin receiving
their Raspberry Pis. Based on
Debian Linux, the new Raspbian
operating system is released for
the Raspberry Pi and adopted as
the recommended OS.
06
SEP LOCALLY
2012
GROWN
Manufacturing of Raspberry
Pis moves to factories in Wales
managed by Sony, bringing the
Raspberry Pi home to be a fully
British computer.
07
APR INDUSTRIAL PI
2014
raspberrypi.org/magpi
RASPBERRY PI ZERO
From inception in January to
peoples hands in November,
the Raspberry Pi Zero didnt take
very long at all, especially when
you compare it to six years for
the original Pi and over two years
for the Pi 2.
Its quite fast for us; its being
helped by the fact it is a real
Raspberry Pi, Eben tells us. So
theresnosignificantsoftware
engineering involved in doing it
it is quite an aggressive schedule,
but its good!
Commentingonthefinal
product, both Eben and Mike seem
very pleased with it, both in terms
of the technological feat theyve
WHAT IS THE
RCA VIDEO OUT
CONNECTOR FOR?
The final product has the
composite signal brought out
to a 0.1 inch pad, so if you want
to solder an RCA cable onto it
you can.
Were psyched about the
idea of people being able to
take it and solder it inside an old
television you know, get an old
television and crack it open. Turn
your television into a computer.
We think thats really good for
developing world applications.
14
JUL A BETTER
2014
RASPBERRY PI
The Raspberry Pi B+ is
announced by the Foundation, a
redesign of the Raspberry Pi that
contains more USB ports and a
better layout of its components.
The form factor will lead the way
for the Raspberry Pi.
raspberrypi.org/magpi
10
NOV A CHEAPER
2014
RASPBERRY PI
ON THE COVER
Were the first ever magazine in the world to give away
a real computer on the cover. Here are some other
notable covermount firsts through the years...
1960S
SATIRE ON RECORD
1970S
FLEXI-DISC MUSIC
1980S
NEW HARDWARE
1990S
DEMO DISCS
2000S
2015
02 A NEW
FEB
2015
Feature
RASPBERRY PI
26
NOV FROM ZERO
2015
TO HERO
December 2015
15
Feature
ZERO UNBOXED
ZER0
Lets take a
closer look at
this miniature
Raspberry Pi
UNBOXED
16
SD CARD SLOT
MINI-HDMI
MICRO-USB
POWER SUPPLY
December 2015
raspberrypi.org/magpi
RASPBERRY PI ZERO
ASSEMBLE
YOUR ZERO
FREE
CABLE
BUNDLE!
SUBSCRIBE TODAY
Page 42
PLUG IN SD CARD
Youll need to install NOOBS
(New Out Of Box Software) onto
your Raspberry Pi Zero, enabling
you to choose an operating
system. Plug your microSD card
into another computer and visit
the Raspberry Pi download site
(magpi.cc/1MYYTMo). Download
theNOOBSzipfileandunzipitonto
the SD card to install it. Once thats
done, slot it in.
GRAB YOUR
CABLES
Dont have a miniHDMI or micro-USB
converter? Weve
got you covered
We understand the cables
required to get your Raspberry
Pi Zero hooked up are not
particularly common; although
you might have a mini-HDMI
cable if you bought a really nice
camera in the last few years, the
micro-USB adaptors are much
less common. With that in mind,
Raspberry Pi has put together a
cable bundle that will help you
get your Zero working in no time.
Just head over to the Swag Store
(swag.raspberrypi.org) to get a
bundle today!
CONNECT CABLES
Plug in the mini-HDMI to HDMI
connector and make sure its
properly connected between the
Raspberry Pi Zero and your display.
Now plug in your USB adaptor,
making sure its in the correct
micro-USB port, and attach your
USB hub. You may not need a hub
with external power, but make sure
one is handy just in case.
raspberrypi.org/magpi
RASPBERRY PI
ZER0
December 2015
17
Feature
GET TO KNOW
PI ZER0
Whether this is your first ever Raspberry Pi, or you want to know more about
how the Zero actually performs, lets get to know it...
The Raspbian
interface should be
familiar to almost
anyone who has
used computers
Start or continue
to learn to code
with the excellent
resources available
in Raspbian
RASPBERRY PI
CASE
18
December 2015
ZER0
raspberrypi.org/magpi
some concessions.
If this is your first time using a
Raspberry Pi, then youll be looking
around Raspbian for the first time
too. As the operating system for
the Raspberry Pi, its built quite
simply to make it easy for people
to use. It has a standard program
menu (much like the Windows Start
menu), where you access all your
programs, and it has a lot of the
standard pieces of software youd
need already installed. Theres a
web browser, an email client, and
office software called LibreOffice.
Theyre all optimised for use on
the low-power Raspberry Pi, and
even though theres a much more
powerful Raspberry Pi alternative,
they all work absolutely fine on the
Raspberry Pi Zero.
LibreOffice especially was a
concern as its a very new piece
of software for Raspbian, but it
WIRELESS KEYBOARD
AND MOUSE COMBO
A good way to save USB space on a hub, there
are plenty of good and cheap mini keyboard
and mouse accessories that work great with
the Raspberry Pi. It also means you can
keep the Pi Zero hidden away in any
little nook or cranny you can find
and not have too many cables
signifying its presence.
raspberrypi.org/magpi
Feature
RASPBERRY PI 2
Boot to command line:
Boot to desktop:
14.50 SEC.
18.00 SEC.
RASPBERRY PI ZERO
Boot to command line:
Boot to desktop:
27.00 SEC.
43.00 SEC.
RASPBERRY PI ZERO
TURBO OVERCLOCK
Boot to command line:
Boot to desktop:
23.00 SEC.
33.00 SEC.
DESKTOP SETUP
Want to set up the Raspberry Pi Zero as a desktop
replacement? Its really quite simple most
of the hard work has been done for you already
First of all, theres the hardware setup. To make
the Pi Zero a desktop PC, you need the basic
components: a monitor, keyboard, mouse, and
wireless dongle. Connecting the monitor is easiest,
as you just need to get a mini-HDMI to HDMI cable
that can plug into a monitor with a HDMI port. For
the rest of the components, youll need to first
look into getting a USB hub that can be powered if
need be plug it into a micro-USB to USB adaptor
and then plug in your keyboard, mouse, and WiFi
dongle. If you have a spare port, you can now use
that for a USB stick.
Lastly, youll need to install Raspbian. This is
dead easy: plug a microSD card into your normal
computer (via an SD adaptor if needed) and unzip
NOOBS to it, which you can download from:
raspberrypi.org/downloads. Insert the microSD
card into the Pi Zero, plug it in with a phone
charger or an official Raspberry Pi power supply,
and then choose to install Raspbian Jessie.
December 2015
19
Feature
REMOTE ACCESS
ESSENTIAL
PI ZER0
TIPS
ZER0
Above To set a
static IP on the
desktop interface,
youll have to
go through the
sameprocess
RASPBERRY PI
Left On some
versions of
Raspbian, avahi
it may already be
installed
20
December 2015
raspberrypi.org/magpi
raspberrypi.org/magpi
raspberrypi:[Number]
Feature
[pihome]
comment= Pi Home
path=/home/pi
browseable=Yes
writeable=Yes
only guest=no
create mask=0777
directory mask=0777
public=no
Press CTRL+X, then Y and ENTER to save the file. To
start the sharing, enter the following in the terminal
and use your Pis password (raspberry by default):
$ ssh pi@raspberrypi
smbpasswd -a pi
You will need to set up a network
name (see box on previous page).
Type yes to trust the connection,
then enter the password raspberry to
log in. If using PuTTY, just type pi@
raspberrypi into the address field
and click Connect. Again, youll have
to agree to trust the Pi Zero, then use
the password raspberry.
You can now control the Raspberry
Pi in as you would normally do via
the command-line.
Above The configuration file looks complicated, but you only need
to change a couple of bits!
December 2015
21
Projects
Feature
EASY PROJECTS
WEB-CONTROLLED HTPC
Turbocharge your TV with nothing more than an HDMI cable and a wireless dongle
Project
data
CATEGORY:
Entertainment
DIFFICULTY:
Easy
Project
data
ULTRA-LOW
PROFILE FILE SERVER
CATEGORY:
Utility
DIFFICULTY:
RASPBERRY PI
ZER0
Easy
22
December 2015
INCOGNITO
COMPUTER
Keep your Raspberry Pi
Zero hidden and safe in an
inconspicuous tin of mints
Project
data
CATEGORY:
Fun
DIFFICULTY:
Easy
raspberrypi.org/magpi
Project
data
CATEGORY:
Utility
DIFFICULTY:
Medium
Feature
Projects
PI ZERO GPIO
SOLDERING
The Pi Zero comes with a full-size GPIO header but no pins. Even if
youve never touched a soldering iron before, dont panic: fitting the
pin header block can be a quick and easy job
Youll
Need
> 2.54mm Male Pin
Headers
magpi.cc/
1PCpMVa
> Soldering Iron
and Solder
magpi.cc/
1Oa5ksX
> Blu-Tack
(optional)
> HAT (optional)
Preparation
Before you plug your iron in, get your workspace ready.
That means making sure that its free from anything
breakable or flammable, that youve put something
protective down on the surface to guard against
scorching from solder splashes, and that youve laid
everything out where you can easily get to it.
The GPIO header blocks themselves, technically
known as 2.54mm male pin headers, will also need
some preparation. Theyre typically supplied in rows of
36 or more, while the Pis GPIO block is laid out in two
rows of 20. Thats easy to rectify: count 20 pins out,
then put your fingernails in the divot between pins 20
and 21 before snapping the excess off with a twist of
your fingers. Do this twice and youll have the precise
number of pins required.
raspberrypi.org/magpi
Above Take your time when soldering the pins, and be careful not
to connect two or more together with excess solder
Soldering
December 2015
23
Tutorial
ZERO PROJECTS
The titchy Pi Zero means
that bot size can be
significantly reduced
Youll
Need
> A 3D-printed
KOROBOT shell
and wheels,
or some craft
materials
magpi.cc/
1PCfwMK
> HC-SR04
ultrasonic sensor
magpi.cc/
1PCfAMs
> Two 28BYJ-48
stepper motors &
ULN2003A driver
boards
magpi.cc/
1PCfCE3
ZER0BOT
THE CASE
This bots body is
3D-printed, but
the lightweight
requirements
mean that
you can easily
make one out
of household
scraps.
24
Stepper motors
December 2015
Eyes to see
raspberrypi.org/magpi
ZEROBOT
zerobot.py
Fig 1 Theres a lot of wiring for this bot. If motors dont turn as
expected, check the GPIO pins for the coils are connected correctly
raspberrypi.org/magpi
Tutorial
Language
25
Tutorial
Feature
ZERO PROJECTS
Project
data
CATEGORY:
Fun/Home
Automation
DIFFICULTY:
Medium
Youll
Need
> GPIO headers
magpi.cc/
1PCpMVa
> Unicorn HAT
magpi.cc/
1PCpRZ5
> USB WiFi dongle
or second
Raspberry Pi
magpi.cc/
1PCpVb5
> Diffuser or
lamp housing
(optional)
>STEP-01
26
December 2015
>STEP-02
Install software
Youll need some extra software to run the Unicorn
HAT, which can pose a problem for the network-less
Pi Zero. You can either connect a USB WiFi dongle
with a USB OTG adaptor, or remove the microSD
card and insert it into a networked Raspberry Pi to
perform this step.
At the terminal, type the following three commands
to install the software youll need to control the
Unicorn HATs numerous LEDs:
raspberrypi.org/magpi
MOOD LIGHT
>STEP-03
>STEP-04
>STEP-05
Feature
Tutorial
Language
rainbow.py
>PYTHON
#!/usr/bin/env python
import unicornhat as unicorn
import time, math, colorsys
DOWNLOAD:
magpi.cc/1iUiQFV
print("Reticulating splines")
time.sleep(.5)
print("Enabled unicorn poop module!")
time.sleep(.5)
print("Pooping rainbows...")
unicorn.brightness(0.1)
i = 0.0
offset = 30
while True:
i = i + 0.3
for y in range(8):
for x in range(8):
r = 0#x * 32
g = 0#y * 32
xy = x + y / 4
r = (
math.cos((x+i)/2.0) + math.cos((y+i)/2.0)) * 64.0 + 128.0
g = (
math.sin((x+i)/1.5) + math.sin((y+i)/2.0)) * 64.0 + 128.0
b = (
math.sin((x+i)/2.0) + math.cos((y+i)/1.5)) * 64.0 + 128.0
r = max(0, min(255, r + offset))
g = max(0, min(255, g + offset))
b = max(0, min(255, b + offset))
unicorn.set_pixel(
x,y,int(r),int(g),int(b))
unicorn.show()
time.sleep(0.01)
>STEP-06
raspberrypi.org/magpi
December 2015
27
Tutorial
ZERO PROJECTS
Project
Stats
CATEGORY:
Fun
DIFFICULTY:
Medium
This enclosure
is watertight, so
take this project
in the rain or on
the open seas
The Raspberry
Pi Zero uses
Python to log
the locations to
a text file
Youll
Need
> USB GPS
receiver
> USB battery pack
> Enclosure
> Micro-USB to
USB adaptors
> GpsPrune
magpi.cc/
1PxO5Fg
ZER0 GPS
LOGGER
Track yourself anywhere on Earth by making your own tiny,
hackable GPS logger and map viewer
sing off-the-shelf components and a
Raspberry Pi Zero, you can create a small and
inexpensive GPS location logging device to
take hiking, kayaking, or in the car. And since the
Raspberry Pi is a full computer, you can even connect
a monitor, keyboard, and mouse to it so that you can
view your routes on a map and analyse the data youve
collected. In this project, youll learn about how GPS
devices deliver location information over serial, and
how you can use Python to parse and save that stream
of data to a file.
28
December 2015
GPS basics
raspberrypi.org/magpi
tail -f /var/log/syslog
Look for a recent log entry towards the bottom
that shows something like pl2303 converter now
attached to ttyUSB0. This means that the GPS
device is now connected and can be used as
/dev/ttyUSB0. To set that ports baud rate, execute
raspberrypi.org/magpi
Tutorial
cat /dev/ttyUSB0
Youll see a lot of data coming in, each line starting
with $GPRMC or similar with comma-separated data
following. These are NMEA sentences, which are
standard ways of communicating certain types of
data, including location. Its a standard set by the (US)
National Marine Electronics Association. The code will
be reading the RMC sentence, which has all the data
youll need.
December 2015
29
Tutorial
Feature
ZERO PROJECTS
Language
gps.py
try:
decimalPointPosition = data.index('.')
degrees = float(data[:decimalPointPosition-2])
minutes = float(data[decimalPointPosition-2:])/60
output = degrees + minutes
if hemisphere is 'N' or hemisphere is 'E':
return output
if hemisphere is 'S' or hemisphere is 'W':
return -output
except:
return ""
>PYTHON
DOWNLOAD:
import serial
import os
magpi.cc/1PxP0pi
firstFixFlag = False
# will go true at first fix
firstFixDate = ""
# Set up serial:
ser = serial.Serial(
port='/dev/ttyUSB0',\
baudrate=4800,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=1)
'fix_time': data[1],
'validity': data[2],
'latitude': data[3],
$GPRMC,204311.602,A,3747.3392,N,12223.8954,
W,0.50,324.18,061115,,,A*7A
The fields youll be interested in are in Fig 1.
The other fields describe speed, course, magnetic
variation, and the final field is a checksum. We wont
be using them for this project.
Press CTRL+C to get back to the command line and
proceed if youve confirmed the path of the serial port
and the baud rate.
Above GpsPrune runs on the Raspberry Pi
and lets you view the data on a map
30
DATA
DESCRIPTION
FORMAT
$GPRMC
Protocol header
204311.602
Time (UTC)
hhmmss.sss
Status
3747.3392
Latitude
ddmm.mmmm
Northern / Southern
Hemisphere indicator
N or S
12223.8954
Longitude
ddmm.mmmm
Eastern / Western
Hemisphere indicator
E or W
061115
Date
ddmmyy
December 2015
raspberrypi.org/magpi
Feature
Tutorial
raspberrypi.org/magpi
December 2015
31
Tutorial
Feature
ZERO PROJECTS
Project
data
The Pi Zero
is a great size
for wearable
technology
CATEGORY:
Utility
DIFFICULTY:
Advanced
Youll
Need
> Battery
> Display
> Real-time clock
(optional)
> Buttons and/
or a very small
joystick (optional)
> Accelerometer
(optional)
> WiFi or Bluetooth
breakout board
(optional)
PI ZER0
CONFERENCE
BADGE
Why settle for a scrappy piece of paper shoved in a plastic wallet when
you could provide your conference guests with an interactive slice of Pi?
onference badges can be very dull: a boring
piece of card with your name on the front
and if youre lucky a brief schedule on the
back. The Pi Zero offers some fantastic opportunities
for producing a truly disruptive digital badge.
The possibilities are endless, but there are a couple
of key things youll probably want to include: a
battery and a display.
As it is going to be hanging around someones
neck, weight is obviously a key issue. A standard
power bank (like the ones used for recharging
phones) would probably be too heavy, but some of
the skinnier credit card-sized examples might be
Right A small
LCD would be
sufficient to show
the delegates
name and basic
information
32
December 2015
raspberrypi.org/magpi
Feature
Tutorial
THINK OF IT LIKE
A PI VERSION OF BADGEr
These were produced for the Open Hardware
Summit in 2013 and based on the Arduino derivative
ATmega328. The Pi Zero allows you to have a fully
functional Linux computer hanging from your lanyard
and takes this concept to the next level.
Above A big TFT screen could provide a fabulous display for the badge
raspberrypi.org/magpi
RASPBERRY PI
ZER0
December 2015
33
Projects
Feature
ZERO PROJECTS
Project
data
Youll
Need
CATEGORY:
Fun
DIFFICULTY:
Easy
RASPBERRY PI
ZER0
34
December 2015
raspberrypi.org/magpi
PI ZER0
Feature
Projects
Hack a toy
Grab something
from a budget
store and open
it up youll be
amazed at what
you can do!
raspberrypi.org/magpi
December 2015
35
Tutorial
Youll
Need
> A Microstack GPS
module
magpi.cc/
1Sx0BCP
> An accelerometer
magpi.cc/
1Sx0GWX
> A 40-pin
maleheader
magpi.cc/
1Sx0FlV
> A box, some
cable ties, and
a bike
ZERO PROJECTS
POTHOLE
MAPPER
Build a map of the state of your local roads
with this bike-mounted sensor
>STEP-01
Solder on a header
RASPBERRY PI
ZER0
>STEP-02
Accelerometer
There are plenty of accelerometers available, but this
tutorial assumes youre using the LSM303 which also
includes a magnetometer. Unfortunately, the latest
official version of the LSM303 Python library is for
The Adafruit
LSM303 contains an
accelerometer and
a magnetometer
cd PiPotholeMapper
python3 Adafruit_LSM303.py
Jiggle the LSM303 around and you should see the
changing values for the x, y and z axes displayed.
>STEP-03
Find my Pi
Now add the GPS module. Once again, there are several
available. This tutorial describes the Microstack board
because it tends to be one of the cheapest. Dont worry
if you havent got a Microstack Base Board, however,
as were going to connect the GPIO pins directly to the
GPS module itself.
Power on the Pi and install the relevant software:
36
December 2015
raspberrypi.org/magpi
Tutorial
POTHOLE MAPPER
and carry out the trilateration process needed to
calculate position. You should see a red LED start to
flash on the board after a few minutes; this means it
has received a GPS lock and knows its position.
>STEP-04
The code
Language
pothole.py
>PYTHON 3
DOWNLOAD:
magpi.cc/1PKzk0j
python3 PiPotholeMapper/pothole.py
Check that a logfile is created - it should be named
pothole<date+time>.log - and that it contains GPS
and accelerometer data. Ideally, you should always
wait until the GPS module has achieved a signal lock
(as indicated by the flashing red LED) before running
the Python script, as the Microstack library does not
seem to always report the change of state correctly.
>STEP-05
Go for a ride
>STEP-06
raspberrypi.org/magpi
print('Starting potholemapper')
tmstmp = time.strftime("%Y%m%d-%H%M%S") # Detao;s for output file
f = open('pothole'+ tmstmp+'.log','w') # Open output file for writing
gps = mst.L80GPS() # Connect to the GPS
lsm = lsm.Adafruit_LSM303() # Connect to the accelerometer
time.sleep(1)
# Define useful functions
def lock_check(): # Checks to see if we have a GPS lock
try:
x = gps.get_gprmc()
return True
except mst.DataInvalidError:
return False
def getLatLon(): # Read lat and lon from GPS module
coords = gps.get_gpgll()
lat = coords['latitude']
lon = coords['longitude']
return lat,lon
def getAccel(): # Read values from accelerometer and magnetometer
acc = lsm.read()
a_x = acc[0][0]
a_y = acc[0][1]
a_z = acc[0][2]
m_x = acc[1][0]
m_y = acc[1][1]
m_z = acc[1][2]
return a_x, a_y, a_z, m_x, m_y, m_z
while True: # Main code loop
if lock_check():
while True:
try:
pos = getLatLon() # Get our position
for t in range(10):
# Take 10 readings from accelerometer for each GPS reading
bumps = getAccel()
# Write CSV formatted output
f.write(str(pos)+ ',' + str(bumps) + '\n')
time.sleep(0.1)
except mst.DataInvalidError:
f.write('No GPS lock\n')
else:
f.write('No GPS lock\n')
print('No GPS Lock')
time.sleep(60)
f.close()
December 2015
37
Tutorial
Project
data
CATEGORY:
Utility
DIFFICULTY:
Advanced
ZERO PROJECTS
TRUE
All Raspberry
Pis include
an integrated
hardware random
number generator
(HWNG), and the Pi
Zero is no exception
RANDOM
NUMBER
GENERATOR
If the security of your servers is
important to you, get the benefit of a
hardware TRNG without the expense
with this low-cost build
>STEP-01
>STEP-02
Youll
Need
> 3.3V USB TTL
serial dongle
magpi.cc/
1HVkwWn
Prepare the Pi
sudo raspi-config
Choose 8 Advanced Options, A8 Serial, and confirm
that you do not want the serial console active by
choosing No. Choose Finish then allow the Pi to
reboot; this will free up the serial port on the GPIO
header for our own use, rather than automatically
spawning a console session.
bcm2708-rng
Right You only need
to connect the TX
and Ground pins
on the Pis GPIO
header, but make
sure you havent
made a mistake!
38
December 2015
Place a blank line beneath it, then save the file and
quit by pressing CTRL+O and then CTRL+X. To avoid
having to reboot again, finish with the following
command at the console:
raspberrypi.org/magpi
>STEP-03
>STEP-05
Tutorial
QUICK TIP 1
ls /dev/tty*
From the list that appears, look for a device called
/dev/ttyACM0 or /dev/ttyUSB0; youll need this for the
next step.
Double-check
your TTL adaptor
Make sure that
any TTL adaptor
you buy is suitable
for 3.3V logic, and
take extra care
when wiring it to
the GPIO header.
>STEP-04
>STEP-06
QUICK TIP 2
Single-USB
power
If your TTL adaptor
has a 5V power
output, it may be
enough to power
the Pi Zero from a
single USB port if
wired to the GPIO
pin 2 or 4.
raspberrypi.org/magpi
ZER0
RASPBERRY PI
December 2015
39
Tutorial
Project
data
CATEGORY:
Entertainment
DIFFICULTY:
Advanced
NES
NES CONSOLE-TROLLER
CONSOLE-TROLLER
Youll
Need
> USB NES
controller
> Micro-USB male
connector
> Solder
> Drill
> RetroPie
magpi.cc/
1HVgNba
THINK OF
IT LIKE
Atari Plug n Play
TV games
The originator
of the console
in a controller
concept popular
at Christmas, but
a bit limited once
youve played
every possible
variation of Pong
and Asteroids
40
December 2015
Below We bought
this NES controller
from eBay: we had
to file down the
plastic a little bit
to slot in the Zero
raspberrypi.org/magpi
3D PI ZERO CASE
Tutorial
PRINT YOURSELF A
PI ZER0 CASE
Get a 3D-printed case today for your brand new Raspberry
Pi Zero with our pre-designed, printable case!
raspberrypi.org/magpi
Get the
3D printable
files here!
magpi.cc/
ZeroCase
December 2015
41