Getting Started with Raspberry Pi Pico using MicroPython and C

Raspberry Pi Pico Getting Started Guide

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.

Pinecil Soldering Iron RPi Pico

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.

Raspberry Pi Pico Headers Soldered

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.

Raspberry Pi Pico Breadboard

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.

Raspberry Pi Pico Pinout DiagramThe 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:


.. and mounted as RPI-RP2 mass storage device.

Raspberry Pi Pico RPI-RP2 mass storageAfter copying pico_micropython_20210121.uf2 file to the drive, it automatically unmounts and the Pico board reboots as a serial device:


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:


Bootterm properly detected ttyACM0 port, so I just ran “bt” to access MicroPython REPL interface, and type some MicroPython commands.


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):


Raspberry Pi Pico Blink LED

and sure enough, it worked! If you want to learn more about the MicroPyton API, press Ctrl+B and type help():


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:


I then went to Run->Select interpreter… to select “MicroPython (Raspberry Pi Pico)“.

Thonny Raspberry Pi Pico

From the user interface, I could type some code to turn off the onboard LED:

Thonny Raspberry Pi Pico Sample

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,

MicroPython Save to Computer or Raspberry Pi Pico

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:


We can have a look at the blink example in pico-examples/blink/blink.c:


Good. Before modifying it, we should try to build it by first export the SDK path, and configuring the build:


We can now enter the blink directory (a new one, generated by cmake), and run make:


We now have a bunch of files:


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:


and build the program again:


Then enter boot mode and copy blink.uf2 to the board, and success!

Raspberry Pi Pico LED Blink

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.

Share this:

Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress

ROCK Pi 4C Plus
Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.
45 Comments
oldest
newest
Schmurtz
Schmurtz
3 years ago

I’m asking myself which technic has employed raspberry foundation to oblige all redactors in the world to make a try of their crappy expensive board…

Schmurtz
Schmurtz
3 years ago

Sorry I like very much your content and in a way I’m happy to see how this board works, thanks for that but I’m a little disappointed to see it everywhere, even on not right places (here it’s ok, but on ETA Prime it’s strange…). But I don’t like prices of raspberry, everybody say that it is cheap but it’s not : the price announced are always not right (or may be they should educate their distributors like their users) and for this kind of chip/boards these are expensive and very rentable for the manufacturer. But in dispite of this,… Read more »

itchy n scratchy
itchy n scratchy
3 years ago

Creating a hype seems to be an art as well.

Honestly dont see why there is no existing uC of a reputed manufacturer that rpi could have used… But well that would have been so much 2010…

itchy n scratchy
itchy n scratchy
3 years ago

Probably exactly that

Philipp Blum
3 years ago

What a shitty company/foundation. People should copy the devices. This is how this industry innovates. Take an existing design and improve it. They probably also learned from other designs. They already make a lot of money. Why do they need to increase the profits? They are not Apple or fucking Google.

Philipp Blum
3 years ago

I don’t think so. If you make money in the questionable way, but use it for the greater good, you still made money in this way. Where do you draw the line then? If they are already willing do to this, where do they draw the line? We have seen this with Google as well. The removed their “don’t be evil” ethical standard. And look at them now. Working with the military, firing people, because they openly discussed their opinion etc. etc. Not actively providing free software is already a big problem. But having your own MCU design to make… Read more »

Jon Smirl
3 years ago

100% agree the this was purposely done to maintain their monopoly.

And why are Arduino clones bad? The closed Apple model with 300% margins on hardware or the PC model with thousands of choices at many different price points. Which world do you want?

Lloyd Seaton
Lloyd Seaton
3 years ago

The programmable IO (PIO) feature seems innovative and powerful to me. Apart from the ESP32 ULP coprocessor, are there other microcontrollers with similar features?

David Willmore
David Willmore
3 years ago

The chips used in the beagle boards do. Cypress PSoC. If you want to go really back, Motorola had chips with a real time controller peripherial that seems eerily similar. That would be the 68K’s and some of the Power chips, IIRC. Every CPLD, FPGA, PLA ever.

Boris
3 years ago

Lloyd Seaton

Kendryte K210 has a powerful FPIOA functionality:

FPIOA allows users to map 255 internal functions to 48 free IOs (pins) on the chip.
This allows code to connect the internal UARTS, I2C, GPIOs, their clocks, chip/slave selects, and put it out to a hardware pin of your choice.

• Programmable IO function selection
• 8 drive strength options for outputs
• Selectable internal pull-up resistors
• Selectable internal pull-down resistors
• Schmitt trigger option for inputs
• Slew rate control for outputs
• Selectable internal input level
• etc…

Ralph R
Ralph R
2 years ago

Yeah, but the difference in price is insane! It has nothing to do with the RP2040 in any way shape or form, ;-(

Philipp Blum
3 years ago

Yep. I guess they wanted to get some experience with making their own microcontroller. I read some people on fb etc. got excited about this MCU. I don’t understand them. I guess they don’t have an idea about the topic itself and just want to tinker a bit. But a STM32F1 would be also suitable for it. It’s also cheaper. At that price point the rpi foundation could have gone with a nrf52832 like pine64 did. Or why didn’t they go with the BL602 as well? Yes, a bit risky, but making your own MCU is also very risky.

Jerry
Jerry
3 years ago

Well, Raspberry products have excellent community support and open source SDKs. I know some people who’ve used ESP8266 and ESP32. The SDK is pretty awful, maintained by some amateurish group of Chinese hackers. Can’t really recommend. If you want Olimex or Linaro style good support, you’d need to buy western MCUs. Same thing when buying DC converters etc. Adafruit and Pololu simply rule. The Chinese clones suck.

David Willmore
David Willmore
3 years ago

Thank you for demonstrating the quality of the Raspberry Pi ‘community’. Toxic, ill informed, and ignorant.

Mike
Mike
3 years ago

The ESP32 is awesome! Best thing out there. The SDK is not awful at all. The ESP32 is in more consumer products than most others. I think your comments are based not by YOU using it but other noobs that you know

Anon
Anon
3 years ago

You know the rpi pico is like $5.

Cobalt
Cobalt
3 years ago

I successfully installed the SDK. Next, I ran the following as stated in your guide: export PICO_SDK_PATH=../../pico-sdk cmake .. What directory should I be in when executing those? I was in the “/home/<username>/pico-examples/blink” folder, and the results were as you listed above. However, the existing blink.C and CMakeLists.txt files were moved back a level into “/home/<username>/pico-examples” Is this expected? should I move the blink.C and CMakeLists.txt files back into the newly created “/blink” folder? Your guide says at this point to make modifications to the blink.C file, and to build the new changes you go back to the blink folder… Read more »

David Willmore
David Willmore
3 years ago

Not that I support anyone using this board, but if you want to see the labels, just solder the headers on the other way around so that the ‘bottom’ is up when plugged in. It’s not like it uses some standard footprint that you need to ensure compatability, so flipping it doesn’t hurt anything. Sure it makes the onboard LED a little harder to see, but meh.

Occam
Occam
3 years ago

Helpful hint… Instead of flipping the board to see pin lables, just print out the Pico pin map. It’s much more informative than the board labels showing all the alternative pin functions. Bonus…the Boot Select button will be accessible. ?

Andreas
Andreas
3 years ago

How did you change the motion sensitivity and sleep timeout on your Pinecil soldering iron?

Philipp Blum
3 years ago

You get a nrf52832 for a similar price. A good old and well supported microcontroller. I don’t understand why anyone should use this MCU. Having a Dual-Core just introduces unnecessary complexity. Even the SDK doesn’t support the Dual-Core at the moment.

David Willmore
David Willmore
3 years ago

And a dual core M0+? Why use the slowest core and put on two of them? Why not just use an M3 or M4?

itchy n scratchy
itchy n scratchy
3 years ago

Is it a broadcom design? Maybe the bt radio is just disabled, only reason for two M0 cores IMO

Philipp Blum
3 years ago

That too, true. That doesn’t make sense at all. Maybe Jean-Luc is right with his point and they just introduce complexity to make it harder to copy their hardware. I already shared this with the RIOT OS core contributors. I don’t think I will even take a look this MCU. I don’t support this kind of behaviour. I actually like the fact this industry is going more into the direction of RISC-V etc. That should be the future.

dgp
dgp
3 years ago

>Having a Dual-Core just introduces unnecessary complexity

It has to be dual core otherwise it would look even more silly against a < 50 cent GigaDevice chip.

Philipp Blum
3 years ago

It looks still silly. What does the BL602 costs? You get the dev board for the same price at least ^^

Zamir
Zamir
3 years ago

For some reason, cmsis-dap + openocd (from raspberrypi) simply don’t work for me. I always get

Error: Sequence 3 not supported.

Error: Sequence 4 not supported.

Replacing cmsis-dap with ST-Link still do not help. This makes me feel that there is still much more to do for RPi foundation to make this a really hacker friendly board.

itchy n scratchy
itchy n scratchy
3 years ago

Just let the hackers do it themselves, while selling boards due to the hype 😉

zoobab
3 years ago

Don’t tell me the IOs are in 5V instead of 3.3V?

Raz
Raz
3 years ago

I clearly see that you all have a ton more competence and experience than myself, but I’ll tell you what I like with this board. My background: Hobby coder for some home projects for fun (building an electric self-driving tractor for a cooperative farm) Seems easy enough to get started with the support of Thonny and Micro Python. Enough PINOUTS for most simple DYI projects that I can think of Low enough power usage so it could run from a small power bank with integrated solar panel. I guess there will be many people sharing projects and ideas that I… Read more »

Raz
Raz
3 years ago

Thanks, I’ll check it out.
Again, thanks for a great post ?

Andreas
Andreas
3 years ago

I guess the main reason for using this other than for education and projects shared by community is that it will use eventually use a fully open source stack with no binary blobs?

tkaiser
tkaiser
3 years ago

> a fully open source stack with no binary blobs

Why do you expect something like this from RPi Trading Ltd., a company currently paying a bunch of employees just to deal with the fully closed main OS of their SBC?

Jerry
Jerry
3 years ago

MicroPython and the venerable community support would be good arguments. People also want a safe way to connect MCUs and SBCs. Having the same brand helps when choosing the best products for your use case. One of the real world problems is, people used to buy full board RPIs for flashing LEDs when they’d only require a MCU. The problem is, there were tons of MCUs to choose from. It’s just that no brand was officially supported. For example Arduino is famous but most Arduino boards are crappy clones with bad onboard regulators (don’t support up to 15V only 12V),… Read more »

Brad Zoltick
Brad Zoltick
3 years ago

Thank you posting this article.

Khadas VIM4 SBC