Getting Started with Raspberry Pi Pico using MicroPython and C
Raspberry Pi Pico board was just launched last Thursday, but thanks to Cytron I received a sample a few hours after the announcement, and I’ve now had time to play with the board using MicroPython and C programming language.
I went to the official documentation to get started, but I had to look around to achieve what I wanted to do, namely blinking some LEDs, so I’ll document my experience with my own getting started guide for Raspberry Pi Pico using a computer running Ubuntu 20.04 operating system. The instructions will be similar for Windows and Mac OS.
Preparing the hardware
In theory, we could just get started with the board alone, but since I got some headers with my board, I also took the opportunity to try out Pine64 Pinecil soldering iron powered by MINIX NEO P2 USB-C power supply.
The soldering iron worked great for about one minute, and then I started to have problems with soldering… Looking a the screen I could see Zzzz and the temperature dropped. I did not move the soldering enough so it failed to detect any activity and entered into sleep. Changing the motion sensitivity or sleep timeout can easily fix this issue, and I could complete the task at hand.
It would be a shame not to use those headers so I inserted Raspberry Pi Pico into a breadboard and added an LED with the accompanying circuitry.
5V is connected to VBUS (pin 40), GND to pin 38, and I decide to use the GPIO the closest to the LED namely GP15 (pin 20). The GPIO markings on Raspberry Pi Pico are only shown on the bottom of the board, so when the board is connected to a breadboard a pinout diagram helps.
The hardware setup is now complete, and all I need is a Micro USB to USB-A cable to connect the board to my laptop.
MicroPython on Raspberry Pi Pico
We should first copy MicroPython firmware to the board. To do so we can download the latest firmware from the getting started guide (pico_micropython_20210121.uf2 at the time of the review), then press the BOOTSEL key on the board while connecting to a computer with a USB port, and release the key after connection. I did so, but nothing happened. That’s because I used my bicycle headlight’s USB cable that lacks the data line… So I went to select a proper micro USB to USB-Type-A cable, and Raspberry Pi Pico was properly recognized on my laptop:
Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[422070.155550]usb1-2:newfull-speed USB device number16using xhci_hcd
At this point, the getting started guide on Raspberry Pi website is not very useful, and we have to switch the Python SDK documentation (PDF).
The documentation uses minicom for the serial console, but now I prefer Bootterm since it’s easier to use. In any case, if you program the board in Linux, make sure your current user is added to the dialout group, or you’ll need to run all programs as root:
Shell
1
sudo usermod-a-Gdialout$(whoami)
Bootterm properly detected ttyACM0 port, so I just ran “bt” to access MicroPython REPL interface, and type some MicroPython commands.
No port specified,using ttyACM0(last registered).Use-ltolist ports.
Trying port ttyACM0...Connected tottyACM0 at115200bps.
Escape character is'Ctrl-]'.Useescape followed by'?'forhelp.
>>>print("Hello, Pico!")
Hello,Pico!
>>>from machine import Pin
>>>led=Pin(25,Pin.OUT)
>>>led.value(1)
>>>led2=Pin(15,Pin.OUT)
>>>led2.value(1)
>>>
I could turn on the onboard LED (GP25), but when I did the same for the LED on the breadboard (GP15), it did not work. I rechecked my circuit, and used a multimeter to check the voltage levels, and found out GP25 was still pulled low. A web search showed GP15 was disabled in CircuitPython because it would interfere with the USB interface.
This is on purpose, GP15 should not be used, it is used by the internal USB peripheral
Ah… I suppose it’s the same for MicroPython, so I switch to the neighboring pin (GP14, pin 19):
Shell
1
2
>>>led2=Pin(14,Pin.OUT)
>>>led2.value(1)
and sure enough, it worked! If you want to learn more about the MicroPyton API, press Ctrl+B and type help():
Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
>>>
raw REPL;CTRL-Btoexit
>
MicroPython v1.13-290-g556ae7914 on2021-01-21;Raspberry Pi Pico with RP2040
Type"help()"formoreinformation.
>>>help()
Welcome toMicroPython!
Foronline help please visit https://micropython.org/help/.
Foraccess tothe hardware usethe'machine'module.RP2 specific commands
are inthe'rp2'module.
Quick overview of some objects:
machine.Pin(pin)--getapin,eg machine.Pin(0)
machine.Pin(pin,m,[p])--getapin andconfigure it forIO modem,pull modep
Pins are numbered0-29,and26-29have ADC capabilities
Pin IO modes are:Pin.IN,Pin.OUT,Pin.ALT
Pin pull modes are:Pin.PULL_UP,Pin.PULL_DOWN
Useful control commands:
CTRL-C--interruptarunning program
CTRL-D--onablank line,doasoft reset of the board
CTRL-E--onablank line,enter pastemode
Forfurther help onaspecific object,typehelp(obj)
Foralist of available modules,typehelp('modules')
We can exit bootterm with “Ctrl+]” followed by “q”. But what if we want to save our Python program on the board and run it automatically? Unless, I missed there’s nothing about that in the Python SDK documentation, so I had to jump to a third guide to find out the best way is to use Thonny.
Ubuntu 20.04 does have Thonny 3.2.7 in its repository, which we can install with sudo apt install thonny, but it does not support Raspberry Pi Pico, so instead I installed the latest version of the program (v3.3.3) with pip3:
Shell
1
pip3 install thonny
I then went to Run->Select interpreter… to select “MicroPython (Raspberry Pi Pico)“.
From the user interface, I could type some code to turn off the onboard LED:
In order to alternatively blink the onboard LED and breadboard LED with a one-second interval, I copied and modified some code from the Python SDK documentation:
I save the file as blink.py on my PC, and it ran fine. But if you’d like to run the code without a PC, it’s possible to save it to Raspberry Pi Pico. Click on File->Save copy, then on “Raspberry Pi Pico” button,
and save the program as main.py. You can now run the program automatically by connecting your board to any USB power source.
C/C++ on Raspberry Pi Pico
Let’s try the “C/C++ SDK” that is basically all C language, except some tools written in C++. We can go back to the official Getting Started documentation, where we are asked to copy blink.uf2 to Raspberry Pi Pico while in boot mode, and it does blink the onboard LED. It works, and it’s super easy, as the binary is pre-built, but what we really want to do is modify the source code, and build our own binary to blink both the internal and external LED.
So we’ll have to install the C/C++ SDK, dependencies, and samples as follows:
The most important ones are blink.uf2 that we can copy to Raspberry Pi Pico to run the program, and blink.elf that can be used by the debugger (OpenOCD + GDB), but that is out of the scope of this getting started guide.
Now that we know how to compile a C program for the Pico board, let’s modify the blink.c example to alternatively turn on and off the onboard LED and external LED connected to pin 14:
C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "pico/stdlib.h"
intmain(){
constuint LED_PIN=25;/* onboard LED */
constuint LED2_PIN=14;/* external LED */
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN,GPIO_OUT);
gpio_init(LED2_PIN);
gpio_set_dir(LED2_PIN,GPIO_OUT);
while(true){
gpio_put(LED_PIN,1);
gpio_put(LED2_PIN,0);
sleep_ms(1000);
gpio_put(LED_PIN,0);
gpio_put(LED2_PIN,1);
sleep_ms(1000);
}
}
and build the program again:
Shell
1
2
cdblink
make
Then enter boot mode and copy blink.uf2 to the board, and success!
We can achieve the same results with either a C or Python program. We’ll have a look at RP2040’s PIO (Programmable IO) interface in an upcoming post, as AFAICT it’s what differentiates Raspberry Pi RP2040 most from other microcontrollers.
I’d like to thank Cytron for sending Raspberry Pi Pico for review. If you are based in ASEAN, you can purchase the board on their store for $4.98, or if you have some time there are offering a $5 carrier board for Raspberry Pi Pico for the same price with the board already soldered that’s expected to ship on February 10th, and available worldwide, not only in ASEAN.
Jean-Luc started CNX Software in 2010 as a part-time endeavor, before quitting his job as a software engineering manager, and starting to write daily news, and reviews full time later in 2011.
This website uses cookies to improve your experience. We'll assume you're ok with this, but if you don't like these, you can remove them Accept
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the ...
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.