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 2: Serial Interfaces and Applications

In this chapter, we are going to discuss the various communication interfaces available on the Pico. We are discussing them all in one place because they will come in handy in the projects we will discuss in the upcoming chapters. The topics discussed in this chapter will be useful for interfacing with sensors, driving actuators, displaying messages, and communicating with a Wi-Fi module to connect to your local Wi-Fi network to publish and retrieve data from the cloud. This chapter will also help you when it comes to communicating with cellular modules that act like peripheral devices.

If you are familiar with the topics discussed in this chapter, skip right ahead to the next chapter. If not, we recommend playing with the code samples discussed in this chapter to gain a basic understanding of the communication interfaces available on the Pico. In the last section of this chapter, we are going to discuss the setup of an ESP32 wireless...

Technical requirements

The following hardware is used in this chapter:

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

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

Installing requisite libraries

In this section, we will install the requisite libraries for the temperature sensor, the display, and the wireless pack. The libraries are all a part of the Adafruit CircuitPython bundle. The latest bundle can be downloaded as a ZIP file from https://circuitpython.org/libraries. We used the bundle version meant for CircuitPython 6.x.x.

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

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 Started with the Raspberry Pi Pico.

HTU21D-F temperature sensor

If you are using the HTU21D-F temperature sensor (shown in the following figure), it requires two libraries, namely adafruit_bus_device and adafruit_htu21d. From the library folder, copy over the adafruit_bus_device...

Using the UART interface to communicate between two Pico boards

In this section, we will discuss the Universal Asynchronous Receiver/Transmitter (UART) interface and its applications. We will be making use of two Pico boards to transmit a message from one Pico to another. The second Pico echoes back the incoming message. Before we jump into the example, let's take a quick look at the UART interface.

The UART interface

The UART interface is a communication protocol using two lines, namely a transmitter and a receiver (shown in the following figure). As the name suggests, the protocol is asynchronous; that is, there is no reference clock signal for the communication. The communication happens at a preset speed known as a baud rate. Typical baud rates include 9600, 19200, 38400, and so on.

The UART interface is used to communicate with sensors such as GPS sensors, dataloggers, and so on. The following figure shows the connection between two devices. The receiver (RX) of...

Interfacing sensors using the I2C interface

In this section, we will make use of the I2C (pronounced I-squared-C) interface to read temperature and humidity from the HTU21D sensor. We will read the temperature and display it on an OLED display.

Introduction to the I2C interface

In the previous section, we discussed the UART interface, which is asynchronous; that is, there is no reference clock signal. Now, we are going to discuss the I2C interface, which is synchronous and typically consists of a clock pin (for the reference clock signal) and a data pin. The following figure shows a schematic representation of devices on an I2C bus where we have a host device, which is usually a microcontroller such as the RP2040, and the sensors interface using the clock and data lines. Each sensor on the bus has a unique address that enables the host to communicate with the devices present on the bus. The following figure shows the host and peripheral devices on an I2C bus.

...

Displaying temperature data using the SPI

In this section, we will make use of an OLED display that comes with an SSD1306 driver. The Serial Peripheral Interface (SPI) will display the temperature and humidity data we retrieved in the previous section. Before we get started, let's take a quick look at the SPI bus.

The Serial Peripheral Interface (SPI)

The SPI is commonly used to interface peripherals such as microSD cards, memory devices, sensors, and so on. It is a synchronous interface like the I2C interface where we use a clock signal to keep the host and the peripheral in sync. The SPI bus typically consists of the following pins:

  • Chip Select (CS) – Enables the host to select the peripheral device with which it wants to initiate communication. The chip select pin enables multiple devices on the same bus.
  • Data Out (DO) – The pin used by the host device to transmit data to the peripheral.
  • Data In (DI) – The pin used by the host device...

Setting up the wireless pack

In this section, we will discuss the setup and testing of the wireless pack. We will be using the same wireless pack for internet connectivity across all projects.

For the sake of convenience, we will make use of Pico Omnibus – Dual Expander from Pimoroni. The Pico and the wireless pack are mounted onto the expander as shown in the following figure:

Figure 2.31 – Pico and wireless pack mounted on to a Pico Omnibus – Dual Expander

If you don't have an expander, the following pins are used to communicate with the ESP32:

  • CS → GP7
  • Ready → GP10
  • Reset → GP11
  • Clock → GP18
  • MOSI → GP19
  • MISO → GP16

A Fritzing schematic of the interface is shown in Figure 2.32:

Figure 2.32 – Interfacing an ESP32 co-processor with the Raspberry Pi Pico

To connect to a Wi-Fi network using the wireless pack, create a file...

Summary

In this chapter, we discussed interfacing two Pico boards via the UART interface and interfacing sensors via the I2C interface because they will come in handy in the projects that we are going to discuss in the upcoming chapters. The examples discussed in this chapter should help you to interface peripherals for your projects since we discussed the most used communication interfaces.

In the next chapter, we are going to discuss home automation and using the Pico in controlling appliances. You will notice that the complexity of projects increases with every chapter.

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