Getting Started With Raspberry Pi Pico Programming in IoT
This family of microcontrollers is a popular option in IoT. Let's dig into the features, alternatives, projects, and resources for programming a Raspberry Pi Pico.
Join the DZone community and get the full member experience.Join For Free
Raspberry Pi Pico is a popular microcontroller (MCU) option for hobbyists and IoT professionals alike. It holds a special place in the IoT world because of its small physical size paired with relatively high processing power and low energy consumption.
Add to that the huge mainstream adoption of its larger siblings (Raspberry Pi 1 through 4) making Raspberry Pi hardware well-known throughout the IoT world. The Pico is also exceptionally cheap, especially compared to the siblings, and you can get started with one in just minutes. You don’t need extensive programming knowledge, and there’s enough documentation online to keep you busy experimenting for weeks on end.
Read on to learn about features, alternatives, projects, and resources for programming a Raspberry Pi Pico.
Raspberry Pi Pico Family and Features
The Raspberry Pi Pico isn’t just one microcontroller. It’s a family of high-performance MCU boards. These include the Raspberry Pi Pico, Pico H, Pico W, and Pico WH. The Pico and Pico H are nearly identical, the only major difference being that the Pico H comes with pre-soldered headers — meaning places to connect wires — whereas the Pico doesn’t. So if you want to attach wires to a normal Pico, you’ll have to solder the headers yourself.
The Pico W and Pico WH add an onboard Wi-Fi module that lets the device connect to the Internet. The only difference between the Pico W and Pico WH is again that the WH has pre-soldered headers.
All four options have the following features:
Dual-Core Arm Cortex M0+ processor
Clock speed of up to 133 MHz
264 KB of SRAM and 2 MB of flash memory
One USB 1.1 port
Low-power or dormant modes
26 General Purpose Input/Output (GPIO) pins
2 x SPI, 2 x I2C, 2 x UART, 3 x ADC
Eight programmable I/O state machines
All four Pico types also have an onboard LED, which will be important for your first project. If you already have some experience with IoT and MCUs, feel free to skip down to the next section. Otherwise, here’s a rundown of what some of those features mean and why they’re important.
Let’s start with the MCU. Most of the Raspberry Pi designs are single board computers (SBCs). They have high processing power for their size, and they can perform the same basic actions as your desktop computer. They typically use RaspberryOS, which is based on the Linux operating system. By contrast, a microcontroller board doesn’t have the ability to run a full operating system. So, while on your desktop or laptop, you can open up multiple applications, an MCU will only run one application at a time.
Consider a standard smart security camera. Its only function is to stream video, so it doesn’t need a full OS. Instead, the Raspberry Pi Pico operating system will be a real-time operating system (RTOS) that only performs one action at a time.
As far as a processor, the ARM processors are extremely popular for IoT and have the highest level of energy efficiency among the most common processor types. The M0+, which is the processor the Raspberry Pi Pico employs, is even more efficient than the typical ARM processor. The 133 MHz clock speed, meaning how fast the processor executes instructions, isn’t especially fast. Still, that speed is more than adequate for a typical IoT application. For reference, a smart fitness device with multiple sensors and functions might have an absolute maximum clock speed of around 300 MHz.
When the specs show 264 KB of SRAM and 2 MB of flash memory, what that means is that the device has 264 KB of memory that only remains on the device as long as there’s power. When the device is unplugged, turned off, or the battery runs out, that memory gets destroyed. Flash memory, by contrast, is memory that stays on the device even after the power is turned off. In other words, it’s the device’s data storage repository.
The Raspberry Pi Pico also has 26 GPIO pins. A pin is basically a small hole that lets you connect external devices. For example, there’s a pin that lets you connect an extra LED light to the board if you want.
SPI, I2C, UART, and ADC are each different types of input protocols/formats. Having a wide variety is what lets you use the Pico for so many different projects and applications. Finally, a programmable I/O state machine is an input/output component that you can program for all different functions and protocols. Having eight makes the Raspberry Pi Pico particularly flexible.
Alternatives to the Raspberry Pi Pico
While the Raspberry Pi Pico stands on its own as a great option for your projects, there are plenty of other options as well. Here’s how they line up against the Pico.
Raspberry Pi Pico vs. Arduino Uno
Compared to the Arduino Uno board, which has a similar physical size, the Raspberry Pi Pico has considerably more processing power and input options. For example, while the Pico has a max clock speed of 133 MHz, the Arduino Uno only goes up to 16 MHz. It has only 32 KB of RAM and just 16 KB of flash memory. The Uno also has fewer I/O pins than the Pico, higher power consumption, and a higher price. The Arduino Uno is still a popular choice, in large part because it’s compatible with Arduino’s large suite of open source tools, libraries, and the Arduino Integrated Development Environment (IDE).
Raspberry Pi Pico vs. Zero
The Raspberry Pi Zero is a full-fledged computer in its own right, with an onboard microprocessor (MPU) rather than an MCU. It can run multiple applications simultaneously, and it has a high clock speed and 512 MB of RAM along with 8 GB of flash memory, plus 40 I/O pins to the Pico’s 28. It’s also the smallest Raspberry Pi MPU available. However, it has a considerably higher power consumption than the Raspberry Pi Pico, and it’s considerably more expensive: $15 while the Pico costs just $4.
Raspberry Pi Pico vs. ESP32
Espressif’s ESP32 MCU family is small and has around twice the processing power and speed of the Pico. While the exact specifications vary among the MCUs in the family, they generally have two additional I/O pins and twice the flash memory compared to the Pico. However, they also consume double the energy and are nearly twice as expensive. So the Pico is probably preferable for a first project. Also the ESP32 is a commercial project whereas the Pico is backed by the Raspberry Pi Foundation, a registered charity with the aim to educate.
Programming a Raspberry Pi Pico
Let’s consider a simple starter project. Just as software programmers traditionally start by writing a simple program to print “Hello World,” hardware programmers traditionally start by writing a simple program to get a light to blink on and off. In this case, you’ll set up the Raspberry Pi Pico to blink a light on and off at set intervals.
To do that, you’ll need to write some code. A Raspberry Pi Pico responds to a variety of coding languages, including C, C++, and MicroPython. MicroPython is a more lightweight and simple version of traditional Python, and it is the most common language used by Raspberry Pi Pico hobbyists. So if you know Python already, it should be easy for you to get started with MicroPython on the Raspberry Pi Pico, and you shouldn’t have any trouble with this starter project.
What You’ll Need: Hardware
For this project, you won’t need anything but the board and a USB cord. You’ll use your computer to provide power and to program the device. If you want to continue using the Pico for other projects, however, you’ll need to connect it to an external power source so it still works when it’s not plugged into your computer.
Alternatively, you can get a Raspberry Pi Pico starter kit on Amazon or another site. The kit will typically provide the Raspberry Pi Pico MCU board, a USB cable, a breadboard, some LEDs and resistors, and tutorial resources of some kind. A breadboard is just a plastic board with holes. You can plug the Pico into it and then connect other electrical components to the board as well, including a power source.
What You’ll Need: Software
You’ll also need to have certain software installed on your computer for Raspberry Pi Pico programming. Most projects use the Thonny Integrated Development Environment, which you will need to download and install. Next, download the MicroPython file to your computer, and you’re all set to start programming.
Step 1: Connect to Your Computer
Connect your USB cord to the USB port on the device. Then press the small white button on the Raspberry Pi Pico board — called the boot selection button — and plug the other end of the USB into your computer while still holding the button down. You can release the button after the device is connected to your computer. In your file explorer, you should see a new connected device called “RP1-RP2.”
Step 2: Install MicroPython on the Pico
Copy the MicroPython file you downloaded. Open the RP1-RP2 device in your file explorer and paste the MicroPython file there.
Step 3: Set the Interpreter for Thonny
Open the Thonny software. Under the Run toolbar, click “Select Interpreter.” There will be a dropdown menu with many options, one of which will be “MicroPython (Raspberry Pico).” Select that and click “OK.”
Step 4: Write Your Program
Type or paste the following code into the main window of the Thonny software:
import machine import utime led = machine.Pin(25, machine.Pin.OUT) while True: led.value(1) utime.sleep(1) led.value(0) utime.sleep(1)
This code will cause the LED light to be on for one second and then off for one second in an infinite loop, or until the device is unplugged.
Step 5: Run Program
Next hit the run button, which has a play symbol on it. A popup will ask you where you want to save the file. Click “Raspberry Pi Pico”. When you name the file, make sure to add .py at the end so that the MCU recognizes the language as MicroPython. You might, for example, name it LED.py. However, if you want the file to automatically run every time you plug in the device, name it main.py. This will tell the Pico that it’s the only program it needs to pay attention to and that it should immediately run the program on startup. Next click “OK.”
Now your light should be blinking at regular intervals. Congratulations on successfully completing your first programming project on your Raspberry Pi Pico.
The Raspberry Pi Pico is a cheap and easy option for anything from learning programming to prototyping to creating IoT devices from scratch. Its flexible design provides endless possibilities for projects that will keep you busy for some time to come.
Published at DZone with permission of Carsten Rhod Gregersen. See the original article here.
Opinions expressed by DZone contributors are their own.