Reader small image

You're reading from  Raspberry Pi Pico DIY Workshop

Product typeBook
Published inMay 2022
PublisherPackt
ISBN-139781801814812
Edition1st Edition
Right arrow
Authors (2):
Sai Yamanoor
Sai Yamanoor
author image
Sai Yamanoor

Sai Yamanoor is an embedded systems engineer working for a private startup school in the San Francisco Bay Area, where he builds devices that help students achieve their full potential. He completed his undergraduate work in mechatronics engineering from Sri Krishna College of Engineering and Technology, Coimbatore, India and his graduate studies in mechanical engineering at Carnegie Mellon University, Pittsburgh PA. His interests, deeply rooted in DIY and open software and hardware cultures, include developing gadgets and apps that improve the quality of life, Internet of Things, crowdfunding, education, and new technologies. In his spare time, he plays with various devices and architectures, such as the Raspberry Pi, Arduino, Galileo, Android devices and others. Sai has earlier published a book titled Raspberry Pi Mechatronics Projects.
Read more about Sai Yamanoor

Srihari Yamanoor
Srihari Yamanoor
author image
Srihari Yamanoor

Srihari Yamanoor is a mechanical engineer, working on medical devices, sustainability, and robotics in the San Francisco Bay Area. He completed his undergraduate studies in mechanical engineering from PSG College of Technology, Coimbatore, India and graduate studies in mechanical engineering at Stanford University. He is certified in SolidWorks, simulation, sustainable design, PDM as well as in quality and reliability engineering and auditing. His has a wide range of interests, from DIY, crowdfunding, AI, travelling, photography to gardening and ecology.
Read more about Srihari Yamanoor

View More author details
Right arrow

Chapter 3: Home Automation Projects

In this chapter, we are going to discuss simple home automation projects using the Raspberry Pi Pico. We are going to work on simple projects that could be executed over a weekend and improve our immediate surroundings. We will get started by learning to interface sensors to detect events at home. Then, we will start publishing the events detected by the sensor to the cloud. We will also discuss controlling appliances at home using the Pico.

While we will be discussing the projects using the Raspberry Pi Pico, we will also introduce the RP2040 Connect from Arduino. The examples discussed in this chapter could be executed using an RP2040 Connect with some modifications to the code.

We are going to cover the following main topics in this chapter:

  • Installing the requisite libraries
  • Interfacing sensors
  • Controlling appliances
  • Publishing sensor events to the cloud
  • Controlling LED strips
  • Introducing the RP2040 Connect...

Technical requirements

The following components are required for this chapter:

The code samples discussed in this chapter are available from here: https://github.com/PacktPublishing/Raspberry-Pi-Pico-DIY-Workshop/tree/main/chapter_03.

Code in Action videos for this chapter can be viewed at https://bit.ly/3kLsX2B.

CircuitPython Installation

We are assuming that you have installed CircuitPython on your Pico. If you are not familiar with the installation process, we recommend following the installation process from Chapter 1, Getting...

Installing the requisite libraries

In this section, we will install the requisite libraries needed for this chapter, including the NeoPixel LED and the wireless pack to the Raspberry Pi Pico. The libraries are all part of the Adafruit CircuitPython bundle. The latest bundle can be downloaded as a ZIP file from https://circuitpython.org/libraries. We have used the bundle version intended for CircuitPython 6.x.x.

After downloading the ZIP file, extract their contents so that we can copy the libraries we need for the project.

NeoPixel

We need the neopixel.mpy binary to control the NeoPixel LED. Copy over the binary to the lib folder.

Wireless pack

We need the adafruit_esp32spi library for the wireless pack. Copy over the folder (with the same name) to the lib folder of your Pico. We will also need the adafruit_requests.mpy binary from the bundle.

Setting Up the Wireless Pack

In Chapter 2, Serial Interfaces and Applications, we discussed setting up the wireless pack...

Interfacing sensors

In this section, we are going to discuss interfacing sensors on the Pico, in particular, a reed switch. We picked a reed switch because we have all been in situations where we either cannot hear a door open, or you are not aware when someone doesn't close a door properly. We are going to discuss detecting such situations using a reed switch.

A reed switch is a type of switch that closes and makes contact in the presence of a magnetic field. Figure 3.1 shows a reed switch sold by Adafruit that could be used as a door sensor. The sensor consists of two pieces, namely, the magnet and the reed switch.

Figure 3.1 – Reed switch

The magnet is usually installed on the door while the reed switch is installed on the door frame. When the magnet is within 13 mm of the reed switch, the switch is closed. We are going to make use of this principle to detect the door state.

The steps to interface a door sensor with the Pico include the...

Controlling appliances

In this section, we are going to control appliances using a product called IoT Power Relay (shown in the following image). We are going to make some minor tweaks to the code sample discussed in the previous section to turn on a light when the door is open and vice-versa.

Appliances Controlled by the Power Relay

We recommend using the power relay for your projects because it offers a safe way to control AC appliances. We recommend controlling simple resistive loads such as a floor lamp.

The following photo shows the IoT power relay. The terminal on the bottom side of the picture is used to control the relay.

Figure 3.11 – IoT power relay (image source: sparkfun.com. License: CC by 2.0)

Figure 3.11 – IoT power relay (image source: sparkfun.com. License: CC by 2.0)

The IoT power relay enables controlling alternating current (AC) appliances with a 3.3 V signal. It comes with the requisite protection to safely interface the Pico with the relay. We will be making use of the GP11 pin to control the...

Publishing sensor events to the cloud

In this section, we are going to discuss publishing sensor events to the cloud. This is especially important to receive email alerts and mobile notifications when a door is open. We are assuming that you have set up your wireless pack following the instructions from Chapter 2, Serial Interfaces and Applications.

Setting up Adafruit IO

In this section, we will discuss setting up Adafruit IO for our project. Adafruit IO is a cloud service provided by Adafruit Industries that enables the Raspberry Pi Pico to be connected to the internet and sensor events published, as well as appliances controlled from anywhere on the web. The setup steps include the following:

Additional Resource

We wrote this section using https://learn.adafruit.com/ as a resource. If you run into problems, refer to the links provided in each step.

  1. The first step is to create an account for yourself at https://io.adafruit.com/. While the basic plan is free, there...

Controlling LED strips

In this section, we will interface a NeoPixel LED strip with the Pico. NeoPixels are serially connected LEDs that are individually addressable. The NeoPixels come in a variety of form factors, namely, horizontal bars, flexible strips, and circular rings. We will be discussing this example with a NeoPixel ring (link: https://bit.ly/3cn5pxj). We are discussing the LED strip interface because they could make a great holiday lighting project or an ambient light controller. Our favorite project using Pico and the NeoPixel LED is this table lamp (link: https://bit.ly/3r6iIdJ).

The examples discussed in this section make use of helper functions from Adafruit.

The NeoPixel ring requires three connections, namely, Data IN or DIN, Power, and Ground (connections shown in Figure 3.23). While the Power pin is connected to the 3.3V pin of the Pico, the Data IN pin is connected to GP10 of the Pico.

Figure 3.15 – Schematic for connecting the...

Introducing the RP2040 Connect

In this section, we will introduce the RP2040 Connect from the Arduino foundation. The RP2040 Connect is a variant of the Pico in the Arduino Nano form factor. We like this board because it comes with Bluetooth and Wi-Fi connectivity. It also comes with an onboard accelerometer, gyroscope, RGB LED, and a microphone. This provides tremendous opportunities for prototyping with the RP2040 Connect. The following diagram shows the pinouts for the RP2040 Connect (available for purchase from here: https://bit.ly/2YZClrY):

Figure 3.16 – RP2040 Connect pinout (image source: Arduino.cc. License: CC-by-SA)

In the next section, we will discuss installing CircuitPython on the RP2040 Connect.

Installing CircuitPython on the RP2040 Connect

Installing CircuitPython is the same as what we discussed in Chapter 1, Getting Started with the Raspberry Pi Pico, except that there will be an extra step in these instructions where we enumerate the flash drive. The steps to install CircuitPython on the RP2040 Connect are as follows:

  1. Set up your RP2040 Connect on a breadboard, as shown in the following photo:

Figure 3.17 – RP2040 Connect on a breadboard

  1. Tap on the reset button twice to put the device into bootloader mode (highlighted using the rectangle).

Figure 3.18 – Reset button on the RP2040 Connect

  1. The device enumerates as a storage device. Download the CircuitPython binary from https://bit.ly/34R29pJ and copy it over to the storage device.
  2. Upon flashing, the device resets itself and enumerates as a storage device with the name of CIRCUITPY.

Figure 3.19 – RP2040 Connect...

Summary

In this chapter, we discussed interfacing sensors to the General Purpose Input/Output (GPIO) pins of the Pico, turning appliances on/off using a relay box, interfacing LED strips to the Pico, and publishing sensor events to the cloud. We discussed controlling an LED strip using a variant of the Pico, namely, the RP2040 Connect.

The examples discussed in this chapter were primarily chosen as examples that could be discussed as individual weekend projects.

In the next chapter, we will have fun with gardening. See you shortly!

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi Pico DIY Workshop
Published in: May 2022Publisher: PacktISBN-13: 9781801814812
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Sai Yamanoor

Sai Yamanoor is an embedded systems engineer working for a private startup school in the San Francisco Bay Area, where he builds devices that help students achieve their full potential. He completed his undergraduate work in mechatronics engineering from Sri Krishna College of Engineering and Technology, Coimbatore, India and his graduate studies in mechanical engineering at Carnegie Mellon University, Pittsburgh PA. His interests, deeply rooted in DIY and open software and hardware cultures, include developing gadgets and apps that improve the quality of life, Internet of Things, crowdfunding, education, and new technologies. In his spare time, he plays with various devices and architectures, such as the Raspberry Pi, Arduino, Galileo, Android devices and others. Sai has earlier published a book titled Raspberry Pi Mechatronics Projects.
Read more about Sai Yamanoor

author image
Srihari Yamanoor

Srihari Yamanoor is a mechanical engineer, working on medical devices, sustainability, and robotics in the San Francisco Bay Area. He completed his undergraduate studies in mechanical engineering from PSG College of Technology, Coimbatore, India and graduate studies in mechanical engineering at Stanford University. He is certified in SolidWorks, simulation, sustainable design, PDM as well as in quality and reliability engineering and auditing. His has a wide range of interests, from DIY, crowdfunding, AI, travelling, photography to gardening and ecology.
Read more about Srihari Yamanoor