Raspberry Pi Cookbook, 4th Edition

Book description

If you've started to work with Raspberry Pi, you know that Raspberry Pi's capabilities are continually expanding. The fourth edition of this popular cookbook provides more than 200 hands-on recipes (complete with code) that show you how to run this tiny low-cost computer with Linux, program it with Python, hook it up to sensors and motors, and use it with the internet of things (IoT). This new edition includes new chapters on the Raspberry Pi Pico and machine learning with the Raspberry Pi.

These easy-to-use recipes will show you, step-by-step, how to:

  • Set up your Raspberry Pi and connect to a network
  • Work with its Linux-based operating system
  • Program your Raspberry Pi with Python
  • Give your Pi "eyes" with computer vision
  • Recognize objects from video and sounds using machine learning
  • Control hardware through the GPIO connector
  • Use your Raspberry Pi to run different types of motors
  • Work with switches, keypads, and other digital inputs
  • Use sensors to measure temperature, light, and distance
  • Connect to IoT devices in various ways and automate your home
  • Use the Raspberry Pi Pico microcontroller board with your Raspberry Pi

Publisher resources

View/Submit Errata

Table of contents

  1. Preface to the Fourth Edition
    1. Using This Book
    2. Conventions Used in This Book
    3. Using Code Examples
    4. O’Reilly Online Learning
    5. How to Contact Us
    6. Acknowledgments
  2. 1. Setup and Management
    1. 1.0. Introduction
    2. 1.1. Selecting a Model of Raspberry Pi
    3. 1.2. Connecting the System
    4. 1.3. Enclosing a Raspberry Pi
    5. 1.4. Selecting a Power Supply
    6. 1.5. Selecting an Operating System
    7. 1.6. Installing an Operating System Using Raspberry Pi Imager
    8. 1.7. Booting Up Your Raspberry Pi for the First Time
    9. 1.8. Setting Up a Headless Raspberry Pi
    10. 1.9. Booting from a Real Hard Disk or USB Flash Drive
    11. 1.10. Connecting a DVI or VGA Monitor
    12. 1.11. Using a Composite Video Monitor/TV
    13. 1.12. Adjusting the Picture Size on Your Monitor
    14. 1.13. Maximizing Performance
    15. 1.14. Changing Your Password
    16. 1.15. Shutting Down Your Raspberry Pi
    17. 1.16. Installing the Raspberry Pi Camera Module
    18. 1.17. Using Bluetooth
  3. 2. Networking
    1. 2.0. Introduction
    2. 2.1. Connecting to a Wired Network
    3. 2.2. Finding Your IP Address
    4. 2.3. Setting a Static IP Address
    5. 2.4. Setting the Network Name of a Raspberry Pi
    6. 2.5. Setting Up a Wireless Connection
    7. 2.6. Connecting with a Console Lead
    8. 2.7. Controlling the Pi Remotely with SSH
    9. 2.8. Controlling the Pi Remotely with VNC
    10. 2.9. Using a Raspberry Pi for Network-Attached Storage
    11. 2.10. Setting Up a Network Printer
  4. 3. Operating System
    1. 3.0. Introduction
    2. 3.1. Browsing Files Graphically
    3. 3.2. Copying Files onto a USB Flash Drive
    4. 3.3. Starting a Terminal Session
    5. 3.4. Navigating the Filesystem Using a Terminal
    6. 3.5. Copying a File or Folder
    7. 3.6. Renaming a File or Folder
    8. 3.7. Editing a File
    9. 3.8. Viewing the Contents of a File
    10. 3.9. Creating a File Without Using an Editor
    11. 3.10. Creating a Directory
    12. 3.11. Deleting a File or Directory
    13. 3.12. Performing Tasks with Superuser Privileges
    14. 3.13. Understanding File Permissions
    15. 3.14. Changing File Permissions
    16. 3.15. Changing File Ownership
    17. 3.16. Making a Screen Capture
    18. 3.17. Installing Software with apt
    19. 3.18. Removing Software Installed with apt
    20. 3.19. Installing Python Packages with pip3
    21. 3.20. Fetching Files from the Command Line
    22. 3.21. Fetching Source Code with Git
    23. 3.22. Fetching This Book’s Accompanying Code
    24. 3.23. Running a Program Automatically on Startup
    25. 3.24. Running a Program Automatically as a Service
    26. 3.25. Running a Program Automatically at Regular Intervals
    27. 3.26. Finding a File
    28. 3.27. Using the Command-Line History
    29. 3.28. Monitoring Processor Activity
    30. 3.29. Working with File Archives
    31. 3.30. Listing Connected USB Devices
    32. 3.31. Redirecting Output from the Command Line to a File
    33. 3.32. Concatenating Files
    34. 3.33. Using Pipes
    35. 3.34. Hiding Output to the Terminal
    36. 3.35. Running Programs in the Background
    37. 3.36. Creating Command Aliases
    38. 3.37. Setting the Date and Time
    39. 3.38. Finding Out How Much Room You Have on the SD Card
    40. 3.39. Finding Out What Operating System Version You Are Running
    41. 3.40. Updating Raspberry Pi OS
  5. 4. Using Ready-Made Software
    1. 4.0. Introduction
    2. 4.1. Making a Media Center
    3. 4.2. Installing Recommended Software
    4. 4.3. Using Office Software
    5. 4.4. Running a Vintage Game Console Emulator
    6. 4.5. Turning Your Raspberry Pi into a Radio Transmitter
    7. 4.6. Editing Bitmap Images
    8. 4.7. Editing Vector Images
    9. 4.8. Using Bookshelf
    10. 4.9. Playing Internet Radio
    11. 4.10. Using Visual Studio Code
    12. 4.11. Controlling a Laser Cutter
  6. 5. Python Basics
    1. 5.0. Introduction
    2. 5.1. Deciding Between Python 2 and Python 3
    3. 5.2. Choosing a Python Editor
    4. 5.3. Editing Python Programs with Thonny
    5. 5.4. Editing Python Programs with Mu
    6. 5.5. Using the Python Console
    7. 5.6. Running Python Programs from the Terminal
    8. 5.7. Assigning Names to Values (Variables)
    9. 5.8. Displaying Output
    10. 5.9. Reading User Input
    11. 5.10. Using Arithmetic Operators
    12. 5.11. Creating Strings
    13. 5.12. Concatenating (Joining) Strings
    14. 5.13. Converting Numbers into Strings
    15. 5.14. Converting Strings into Numbers
    16. 5.15. Finding the Length of a String
    17. 5.16. Finding the Position of One String Within Another
    18. 5.17. Extracting Part of a String
    19. 5.18. Replacing One String of Characters with Another Within a String
    20. 5.19. Converting a String to Uppercase or Lowercase
    21. 5.20. Running Commands Conditionally (if)
    22. 5.21. Comparing Values
    23. 5.22. Using Logical Operators
    24. 5.23. Repeating Instructions an Exact Number of Times
    25. 5.24. Repeating Instructions Until Some Condition Changes
    26. 5.25. Breaking Out of a Loop
    27. 5.26. Defining a Function in Python
  7. 6. Python Lists and Dictionaries
    1. 6.0. Introduction
    2. 6.1. Creating a List
    3. 6.2. Accessing Elements of a List
    4. 6.3. Finding the Length of a List
    5. 6.4. Adding Elements to a List
    6. 6.5. Removing Elements from a List
    7. 6.6. Creating a List by Parsing a String
    8. 6.7. Iterating Over a List
    9. 6.8. Enumerating a List
    10. 6.9. Testing if Something Is in a List
    11. 6.10. Sorting a List
    12. 6.11. Cutting Up a List
    13. 6.12. Using Comprehensions
    14. 6.13. Creating a Dictionary
    15. 6.14. Accessing a Dictionary
    16. 6.15. Removing Entries from a Dictionary
    17. 6.16. Iterating Over Dictionaries
  8. 7. Advanced Python
    1. 7.0. Introduction
    2. 7.1. Formatting Numbers
    3. 7.2. Formatting Dates and Times
    4. 7.3. Returning More Than One Value
    5. 7.4. Defining a Class
    6. 7.5. Defining a Method
    7. 7.6. Inheritance
    8. 7.7. Writing to a File
    9. 7.8. Reading from a File
    10. 7.9. Using Pickling to Save and Load Data in a File
    11. 7.10. Handling Exceptions
    12. 7.11. Using Modules
    13. 7.12. Generating Random Numbers
    14. 7.13. Making Web Requests from Python
    15. 7.14. Specifying Command-Line Arguments in Python
    16. 7.15. Running Linux Commands from Python
    17. 7.16. Sending Email from Python
    18. 7.17. Writing a Simple Web Server in Python
    19. 7.18. Doing Nothing in Python
    20. 7.19. Doing More Than One Thing at a Time
    21. 7.20. Parsing JSON Data
    22. 7.21. Saving Dictionaries as JSON Files
    23. 7.22. Creating User Interfaces
    24. 7.23. Using Regular Expressions to Search for Patterns in Text
    25. 7.24. Using Regular Expressions to Validate Data Entry
    26. 7.25. Using Regular Expressions for Web Scraping
  9. 8. Computer Vision
    1. 8.0. Introduction
    2. 8.1. Installing OpenCV
    3. 8.2. Setting Up a USB Camera for Computer Vision
    4. 8.3. Using a Raspberry Pi Camera Module for Computer Vision
    5. 8.4. Counting Coins
    6. 8.5. Face Detection
    7. 8.6. Motion Detection
    8. 8.7. Extracting Text from an Image
  10. 9. Machine Learning
    1. 9.0. Introduction
    2. 9.1. Identifying Objects in Video with TensorFlow Lite
    3. 9.2. Reacting to Objects in Video with TensorFlow Lite
    4. 9.3. Identifying Sounds with TensorFlow Lite
    5. 9.4. Reacting to a Whistle with TensorFlow Lite
    6. 9.5. Installing Edge Impulse
    7. 9.6. Recognizing a Spoken Command (in the Cloud)
    8. 9.7. Recognizing a Spoken Command (Locally)
    9. 9.8. Responding to a Spoken Command in Python
  11. 10. Hardware Basics
    1. 10.0. Introduction
    2. 10.1. Finding Your Way Around the GPIO Connector
    3. 10.2. Using the GPIO Connector on a Raspberry Pi 400
    4. 10.3. Keeping Your Raspberry Pi Safe When Using the GPIO Connector
    5. 10.4. Setting Up I2C
    6. 10.5. Using I2C Tools
    7. 10.6. Setting Up SPI
    8. 10.7. Installing pySerial for Access to the Serial Port from Python
    9. 10.8. Installing Minicom to Test the Serial Port
    10. 10.9. Using a Breadboard with Jumper Leads
    11. 10.10. Using a Raspberry Squid
    12. 10.11. Using a Raspberry Squid Button
    13. 10.12. Converting 5V Signals to 3.3V with Two Resistors
    14. 10.13. Converting 5V Signals to 3.3V with a Level Converter Module
    15. 10.14. Powering a Raspberry Pi with a LiPo Battery
    16. 10.15. Getting Started with the Sense HAT
    17. 10.16. Getting Started with the Explorer HAT Pro
    18. 10.17. Making a HAT
    19. 10.18. Using the Raspberry Pi Zero 2 and Pi Zero 2 W
  12. 11. Controlling Hardware
    1. 11.0. Introduction
    2. 11.1. Connecting an LED
    3. 11.2. Leaving the GPIO Pins in a Safe State
    4. 11.3. Controlling the Brightness of an LED
    5. 11.4. Switching a High-Power DC Device Using a Transistor
    6. 11.5. Switching a High-Power Device Using a Relay
    7. 11.6. Switching Using a Solid-State Relay
    8. 11.7. Controlling High-Voltage AC Devices
    9. 11.8. Controlling Hardware with Android and Bluetooth
    10. 11.9. Making a User Interface to Turn Things On and Off
    11. 11.10. Making a User Interface to Control PWM Power for LEDs and Motors
    12. 11.11. Making a User Interface to Change the Color of an RGB LED
    13. 11.12. Using an Analog Meter as a Display
  13. 12. Motors
    1. 12.0. Introduction
    2. 12.1. Controlling Servomotors
    3. 12.2. Controlling Servomotors Precisely
    4. 12.3. Controlling Multiple Servomotors Precisely
    5. 12.4. Controlling the Speed of a DC Motor
    6. 12.5. Controlling the Direction of a DC Motor
    7. 12.6. Using a Unipolar Stepper Motor
    8. 12.7. Using a Bipolar Stepper Motor
    9. 12.8. Using a Stepper Motor HAT to Drive a Bipolar Stepper Motor
  14. 13. Digital Inputs
    1. 13.0. Introduction
    2. 13.1. Connecting a Push Switch
    3. 13.2. Toggling with a Push Switch
    4. 13.3. Using a Two-Position Toggle or Slide Switch
    5. 13.4. Using a Center-Off Toggle or Slide Switch
    6. 13.5. Debouncing a Button Press
    7. 13.6. Using an External Pull-Up Resistor
    8. 13.7. Using a Rotary (Quadrature) Encoder
    9. 13.8. Using a Keypad
    10. 13.9. Detecting Movement
    11. 13.10. Adding GPS to the Raspberry Pi
    12. 13.11. Intercepting Keypresses
    13. 13.12. Intercepting Mouse Movements
    14. 13.13. Giving the Raspberry Pi a Reset Button
  15. 14. Sensors
    1. 14.0. Introduction
    2. 14.1. Using Resistive Sensors
    3. 14.2. Measuring Light
    4. 14.3. Measuring Temperature with a Thermistor
    5. 14.4. Detecting Methane
    6. 14.5. Measuring Air Quality (CO2)
    7. 14.6. Measuring Soil Moisture
    8. 14.7. Measuring a Voltage
    9. 14.8. Reducing Voltages for Measurement
    10. 14.9. Using Resistive Sensors with an ADC
    11. 14.10. Measuring Temperature with an ADC
    12. 14.11. Measuring the Raspberry Pi CPU Temperature
    13. 14.12. Measuring Temperature, Humidity, and Pressure with a Sense HAT
    14. 14.13. Measuring Temperature Using a Digital Sensor
    15. 14.14. Measuring Acceleration with an MMA8452Q Module
    16. 14.15. Finding Magnetic North with the Sense HAT
    17. 14.16. Using the Inertial Measurement Unit of the Sense HAT
    18. 14.17. Sensing a Magnet with a Reed Switch
    19. 14.18. Sensing a Magnet with the Sense HAT
    20. 14.19. Measuring Distance Using Ultrasound
    21. 14.20. Measuring Distance Using a Time-of-Flight Sensor
    22. 14.21. Adding Touch Sensing to Your Raspberry Pi
    23. 14.22. Reading Smart Cards with an RFID Reader/Writer
    24. 14.23. Displaying Sensor Values
    25. 14.24. Logging to a USB Flash Drive
  16. 15. Displays
    1. 15.0. Introduction
    2. 15.1. Using a Four-Digit LED Display
    3. 15.2. Displaying Graphics on an I2C LED Matrix
    4. 15.3. Using the Sense HAT LED Matrix Display
    5. 15.4. Using an OLED Graphical Display
    6. 15.5. Using Addressable RGB LED Strips
    7. 15.6. Using the Pimoroni Unicorn HAT
    8. 15.7. Using an ePaper Display
  17. 16. Sound
    1. 16.0. Introduction
    2. 16.1. Connecting a Loudspeaker
    3. 16.2. Controlling Where Sound Is Output
    4. 16.3. Playing Audio on a Raspberry Pi Without an Audio Socket
    5. 16.4. Playing Sound from the Command Line
    6. 16.5. Playing Sound from Python
    7. 16.6. Using a USB Microphone
    8. 16.7. Making a Buzzing Sound
  18. 17. The Internet of Things
    1. 17.0. Introduction
    2. 17.1. Controlling GPIO Outputs Using a Web Interface
    3. 17.2. Displaying Sensor Readings on a Web Page
    4. 17.3. Getting Started with Node-RED
    5. 17.4. Sending Email and Other Notifications with IFTTT
    6. 17.5. Sending Tweets Using ThingSpeak
    7. 17.6. Changing LED Color Using CheerLights
    8. 17.7. Sending Sensor Data to ThingSpeak
    9. 17.8. Responding to Tweets Using Dweet and IFTTT
  19. 18. Home Automation
    1. 18.0. Introduction
    2. 18.1. Making a Raspberry Pi into a Message Broker with Mosquitto
    3. 18.2. Using Node-RED with an MQTT Server
    4. 18.3. Flashing a Sonoff WiFi Smart Switch for MQTT Use
    5. 18.4. Configuring a Sonoff WiFi Smart Switch
    6. 18.5. Using Sonoff Web Switches with MQTT
    7. 18.6. Using Flashed Sonoff Switches with Node-RED
    8. 18.7. Turning Things On and Off Using the Node-RED Dashboard
    9. 18.8. Scheduling Events with Node-RED
    10. 18.9. Publishing MQTT Messages from a Wemos D1
    11. 18.10. Using a Wemos D1 with Node-RED
  20. 19. Raspberry Pi Pico and Pico W
    1. 19.0. Introduction
    2. 19.1. Connecting a Pico or Pico W to a Computer
    3. 19.2. Using the Python Shell on a Pico
    4. 19.3. Using a Pico with a Breadboard
    5. 19.4. Using Digital Outputs on a Pico
    6. 19.5. Using Digital Inputs on a Pico
    7. 19.6. Using Analog (PWM) Outputs on a Pico
    8. 19.7. Using Analog Inputs on a Pico
    9. 19.8. Controlling a Servomotor from a Pico
    10. 19.9. Using the Pico and Pico W’s Filesystem
    11. 19.10. Making Use of the Second Core
    12. 19.11. Running a WiFi Web Server on the Pico W
    13. 19.12. Using Pico-Compatible Boards
    14. 19.13. Using the Pico on Batteries
  21. Parts and Suppliers
    1. Parts
    2. Prototyping Equipment and Kits
    3. Resistors and Capacitors
    4. Transistors and Diodes
    5. Integrated Circuits
    6. OptoElectronics
    7. Modules
    8. Miscellaneous
  22. Raspberry Pi Pinouts
    1. Raspberry Pi 400/4/3/2 Model B, B+, A+, Zero
    2. Raspberry Pi Model B revision 2, A
    3. Raspberry Pi Model B revision 1
    4. Raspberry Pi Pico
  23. Index
  24. About the Author

Product information

  • Title: Raspberry Pi Cookbook, 4th Edition
  • Author(s): Simon Monk
  • Release date: December 2022
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098130923