Book Image

Raspberry Pi Pico DIY Workshop

By : Sai Yamanoor, Srihari Yamanoor
Book Image

Raspberry Pi Pico DIY Workshop

By: Sai Yamanoor, Srihari Yamanoor

Overview of this book

The Raspberry Pi Pico is the latest addition to the Raspberry Pi family of products. Introduced by the Raspberry Pi Foundation, based on their RP2040 chip, it is a tiny, fast microcontroller that packs enough punch to power an extensive range of applications. Raspberry Pi Pico DIY Workshop will help you get started with your own Pico and leverage its features to develop innovative products. This book begins with an introduction to the Raspberry Pi Pico, giving you a thorough understanding of the RP2040's peripherals and different development boards for the Pico designed and manufactured by various organizations. You'll explore add-on hardware and programming language options available for the Pico. Next, you'll focus on practical skills, starting with a simple LED blinking project and building up to a giant seven-segment display, while working with application examples such as citizen science displays, digital health, and robots. You'll also work on exciting projects around gardening, building a weather station, tracking air quality, hacking your personal health, and building a robot, along with discovering tips and tricks to give you the confidence needed to make the best use of RP2040. By the end of this Raspberry Pi book, you'll have built a solid foundation in product development using the RP2040, acquired a skillset crucial for embedded device development, and have a robot that you built yourself.
Table of Contents (17 chapters)
1
Section 1: An Introduction to the Pico
6
Section 2: Learning by Making
10
Section 3: Advanced Topics

Implementing the LED-blinking example

In the previous section, we used an interpreter to write our program. An interpreter can be helpful when testing code or finding out more information about the modules being imported. In this example, we will discuss writing a script that runs automatically on powering the Pico. We will discuss the "hello world" example of getting started with electronics where we will blink an LED at a 1-second interval.

The Pico comes with a green LED on the board and its location is shown in the following image:

Figure 1.21 – Pico LED location

Figure 1.21 – Pico LED location

We will make this LED blink at a 1-second interval—that is, we will turn it on and off for a second. In order to write this program, we will need the machine and utime modules.

According to the Pico datasheet, the onboard LED is connected to general-purpose I/O (GPIO) pin number 25. We will proceed as follows:

  • We will use the Pin class from the machine module to control the LED.
  • We will be using the utime module to introduce the delay between turning ON and turning OFF the LED.

You will now need to take the following steps:

  1. Let's take a look at the following code sample:
    from machine import Pin
    import utime
    led = Pin(25, Pin.OUT)
    while True:
        led.toggle()
        utime.sleep(1)

    Exercise

    Develop a practice of actively using an interpreter while you are writing code. In the interpreter, import the machine and utime modules, try executing help(machine), help(utime), and find out for yourself.

  2. Create a new file and enter the preceding code snippet shown in Step 1. Set the file location as Raspberry Pi Pico, as seen in the following screenshot to the right:
Figure 1.22 – File location

Figure 1.22 – File location

  1. Save the file as main.py and your code should automatically begin execution.
  2. Try disconnecting the USB cable and reconnecting it. You will notice that the script starts running automatically.
  3. To stop the code execution, click on the STOP button located on the toolbar, as shown in the following screenshot:
Figure 1.23 – Pressing STOP to interrupt execution

Figure 1.23 – Pressing STOP to interrupt execution

  1. To resume execution, click the Run Current Script button, which is indicated by the green play button indicated at the top of the page, as shown in the following screenshot:
    Figure 1.24 – Clicking Run Current Script to resume execution

.

Figure 1.24 – Clicking Run Current Script to resume execution

In the next section, we will take a closer look at the code.

Description of the code sample

In this section, we will discuss in detail the code sample shown previously, as follows:

  1. We get started by importing the utime module and the Pin class from the machine module, as follows:
    from machine import Pin
    import utime

    GPIO

    A GPIO pin can be used as an input pin or an output pin. When you use a GPIO pin as an output pin, you can set it to HIGH or LOW. Likewise, when you use it as an input pin, you can read whether the pin is HIGH or LOW. In this example, we are turning the LED on/off by alternating between HIGH and LOW states.

  2. Next, we declare the GPIO pin 25 as an output pin using an object belonging to the Pin class. The first argument in the following code snippet refers to the pin number (pin 25), while the second argument sets the pin as an output pin:
    led = Pin(25, Pin.OUT)
  3. Now, we need to blink an LED at a 1-second interval. We are going to do this inside an infinite loop.
  4. The led object has a toggle() function that toggles the pin between the ON and OFF states.
  5. We will introduce a 1-second delay by calling the sleep function after toggling the pin, as follows:
    while True:
        led.toggle()
        utime.sleep(1)

Congratulations on your first step with the Raspberry Pi Pico! Now, we will discuss the same example in CircuitPython.

CircuitPython example

In this section, we will discuss using CircuitPython for programming the Pico. We will discuss the same LED-blinking example using Pico. We will also install the Mu IDE to the program by using CircuitPython.

Flashing the CircuitPython binary

In this section, we will flash the CircuitPython binary onto the Pico. The installation process is the same as for MicroPython. The binary can be downloaded from https://bit.ly/31pnLI4.

Once you have downloaded the binary, follow the instructions from the MicroPython section.

Coding with Mu

In this section, we will install the Mu IDE. The IDE is available for download from https://bit.ly/3ruxDKW. The IDE is available for Windows, Linux, and Mac operating systems.

Raspberry Pi installation

On a Raspberry Pi, Mu can be installed as follows:

  1. Go to Menu | Preferences | Recommended Software | Select Mu to choose Mu from the list of software to install.
  2. Mu can be launched from Menu | Programming | Mu.

In the next section, we will review writing our first program with Mu.

Launching Mu

The steps to writing a program with Mu include the following:

  1. Connect the Pico to your computer/Raspberry Pi using a USB cable.
  2. Once Mu is launched, we need to set the programming mode. Since we are programming in CircuitPython, we will set it to CircuitPython, as shown in the following screenshot:
Figure 1.25 – Selecting programming mode

Figure 1.25 – Selecting programming mode

  1. You can also change the programming mode from the main window of the IDE, as shown in the following screenshot:
Figure 1.26 – Changing Python mode

Figure 1.26 – Changing Python mode

Note the Pico being automatically detected in the preceding screenshot.

  1. Click the Serial button to launch access to the Python interpreter, as shown here:
Figure 1.27 – Serial button location

Figure 1.27 – Serial button location

  1. Launch the Python interpreter by pressing Ctrl + C, and it should take you to the >>> REPL prompt, as shown toward the bottom of the following screenshot:
Figure 1.28 – CircuitPython interpreter on Pico

Figure 1.28 – CircuitPython interpreter on Pico

Finally, in this process, repeat the Hello World! example from the MicroPython section by using the interpreter.

Second LED-blinking example

We will discuss the same LED-blinking example using CircuitPython and observe the differences between the two flavors of Python. Let's take a quick look at the following code snippet:

import time
import board
import digitalio
 
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
 
while True:
    led.value = True
    time.sleep(1)
    led.value = False
    time.sleep(2)

Let's review the code sample, as follows:

  1. We get started by importing the time, board, and digitalio modules. The time module is used to introduce a delay between turning the LED ON and OFF.
  2. The board module contains definitions of the pins and peripherals specific to the board. In this example, we are making use of the LED constant from the board module to drive the onboard LED on the Raspberry Pi Pico.
  3. The digitalio module provides access to the Pico's peripherals. In this example, we need to declare the LED pin (GPIO pin 25) as an output pin:
    led = digitalio.DigitalInOut(board.LED)
    led.direction = digitalio.Direction.OUTPUT
  4. In the first line of the preceding code snippet, we are declaring led as an instance of the DigitalInOut class.
  5. In the second line, we are setting the direction of the led pin to be an output pin. We are making use of the Direction class from the digitalio module.
  6. Next, we enter an infinite loop where we turn the LED on/off, as follows:
    while True:
        led.value = True
        time.sleep(1)
        led.value = False
        time.sleep(2)
  7. In the first line of the while loop, we set the value to be True. This turns ON the LED. This is followed by a 1-second delay. This is achieved by calling time.sleep(1).
  8. In the third line of the while loop, we set the value to be False. This turns OFF the LED. This is also followed by a 1-second delay.
  9. We want the script to launch upon reset. Load the code.py file located on the Pico that is currently enumerated on your computer as a storage device. The Load button is located on the top toolbar.
  10. Type the code sample we discussed into code.py and save it.
  11. Press Ctrl + D from the CircuitPython interpreter and you should notice the LED blinking on the Pico.

Congratulations on writing your first CircuitPython program for the Raspberry Pi Pico!

CircuitPython or MicroPython?

In this chapter, we discussed examples with both CircuitPython and MicroPython. The examples were somewhat identical and share a similar structure. What are their differences and which flavor of Python should you use for your development?

The short answer is that it is up to you. For the sake of consistency, we will be discussing all examples in CircuitPython using the Mu IDE.

Both implementations have a wide user base and libraries for add-on hardware. CircuitPython was spun off MicroPython by Adafruit. CircuitPython can be helpful while using sensor breakout boards from Adafruit Industries.

Thonny versus Mu IDE

In this chapter, you might have noticed that we used Thonny for the MicroPython example and Mu for the CircuitPython example. We wanted to demonstrate the various tools available for the Raspberry Pi Pico. You can even use a simple text editor for your development. We will show you how to save and upload your code to the Pico.

In the next section, we will discuss add-on hardware for the Pico.