Getting Started with Raspberry Pi Zero Get started with the smallest, cheapest, and highest-utility Pi ever—Raspberry Pi Zero Richard Grimmett BIRMINGHAM - MUMBAI Getting Started with Raspberry Pi Zero Copyright © 2016 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: March 2016 Production reference: 1210316 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78646-946-5 www.packtpub.com Credits Author Project Coordinator Richard Grimmett Francina Pinto Reviewer Proofreader David Whale Safis Editing Commissioning Editor Indexer Kartikey Pandey Priya Sane Acquisition Editor Graphics Tushar Gupta Disha Haria Content Development Editor Production Coordinator Merint Thomas Mathew Shantanu N. Zagade Technical Editor Cover Work Saurabh Malhotra Shantanu N. Zagade Copy Editors Kevin McGowan Sneha Singh About the Author Richard Grimmett has always been fascinated by computers and electronics since his very first programming project that used Fortran on punch cards. He has a bachelor's and master's degree in electrical engineering and a PhD in leadership studies. He also has 26 years of experience in the radar and telecommunications industries, and even has one of the original brick phones. He now teaches computer science and electrical engineering at the Brigham Young University, Idaho, where his office is filled with his numerous robotics projects. This book is the result of working with the wonderful students at BYU-Idaho. It also wouldn't be possible without the help of my wonderful wife, Jeanne. About the Reviewer David Whale is a software developer living in Essex, UK. He started coding as a schoolboy aged 11, inspired by the school science technician to build his own computer from a kit and quickly progressed to writing machine code programs because they were "small and fast". These early experiments led to some of his code being used inside a saleable educational word game when he was only 13. He has been developing software professionally ever since, mainly writing small and fast code that goes into electronic products, including automated machinery, electric cars, mobile phones, energy meters, and wireless doorbells. These days, he runs his own software consultancy called Thinking Binaries and spends nearly half of his time helping to design the next wave of the Internet called The Internet of Things, by connecting electronic devices to it. The rest of the time he volunteers for The Institution of Engineering and Technology, running training courses for teachers, designing and running workshops and clubs for school children, and being busy with his Raspberry Pi, BBC micro:bit and Arduino. He was the technical editor for the book Adventures in Raspberry Pi and the co-author of the book Adventures in Minecraft, and is a regular reviewer and editor of technical books from a number of book publishers. I was really pleased to be asked to review this great new book of projects using the Raspberry Pi Zero. The size of the Pi Zero makes it ideal for building into other products. In this book, Richard Grimmett takes us on an amazing journey of circuit bending, coding, and innovating using this tiny computer! But don't stop here; the projects in this book will give you the skills you need and inspire you to come up with many new ideas yourself! www.PacktPub.com eBooks, discount offers, and more Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub. com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via a web browser Table of Contents Preface v Chapter 1: Getting Started with Raspberry Pi Zero 1 Setting up the Raspberry Pi Zero 2 Powering the board 3 Hooking up a keyboard, mouse, and display 4 Installing the operating system 9 Adding Internet access 14 Accessing your Raspberry Pi Zero from your host PC 17 Summary 30 Chapter 2: Programming Raspberry Pi Zero 31 Powering up Raspberry Pi Zero with Linux 31 Creating, editing, and saving files 37 Creating and running Python programs 39 Basic programming constructs on Raspberry Pi Zero 43 The if statement 43 The while statement 45 Working with functions 46 Libraries/modules in Python 48 Summary 50 Chapter 3: Accessing the GPIO Pins on Raspberry Pi Zero 51 The GPIO capability of Raspberry Pi Zero 51 Simple GPIO digital voltage output 53 Raspberry Pi Zero and LED code 59 Adding a sonar sensor 61 Raspberry Pi Zero and the sonar sensor code 64 Connecting a digital compass to Raspberry Pi Zero 66 Accessing the compass programmatically 70 Summary 75 [ i ] Table of Contents Chapter 4: Building and Controlling a Simple Wheeled Robot 77 The basic platform 77 Controlling an H-bridge interface to the DC motors 80 Controlling your mobile platform programmatically using the Raspberry Pi Zero 83 Controlling the speed of your motors with PWM 86 Using a motor controller board to control the DC motors 89 Controlling the vehicle using the Raspberry Pi Zero in Python 92 Planning your path 96 Summary 100 Chapter 5: Building a Robot That Can Walk 101 Robots that can walk 101 How servo motors work 102 Building the quadruped platform 103 Using a servo controller to control the servos 107 Communicating between the servo controller and a PC 110 Connecting the servo controller to the Raspberry Pi Zero 114 Creating a program in Linux to control your quadruped 118 Summary 121 Chapter 6: Adding Voice Recognition and Speech – A Voice Activated Robot 123 Communication between the Raspberry Pi Zero and the robot 125 Giving your robot voice commands 129 Using eSpeak to allow your robot to respond with an audible voice 137 Using pocketsphinx to accept your voice commands 138 Interpreting commands and initiating actions 144 Summary 147 Chapter 7: Adding Raspberry Pi Zero to an RC Vehicle 149 Configuring and controlling an RC car with Raspberry Pi Zero 150 Controlling the RC car in Python 160 Accessing the RC car remotely 163 Connecting a webcam 166 Summary 167 [ ii ] Table of Contents Chapter 8: Playing Rock, Paper, or Scissors with Raspberry Pi Zero 169 A robotic hand 170 Moving the robotic hand 172 Connecting the servo controller to the Raspberry Pi Zero 175 Creating a program on Raspberry Pi Zero so that you can control your hand 178 Installing a USB camera on Raspberry Pi Zero 180 Downloading and installing OpenCV – a fully featured vision library 183 Gesture detection 185 Summary 189 Chapter 9: Adding Raspberry Pi Zero to a Quadcopter 191 Constructing the platform 192 Mission planning software 196 Summary 205 Index 207 [ iii ]