Charles Bell

Beginning MicroPython with the Raspberry Pi Pico

Build Electronics and IoT Projects

Charles Bell
Warsaw, VA, USA
ISBN 978-1-4842-8134-5e-ISBN 978-1-4842-8135-2
© Charles Bell 2022
This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.

This Apress imprint is published by the registered company APress Media, LLC, part of Springer Nature.

The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A.

I dedicate this book to my brother, Ronald, who is in our hearts and in our prayers daily. May God bless you and heal you.

Introduction

The Raspberry Pi is the most popular single-board computer platform available. The boards are inexpensive, run the latest fully featured operating systems, and are backed by a growing ecosystem of developers, engineers, enthusiasts, and hobbyists. But raspberrypi.org wasn’t done there. Now, we have a Raspberry Pi microcontroller called the Raspberry Pi Pico.

The Raspberry Pi Pico, with a very low cost, small form factor, and system-on-the-chip technology, is enabling many more people to learn, experience, and complete projects that would previously have required special (and expensive) hardware or having to learn a complex programming language – MicroPython. Built on the parent language, Python, MicroPython provides all of the ease of programming and hardware access in Python with a special interpreter built-in that allows the Pico to boot and execute MicroPython code. Nice.

This book presents a beginner’s guide to MicroPython and the Raspberry Pi Pico. I cover topics including a tour of the Pico and related hardware, a tutorial on MicroPython programming, what types of sensors exist, how they communicate their values (observations or events), how they can be used in your MicroPython projects, and how to build your own Internet of Things (IoT) projects.

Who This Book Is For

I have written this book with a wide variety of readers in mind. It is intended for anyone who wants to get started building their IoT projects without learning a complicated programming language or those who want to learn how to use components, devices, and sensors with a Raspberry Pi Pico.

Whether you have already been working with IoT projects, or maybe have taken an introductory electronics course, or even have read a good Apress book on the Raspberry Pi, you will get a lot out of this book. Best of all, if you ever wanted to build your own IoT solutions, this book is just what you need!

Most importantly, I wrote this book to meet my own needs. Although there are some excellent books on the Raspberry Pi, sensors, IoT, and MicroPython, I could not find a single reference that showed how to put all of these together.

About the Chapters

There are fourteen chapters, six of which include projects that demonstrate and teach key concepts of building IoT projects. There are also chapters that introduce MicroPython, present an overview of the hardware, and teach you how to program in MicroPython, and there’s also an introduction to electronics for beginners.

The project chapters are split into two groups, those projects that require discrete components to form simple IoT solutions and those that use the Grove component system to utilize Grove modules to build more sophisticated IoT projects without soldering.

Depending on your skill level with the chapter topic, you may find some of the projects easier to complete than others. It is my hope that you find the projects challenging and enlightening (but, more importantly, informative) so that you can complete your own projects. The following presents an overview of each chapter.

Chapter 1 – Introducing the Raspberry Pi Pico

In this chapter, you will learn what makes the Pico different from the Raspberry Pi boards, what a microcontroller is, and a demonstration of how easy it is to work with the Raspberry Pi Pico.

Chapter 2 – Introducing MicroPython

You will learn more about MicroPython including an overview of how to get started. The examples in this chapter are intended to give you a taste of what you can do rather than a detailed tutorial. That said, I encourage you to attempt the examples for practice. We will see a detailed tutorial for programming MicroPython in Chapter 3 and take a deeper dive into the software libraries for lower-level hardware support in Chapter 4.

Chapter 3 – How to Program in MicroPython

In this chapter, you will learn some of the basic concepts of Python programming. We begin with the building blocks of the language such as variables, modules, and basic statements and then move into the more complex concepts of flow control and data structures. While the material may seem to come at you in a rush, this tutorial on Python covers only the most fundamental knowledge of the language and how to use it on your PC and Pico. It is intended to get you started writing Python applications quickly.

If you know the basics of Python programming, feel free to skim through this chapter. However, I recommend working through the example projects at the end of the chapter, especially if you’ve not written many Python applications.

Chapter 4 – Low-Level Hardware Support

The chapter begins with a more detailed look at the GPIO header and pins. In this chapter, you will learn the MicroPython libraries available for you to use in your projects and take a brief look at the low-level hardware support in MicroPython for the Pico. Finally, you will also revisit working with breakout boards to demonstrate some of the libraries and hardware protocols and techniques discussed in previous chapters.

Chapter 5 – Electronics for Beginners

In this chapter, you will see an overview of electronics commonly found in electronics projects. I include an overview of some of the basics, descriptions of common components, and an introduction to sensors. If you are new to electronics, this chapter will give you the extra boost you need to understand the components used in the projects in this book.

If you have experience with electronics either at the hobbyist or enthusiast level or have experience or formal training in electronics, you may want to skim this chapter or read the sections with topics that you may want a refresher.

Chapter 6 – Project: Hello, World! MicroPython Style

This chapter begins the first set of three project chapters that use discrete components to build small IoT projects starting with a very simple example using LEDs and a real-time clock (RTC) module.

The chapter starts with an overview of the project, followed by a list of the required components and how to assemble the hardware. Once the hardware is explained, you will then see how to connect everything and begin writing the code. Each chapter will close with how to execute the project along with a sample of it running and suggestions for embellishing the project.

The chapter also discusses a few best practices and other practical advice for developing projects. These apply to all projects in this chapter and likely any future project you may have in mind.

Chapter 7 – Project: Pedestrian Crossing

The project in this chapter is a simulation. More specifically, you will implement a traffic light and a pedestrian walk button. The walk button is a button pedestrians can use to trigger the traffic signal to change and stop traffic so they can cross the street. This project represents a more complex example of using multiple LEDs as well as writing more sophisticated MicroPython code.

Chapter 8 – Project: Soil Moisture Monitor

The project in this chapter presents more of a challenge because it uses more complex hardware and code to explore combining data logging with data visualization. You will use an OLED made specifically for the Pico using a third-party host board. You will also see how to use an analog sensor that produces analog data that we will then have to interpret. In fact, we will rely on the analog-to-digital conversion (ADC) capabilities of our Pico to change the voltage reading to a value we can use. Finally, we will be reusing the RTC module from Chapter 6.

Chapter 9 – Introducing Grove

This chapter introduces a better alternative to using breadboards and jumper wires. There are component systems designed to unify wiring by providing a modular cabling system to connect modules. One such component system that has been around for a while and is available for use with the Pico is called Grove.

The Grove component system has a rich host of modules we can use to build our projects simply by connecting the hardware together using polarized connectors (you can’t plug them in incorrectly). Grove expands your opportunities for building more complex projects, freeing you to concentrate on the code for your project.

This chapter prepares you to use the Grove component system in the next set of projects.

Chapter 10 – Project: Sound Activated Lights

This chapter presents the second set of three projects using the Grove component system. You will learn how to build a simple project that demonstrates how to use a sound sensor and a red-green-blue (RGB) LED to display assorted colors based on the sound detected. The idea is the LED will light up whenever sound is detected, and the color will differ based on the loudness of the sound. So, you will be creating a sound detector.

Chapter 11 – Project: Simon Game

The project for this chapter is designed to demonstrate how to use analog, digital, and I2C devices on the same Grove host adapter to build a Simon game. It works very much like the original game but with an LCD for displaying messages. We will use a Grove Buzzer for sound and two Grove Dual Button modules. For the lights, we will use one Grove RGB LED module.

Chapter 12 – Example: Monitoring Your Environment

The project for this chapter is designed to demonstrate how to use analog, digital, and multiple I2C devices on the same Grove host adapter to build an indoor environment monitor. It uses several sensors to sample the air for gases and dust as well as sample the temperature and barometric pressure. As you will see, this is the most challenging of the projects in this book not only for the number of modules used but also for the complexity of the code.

Chapter 13 – Introducing IoT for the Cloud

In previous chapters, you’ve seen a number of projects, ranging from very basic to advanced in difficulty. However, the projects did not require to be connected to the Internet nor has there been any mention of using cloud services. While a complete tutorial of IoT cloud services would take several chapters, you will see an overview of what the cloud is and how it is used for IoT solutions. The chapter also presents a concise overview of the popular cloud systems for IoT as well as a short example using two of our earlier projects to give you a sense of what is possible and how projects can be modified to use the Internet.

Chapter 14 – Using ThingSpeak

This chapter presents a popular, easy-to-use, cloud-based IoT data hosting service from MathWorks called ThingSpeak (www.thingspeak.com). You will learn how ThingSpeak can allow you to gain more insights about the data. The chapter begins with a brief tour of ThingSpeak and how to get started using it in IoT projects and concludes with examples of how to expand some of the projects in the book to use ThingSpeak.

Tips for Buying Hardware

The hardware list for this book contains a number of common components such as temperature sensors, breadboards, jumper wires, and resistors. Most of these items can be found in electronics stores that stock supplies for electronics enthusiasts.

The appendix has a list of the components used from the project chapters. The appendix includes the name of each component and at least one link to an online vendor that stocks the component. In addition, I include the quantity needed for the chapter and an estimated cost. If you add up all the components needed and sum the estimated cost, the total may be a significant investment for some readers.

Downloading the Code

The code for the examples shown in this book is available on the Apress website, www.apress.com. A link can be found on the book’s information page under the Source Code/Downloads tab. This tab is located underneath the Related Titles section of the page.

Reporting Errata

Should you find a mistake in this book, please report it through the Errata tab on the book’s page at www.apress.com. You will find any previously confirmed errata in the same place.

Acknowledgments

I would like to thank all of the many talented and energetic professionals at Apress. I appreciate the understanding and patience of my managing editor, Aaron Black; coordinating editor, Jessica Vakili; and development editor, Mark Powers. Each was instrumental in the success of this project. I appreciate their encouragement and guidance as well as patience in dealing with my many questions. I would also like to thank the small army of publishing professionals at Apress for making me look so good in print. Thank you all very much!

I’d like to especially thank the technical reviewer for his patience and attention to detail. Most importantly, I want to thank my wife, Annette, for her unending patience and understanding during the many hours I spent staring into the abyss of a blank page on my laptop or conducting IoT experiments on the dining room table, in the plants on the porch, or plugged into strange places in the house.

Table of Contents
About the Author
Dr. Charles A. Bell
conducts research in emerging technologies. He is a principal software developer of the Oracle MySQL Development team. He lives in a small town in rural Virginia with his loving wife. He received his Doctor of Philosophy in Engineering from Virginia Commonwealth University in 2005.

Dr. Bell is an expert in the database field and has extensive knowledge and experience in software development and systems engineering. His research interests include microcontrollers, three-dimensional printing, database systems, software engineering, and sensor networks. He spends his limited free time as a practicing maker, focusing on microcontroller projects and refinement of three-dimensional printers.

 
About the Technical Reviewer
Sai Yamanoor

is an embedded systems engineer working for an industrial gases company in Buffalo, NY. His interests, deeply rooted in DIY and open source hardware, include developing gadgets that aid behavior modification. He has published two books with his brother, and in his spare time, he likes to build things that improve quality of life. You can find his project portfolio at http://saiyamanoor.com.

 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
44.200.27.215