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

Implementing the "Hello World!" example

In this section, we will discuss a hello world example. In any programming language, the first coding exercise is to print "Hello World" to the screen. We will discuss this example using MicroPython and CircuitPython.

Reset button for the Pico (optional)

The Pico does not come with a reset button. In order to reset the Pico, you would have to disconnect and reconnect the USB cable, which can be tedious. This can be overcome by adding a reset button between pin numbers 28 and 30 (note the extra button right next to the microcontroller), as shown in the following image (you can get this button fixture from this link: https://bit.ly/3w8AmO2):

Figure 1.13 – Reset button on the Pico

Figure 1.13 – Reset button on the Pico

The reset button makes it easier to restart the program during development. Adding a reset button is optional and it is not recommended if you are not comfortable with soldering. In the next section, we will be writing our first program for the Pico.

MicroPython

We will be using Thonny IDE (IDE stands for integrated development environment) for this example. Thonny IDE can be downloaded from https://thonny.org. The IDE is available for Windows, Mac, and Linux operating systems.

If you use a Raspberry Pi for your programming needs, it comes pre-installed with Thonny. You can launch Thonny from Menu | Programming and scroll down the drop-down button to find Thonny Python IDE, as illustrated in the following screenshot:

Figure 1.14 – Thonny IDE location on the Raspberry Pi Desktop

Figure 1.14 – Thonny IDE location on the Raspberry Pi Desktop

Now, let's prepare the Pico by flashing the MicroPython binary.

Flashing the MicroPython binary

The Pico needs to be connected to the computer in a specific manner to flash the banner. The steps to flash the binary are outlined here:

  1. The first step is to download the binary from https://bit.ly/31nBMFW.
  2. Press and hold the BOOTSEL button shown in the following screenshot as you connect a MicroUSB cable to your Pico. Ensure that the other end of the USB cable is connected to the computer.
Figure 1.15 – Pico BOOTSEL button

Figure 1.15 – Pico BOOTSEL button

  1. The Pico should enumerate as a storage device on your computer, which should look like something similar to this:
Figure 1.16 – Raspberry Pi Pico enumerated as a storage device

Figure 1.16 – Raspberry Pi Pico enumerated as a storage device

  1. Next, copy over the binary onto the storage drive. The Pico will reset itself and we are ready to write programs in MicroPython.

Now, we are ready to write our first program in MicroPython!

Writing our first program

Let's launch Thonny and take it for a test drive! You will need to take the following steps:

  1. Launch Thonny and you should see a window, as shown in the following screenshot:
Figure 1.17 – Thonny IDE

Figure 1.17 – Thonny IDE

  1. We are going to be running our first program using the interpreter running on your Pico. If you are not familiar with Python interpreters, they enable the testing of your code as you write them. Go to Run, and then click on Select Interpreter. From here, you will select the following options, as shown in Figure 1.18:
    • MicroPython (Raspberry Pi Pico)
    • < Try to detect port automatically >

These options can be seen in the following screenshot:

Figure 1.18 – Selecting the Python interpreter

Figure 1.18 – Selecting the Python interpreter

  1. Now, you should see the Python interpreter, as shown in the following screenshot. This is the MicroPython interpreter running on the Pico. Let's take it for a test drive.
Figure 1.19 – Python interpreter

Figure 1.19 – Python interpreter

  1. At the prompt >>>, type the following line of code:
    >>> print("Hello World")

You should see the following output:

Figure 1.20 – Interpreter output

Figure 1.20 – Interpreter output

You have just finished writing your first program on your Raspberry Pi Pico! In the next example, we will write a script that starts running continuously when the Pico is powered by a USB cable.

Previous PageNext Page
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