Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Raspberry Pi Zero Cookbook
Raspberry Pi Zero Cookbook
Raspberry Pi Zero Cookbook
Ebook783 pages4 hours

Raspberry Pi Zero Cookbook

Rating: 0 out of 5 stars

()

Read preview

About this ebook

About This Book
  • Deep dive into the components of the small yet powerful Raspberry Pi Zero
  • Get into grips with integrating various hardware, programming, and networking concepts with the so-called “cheapest computer”
  • Explore the functionalities of this $5 chip through practical recipes
Who This Book Is For

This book is for programmers and hobbyists who are eager to dive deep into the Raspberry Pi Zero. If you have basic or zero knowledge of the Raspberry Pi Zero, or if you looking for examples of ways to utilize the Raspberry Pi’s GPIO interface, then this book is ideal for you. Basic knowledge of Python will be beneficial, and experience with circuitry and electronics will be needed for the later chapters in the book.

LanguageEnglish
Release dateMar 24, 2017
ISBN9781786466471
Raspberry Pi Zero Cookbook

Related to Raspberry Pi Zero Cookbook

Related ebooks

Programming For You

View More

Related articles

Reviews for Raspberry Pi Zero Cookbook

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Raspberry Pi Zero Cookbook - Edward Snajder

    Table of Contents

    Raspberry Pi Zero Cookbook

    Credits

    About the Author

    About the Reviewer

    www.PacktPub.com

    Why subscribe?

    Customer Feedback

    Preface

    What this book covers

    What you need for this book

    Who this book is for

    Sections

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Conventions

    Reader feedback

    Customer support

    Downloading the example code

    Downloading the color images of this book 

    Errata

    Piracy

    Questions

    1. Kick-Start Your Raspberry Pi Zero

    Introduction

    Understanding the standard connectors and test points

    Getting ready

    How to do it...

    Reviewing power supply requirements

    How to do it...

    Choosing an operating system to install

    How to do it...

    Writing to an SD card with NOOBS

    Getting ready

    How to do it...

    There's more...

    Using Windows to write a Raspbian image to an SD card

    How to do it...

    Using OS X to validate a Raspbian image and write it to an SD card

    How to do it...

    Using Ubuntu 16.04 to validate a Raspbian image and write it to an SD card

    How to do it...

    Identifying RCA solder points for analog video connections

    Getting ready

    How to do it...

    Adding a USB extension over a USB OTG connector

    Getting ready

    How to do it...

    Connecting to displays and changing the configuration settings

    Getting ready

    How to do it...

    Initial Pi configuration

    Logging in to the RPZ desktop for the first time, creating users, and rebooting

    How to do it...

    There's more...

    2. Setting Up Physical and Wireless Connections

    Introduction

    Controlling the RPZ from the UART GPIO port using the console cable

    Getting ready

    How to do it...

    Adding Wi-Fi dongle over USB OTG

    Getting ready

    How to do it...

    Hacking RPZ hardware to add up permanent Wi-Fi dongle

    Getting ready

    How to do it...

    Setting Up dynamic and static IP address for RPZ

    Getting ready

    How to do it...

    Pinging from another computer over same network

    Getting ready

    How to do it...

    SSHing your RPZ from your desktop computer

    Getting ready

    How to do it...

    Sharing a screen on your desktop computer

    Getting ready

    How to do it...

    Copying different files to and from your home network

    Getting ready

    How to do it...

    Adding USB functions to Raspbian Jessie

    Getting ready

    How to do it...

    Using a virtual serial adapter on USB OTG

    Getting ready

    How to do it...

    Programming over a virtual Ethernet modem on USB OTG

    Getting ready

    How to do it...

    Making your RPZ a USB mass storage device

    Getting ready

    How to do it...

    There's more...

    3. Programming with Linux

    Navigating a filesystem and viewing and searching the contents of a directory

    Getting ready

    How to do it...

    Creating a new file, editing it in an editor, and changing ownership

    Getting ready

    How to do it...

    Renaming and copying/moving the file/folder into a new directory

    Getting ready

    How to do it...

    Installing and uninstalling a program

    Getting ready

    How to do it...

    There's more...

    Downloading a file from the Internet and deleting a file

    Getting ready

    How to do it...

    Changing to root and using superpowers

    Getting ready

    How to do it...

    Extracting a zipped file and zipping it back

    Getting ready

    How to do it...

    Searching executed code from the terminal's history

    Getting ready

    How to do it...

    Changing RPZ configuration settings from the command line

    Getting ready

    How to do it...

    There's more...

    Checking running processes and killing a process running in the background

    Getting ready

    How to do it...

    Creating our first shell program and automating a process

    Getting ready

    How to do it...

    Syncing with NTC servers to update the current time

    Getting ready

    How to do it...

    Running a background process in Linux

    Getting ready

    How to do it...

    Setting a file to run automatically on startup

    Getting ready

    How to do it...

    Using crontab to run a script automatically at predefined intervals

    Getting ready

    How to do it...

    4. Programming with Python

    Introduction

    Choosing between Python 2 and 3

    Getting ready

    How to do it...

    Installing important Python packages

    Getting ready

    How to do it...

    Creating our first Python program and running loops

    Getting ready

    How to do it...

    There's more...

    Playing with strings

    Getting ready

    How to do it...

    Plotting graphs using Python

    Getting ready

    How to do it...

    There's more...

    Sending an e-mail from a Python Script

    Getting ready

    How to do it...

    Creating a program to log and append data to a CSV file

    Getting ready

    How to do it...

    Using a Python script to upload data online to a Google spreadsheet interface

    Getting ready

    How to do it

    There's more...

    Adding help and parameters to your Python Program

    Getting ready

    How to do it...

    5. Getting Your Hands Dirty Using the GPIO Header

    Introduction

    Pin configurations and precautions for using the GPIO pins

    Getting ready

    How to do it...

    Using the GPIOs with the WiringPi library

    Getting ready

    How to do it...

    Connecting an LED with RPZ and controlling it using C, Python, and a shell

    Getting ready

    How to do it...

    Part 1 - blinking the LED using C

    Part 2 - blinking the LED using Python

    Part 3 - blinking the LED from the shell

    Basics of the UART port and getting data from the desktop on the serial port using Minicom

    Getting ready

    How to do it...

    Manually disabling the serial port

    Disabling the serial port the easy way

    Writing a Python/C program to get UART data in your code

    Getting ready

    How to do it...

    Basics of I2C and checking the I2C devices present on a port

    Getting ready

    How to do it...

    Basics of SPI and setting up an SPI module

    Getting ready

    How to do it...

    Converting a 5V signal into a 3.3V signal and slew rates

    Getting ready

    How to do it...

    Running RPZ on a battery

    Getting ready

    USB battery pack

    Prebuilt power convertor

    DIY power convertor

    How to do it...

    USB battery packs

    Prebuilt power convertor

    DIY voltage regulator

    Controlling GPIOs using a web interface

    Getting ready

    How to do it...

    How it works...

    Making RPZ a radio transmitter and sharing music

    Getting ready

    How to do it...

    Using a Node.js library to control the GPIOs

    Getting ready

    How to do it...

    Interfacing the ESP8266 WiFi module with RPZ

    Getting ready

    How to do it...

    There's more...

    6. Controlling the LEDs and Displays

    Introduction

    Setting up brightness using PWM

    Getting ready

    How to do it...

    Monitoring Twitter/e-mails to blink an LED

    Getting ready

    How to do it...

    Connecting a seven-segment number display module to the RPZ

    Getting ready

    How to do it...

    Connecting an LED matrix display to the RPZ

    Getting ready

    How to do it...

    Connecting an RGB LED and generating different colors

    Getting ready

    How to do it...

    Interfacing the 16x2 LCD display with RPZ

    Getting ready

    How to do it...

    Connecting the 74HC595N shift register to control many LEDs

    Getting ready

    How to do it...

    7. Controlling the Hardware

    Introduction

    Integrating voltage translators with the RPZ

    Getting ready

    How to do it...

    Controlling a stepper motor using a RPZ and motor drivers

    Getting ready

    How to do it...

    Connecting a DC motor and controlling its speed and direction

    Getting ready

    How to do it...

    Controlling high power AC load using relays

    Getting ready

    How to do it...

    Controlling high power DC load using MOSFETs

    Getting ready

    How to do it...

    Controlling a buzzer with an RPZ

    Getting ready

    How to do it...

    Monitoring the physical health of the RPZ hardware

    Getting ready

    How to do it...

    Interfacing the PiFace with the RPZ

    Getting ready

    How to do it...

    8. Taking Digital Inputs to the Raspberry Pi Zero

    Introduction

    Interfacing push switches

    Getting ready

    How to do it...

    Interfacing toggle switches and setting debouncing

    Getting ready

    How to do it...

    Avoiding the floating states of the input line

    Getting ready

    How to do it...

    Interfacing a keypad with the RPZ

    Getting ready

    How to do it...

    Interfacing RTC to get accurate time

    Getting ready

    How to do it...

    There's more...

    Setting up Interrupts on a toggle switch through GPIO

    Getting ready

    How to do it...

    Interfacing RFID tags with the RPZ

    Getting ready

    How to do it...

    Interfacing a GPS module with the RPZ

    Getting ready

    Enabling the Google Geocoding API

    How to do it...

    9. Interfacing Sensors with the Raspberry Pi Zero

    Interfacing any resistive sensor on GPIO

    Getting ready

    How to do it...

    Interfacing an LDO with the RPZ

    Getting ready

    How to do it...

    Interfacing an ultrasonic sensor with the RPZ

    Getting ready

    How to do it...

    Interfacing an analog-to-digital convertor to interface any analog sensor with the RPZ

    Getting ready

    How to do it...

    Interfacing an infrared receiver to read remote controls and control a TV with the RPZ

    Getting ready

    How to do it...

    Interfacing a motion sensor

    Getting ready

    How to do it...

    Interfacing a temperature humidity sensor using bit-banging

    Getting ready

    How to do it...

    Interfacing a gyroscope and accelerometer with the RPZ

    Getting ready

    How to do it...

    Pulling it all together with the Pi Sense Hat

    Getting ready

    How to do it...

    10. Cooking up Projects to Amaze the World!

    Introduction

    Setting up hardware for home automation with the RPZ

    Getting ready

    How to do it...

    Setting up software for home automation with the RPZ

    Getting ready

    How to do it...

    Making the RPZ an IoT Sensor Node

    Getting ready

    How to do it...

    Making the RPZ a media center

    Getting ready

    How to do it...

    Controlling RPZ media center using IR remote

    Getting ready

    How to do it...

    Heartrate monitoring wearable device

    Getting ready

    How to do it...

    Raspberry Pi Zero Cookbook


    Raspberry Pi Zero Cookbook

    Copyright © 2017 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 2017

    Production reference: 1210317

    Published by Packt Publishing Ltd.

    Livery Place

    35 Livery Street

    Birmingham 

    B3 2PB, UK.

    ISBN 978-1-78646-385-2

    www.packtpub.com

    Credits

    About the Author

    Edward Snajder takes on the challenges of performance, optimization, scalability, and portability for PostgreSQL, Oracle, SQL Server, and MySQL DB engines for the databases behind the Jive platform. He's got hands-on experience with system design, HA, virtualization, and distributed systems, and has had the opportunity to interface with quality engineering, support, technical operations, and professional services when not building things in engineering. In his spare time, he is an Internet of Things enthusiast and has spoken on the wonders of the Raspberry Pi at conferences and user groups. He loves his Raspberry Pis. He has also built his own 3D printer and has several 75%-complete Arduino projects.

    First, I would like to thank Packt Publishing for giving me my first chance to write a book. It has always been one of those things I’ve thought about, but until someone asks you to do it, you can put it off indefinitely. I’d also like to thank the author of Raspberry Pi Sensors, Rushi Gajjar, for putting together a fantastic outline. It covers such a broad scope of popular and applicable projects, I don’t think I would have put together such a comprehensive list together myself.

    This book also wouldn’t have been possible without the Raspberry Pi Foundation and community, which has grown so much over the years. Without the docs, forums, and discussions, I would find myself still trying to figure out some of these recipes.

    To my parents, who always enabled and encouraged me to play with technology and take things apart. My brothers continue to be a source of inspiration.

    Finally, to Lindsay, and our Shih-Tzus, Gizmo and Obi-wan, for giving me the love, time, space, and encouragement to take this on, while taking occasional walk breaks.

    About the Reviewer

    Ed Venaglia was born to be an engineer. He’s been tinkering with electronics and mechanical things since he could see over the counter at Radio Shack, back when you could actually buy radio parts there. Professionally, Ed is a software engineer, but after hours, he can be found engaged in machining, making, robotics, chemistry, and all manner of mad science in his workshop.

    I’d like to thank my friend Ed Snajder, this book’s author, for the opportunity to help with recipes in this book. I’d also like to thank my loving wife for supporting me and giving me the time to make this happen.

    www.PacktPub.com

    For support files and downloads related to your book, please visit www.PacktPub.com.

    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 service@packtpub.com 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.

    https://www.packtpub.com/mapt

    Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.

    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

    Customer Feedback

    Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1786463857.

    If you'd like to join our team of regular reviewers, you can e-mail us at customerreviews@packtpub.com. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!

    Preface

    Thank you for picking the Raspberry Pi Zero Cookbook! In this book, we explore the awesome potential of what this $5 computer can deliver. With a Raspberry Pi Zero and a few accessories, you have your hands on a miniature computer that has the same functionality, applications, and connectivity that you would get with a system costing hundreds or thousands of dollars.

    The cookbook starts with introducing the Raspberry Pi Zero and its operating system and shows ways that make the Raspberry Pi Zero unique in the Raspberry Pi family, aside from being the only one that will fit in a mint tin. From there, we move right into operating system control and simple programming, predominantly in Python. For many, this will be a revisit, though I did make every recipe as useful as possible, and almost everything in the first few chapters will be possible with just a Raspberry Pi Zero and typical accessories you would need for a computer. I've truly made every attempt not to Hello World you to death, with a few scripts you can sink your teeth into.

    Once we get through installation, configuration, and some basic programming, we will begin to learn the Raspberry Pi Zero’s GPIO Interface and how you would get it to interact with the physical world. Here is where you’ll play with sensors, motors, and controllers so you can ultimately monitor or alert anything to anyone, wherever they are.

    As a cookbook, it is intended to have each recipe possible to create without anything else. There are some recipes that are helpful or provide prerequisites to later recipes, but for the most part, with the ingredients listed, you can get right to creating whatever interests you the most. If you are more of a beginner, the recipes should be iterative, in that each recipe is similar but a bit more challenging that the last. In the end, you'll see that there are some very common patterns, and even the more seemingly complex solutions use the same simple concepts consistently with respect to the GPIO Interface.

    Electronics, sensors, and motors can add up in cost rather quickly. Some of the recipes do have specific boards I’ve used for the recipe, but in the hardware and software section, I’ve also found some kits with most of what the cookbook uses, and only for the cost of about maybe 10 Raspberry Pi Zeroes.

    I hope you enjoy the book and have enough fun that you end up with Raspberry Pi Zeroes to share; for friends and kids, just starting with computers is the most inspiring part of the $5 computer. If every kid can get their hands on one, we will soon see the days of flying cars and Star Trek replicators. I hope this book introduces enough concepts to open the doors to immensely more creative and interesting ideas than any recipe in this book.

    What this book covers

    Chapter 1, Kick-Start Your Raspberry Pi Zero, starts right out of the box and covers what you’ll need to get your Raspberry Pi running. Then we install and configure the operating system.

    Chapter 2, Setting Up Physical and Wireless Connections , covers the many ways you can communicate with your Raspberry Pi Zero, how to get it on your home network, and USB configurations that make the Raspberry Pi Zero unique to its siblings.

    Chapter 3, Programming with Linux, goes over some of the useful things to know when using a Linux operating system. From basic filesystem operation to application installation and upgrades, this will provide a set of tools you will find necessary for using your Raspberry Pi Zero.

    Chapter 4, Programming with Python, begins with a brief introduction of Python and its major versions, and must-have libraries when using Python and a Raspberry Pi. From there, we move on to creating a solution that monitors, graphs, and notifies Raspberry Pi Zero board temperatures over time.

    Chapter 5, Getting Your Hands Dirty Using the GPIO Header, gets into some basic hardware and usage of the versatile General Purpose Input Output (GPIO) interface. We’ll explore the different GPIO modes and methods of communication, using a variety of languages and tools.

    Chapter 6, Controlling the LEDs and Displays, starts with more advanced LED exercises and moves on to the control and operation of LED matrices, LCD displays, and controllers for operating several LEDs individually.

    Chapter 7, Controlling the Hardware, shows how easy it is to control motors, relays, and buzzers. It also touches on more advanced circuitry to control high-voltage systems from your Raspberry Pi. More advanced electrical experience is needed for some of these recipes.

    Chapter 8, Taking Digital Inputs to the RPZ, moves into receiving inputs from external devices for the Raspberry Pi Zero to detect. We also cover receiving triggers and data from devices: RFID scanners, GPS boards, and more!

    Chapter 9, Interfacing Sensors with RPZ, dives deeper into the options for receiving sensor data on the Raspberry Pi Zero. At this point, there shouldn’t be a device available that you can’t have your Raspberry Pi Zero talk to.

    Chapter 10, Cooking Up Projects to Amaze the World!, wraps up with bringing home automation and monitoring to your Raspberry Pi Zero to centralize all of the sensor inputs and monitors to one place.

    If you go through the chapters completely, my hope is that you will have ideas for dozens of projects where these recipes are only a component of your larger solution. I also hope that it will get you more involved in the Raspberry Pi community and its immense wealth of shared ideas and that you’ll be telling your friends about how they need to get their hands on one (and an extra for your birthday!).

    What you need for this book

    The operating system used in this book is the open source Raspbian, designed specifically for the Raspberry Pi family of computers.

    Pretty much any part in this book, outside of common components such as resistors and capacitors, is available through Adafruit. Adafruit sells high-quality components and boards that are great for all kinds of Internet of Things and maker projects. Purchasing each piece individually can add up fast; fortunately, sites such as Amazon offer some great kits that are perfect for beginners. The Elegoo Most Complete Ultimate Starter Kit is the one I used. It costs around $60 US and includes most of the sensors, displays, and motors used in this cookbook as well as common components, such as a breadboard, jumper wires, power supplies, LEDs, resistors, and capacitors. It is a great way to get started! Everything marked with an asterisk (*) is something that is included in the Elegoo kit. In the specific recipes, I will reference the equivalent Adafruit part if available.

    Hardware requirements

    Raspberry Pi Zero

    5V/1.2 A micro USB power supply

    Micro USB–OTG adapter

    4-port powered USB hub

    Micro HDMI to standard HDMI adapter and a standard HDMI cable; alternatively, a standard HDMI to micro HDMI cable

    Micro SD card, 4 GB or greater (8 GB recommended)

    USB keyboard and mouse: I used the Logitech MK270, a wireless keyboard/mouse combo that uses a single USB port.

    HDMI-compatible monitor

    Another computer and slot or adapter to write SD card images

    Raspberry Pi Cobbler and a breadboard: Adafruit has a great one, though there are a few alternatives

    Jumper cables

    USB-to-serial port adapter

    USB Wi-Fi adapter

    Standard-to-micro USB cable

    Standard LEDs, various colors*

    RGB 4-lead LED*

    Two 4xAA battery packs and batteries

    Adafruit SI4713 FM radio transmitter

    Adafruit ESP8266 Wi-Fi module

    Seven-segment LED display*

    8X8 LED matrix display*

    16x2 LCD display*

    74HC595N shift register*

    Various resistors*

    Various electrolytic and ceramic capacitors*

    P222N transistors*

    5V mechanical (sugar cube) relay*

    Stepper motor*

    DC motor*

    Piezo buzzer*

    Adafruit bidirectional level shifter

    PiFace Digital Revision 2 shield

    Push switches*

    Toggle switches

    Sixteen-digit keypad*

    Real-time clock board with battery*

    RFID scanner board*

    Adafruit Ultimate GPS breakout board

    Photoresistor*

    Ultrasonic sensor*

    Adafruit MCP3008 8-channel 10-bit analog-to-digital convertor

    Infrared receiver and remote*

    Motion sensor*

    Temperature and humidity sensor*

    Gyroscope/accelerometer*

    Heart rate/pulse sensor (via Adafruit or www.pulsesensor.com, which I used)

    Who this book is for

    This book is for programmers and hobbyists who are eager to dive deep into the Raspberry Pi Zero. If you have basic or zero knowledge of the Raspberry Pi Zero or if you're looking for examples of ways to utilize the Raspberry Pi’s GPIO interface, then this book is ideal for you. Basic knowledge of Python will be beneficial, and experience with circuitry and electronics will be needed for the later chapters in the book.

    Sections

    In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).

    To give clear instructions on how to complete a recipe, we use these sections as follows:

    Getting ready

    This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

    How to do it…

    This section contains the steps required to follow the recipe.

    How it works…

    This section usually consists of a detailed explanation of what happened in the previous section.

    There's more…

    This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

    See also

    This section provides helpful links to other useful information for the recipe.

    Conventions

    In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

    Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: As you can see, /dev/disk2s6 and /dev/disk2s1 were added. This creates two partitions on disk2 -- most SD cards will only have one, but however many there are mounted, you need to unmount them with diskutil.

    A block of code is set as follows:

    @app.route('/update_pin', methods=['POST'])

    def update_pin():

            #Read in form entry. Since they are all buttons, you should only get

    Any command-line input or output is written as follows:

    $ sudo diskutil

    Enjoying the preview?
    Page 1 of 1