How To Program A Raspberry Pi Pico With MicroPython?

If you read “How To Boot Raspberry Pi Pico for the First Time?” then you might have gotten an idea about the Raspberry Pi Pico. In that post, we covered most of the basic things required to know about the Raspberry Pi Pico. Please don’t skip reading that post if you are not aware of the Raspberry Pi Pico. Just to tell about the Raspberry Pi Pico, in short, it’s not a computer. It’s a small microcontroller board that is powerful enough to run programming languages like C and Python and allows you to do pretty much anything that you need to do for small electronic projects. Well, you can’t run the standard Python compiler on the Pico to control its hardware. You need to have MicorPython installed on it to program a Raspberry Pi Pico. We will show you how to install MicroPython on a Raspberry Pi Pico and how to program a Raspberry Pi Pico with MicroPython, in this post. Let’s get started.

About MicroPython:

MicroPython is a tiny open-source Python programming language interpreter that runs on small embedded development boards. MicroPython is designed to be as compatible with regular Python as possible (within reason) to allow you to transfer code with ease from desktop computers to microcontrollers and back.

MicroPython aims to be as practical as regular Python for everyday use on microcontrollers. MicroPython implements a subset of the Python 3 standard library and is extended to include modules for microcontroller-specific hardware. MicroPython runs bare-metal on microcontrollers and thus can be used to program them without an operating system. MicroPython is also packed with many features such as an integrated development environment (IDE), object relational mapper (ORM), and support for remote code repositories.

MicroPython is licensed under the MIT license, making it free and open-source software. MicroPython is currently developed by Damien George, with the latest releases available for download from MicroPython.org.

MicroPython is a great way to get started programming microcontrollers. By being able to program in MicroPython, you can develop code on your computer and then easily transfer it to a microcontroller to run. MicroPython is also very easy to use, thanks to its small size and feature-rich set of libraries. If you’re looking for a way to get started programming microcontrollers, MicroPython is a great option.

Hardware Prerequisites:

Software Prerequisites:

  • Thonny Python IDE installed on a Raspberry Pi or other Linux/Windows computer

  • MicroPython firmware installed on the Raspberry Pi Pico

  • Basic knowledge of Python programming

The post covers the process of installing MicroPython firmware on the Pico using the Thonny Python IDE. This involves booting the Pico into BOOTSEL mode, connecting it to Thonny, and initiating the firmware installation process.

Once MicroPython is installed, the Pico can be programmed using Python code. The post shows a simple example of turning an LED on and off using Python code to control a pin on the Pico.

So in summary, the key prerequisites are:

  • Raspberry Pi Pico hardware

  • Thonny IDE with MicroPython support

  • Understanding of basic Python syntax

  • Ability to connect the Pico to Thonny in BOOTSEL mode

  • Installing the MicroPython firmware on the Pico

With these elements covered, you will have what you need to start programming the Raspberry Pi Pico using MicroPython, as covered in the blog post. Let me know if you need any clarification or have additional questions!

How To Program A Raspberry Pi Pico With MicroPython?

You can program a Raspberry Pi Pico using C, Python, or any other programming language that supports Pico. However, we will show you how to program a Raspberry Pi Pico with MicoroPython, a subsidiary of Python. Before you program a Raspberry Pi Pico, it is a must to install it. Let’s see how to install MicroPython on a Raspberry Pi Pico.

How to install MicroPython on a Raspberry Pi Pico?

There are multiple ways to install MicroPython on Raspberry Pi Pico. Let’s see how to install MicroPython on a Raspberry Pi Pico using Thonny Python IDE in this post.
1. Install MicroPython on Raspberry Pi Pico using Thonny Python IDE.2. Install MicroPython on Raspberry Pi Pico using

ColophonCopyright © 2020-2024 Raspberry Pi Ltd (formerly Raspberry Pi (Trading) Ltd.)The documentation of the RP2040 microcontroller is licensed under a Creative Commons Attribution-NoDerivatives 4.0International (CC BY-ND).build-date: 2024-02-02build-version: 169135e-dirtyAbout the SDKThroughout the text “the SDK” refers to our Raspberry Pi Pico SDK. More details about the SDK can befound in the Raspberry Pi Pico C/C++ SDK book. Source code included in the documentation isCopyright © 2020-2023 Raspberry Pi Ltd (formerly Raspberry Pi (Trading) Ltd.) and licensed under the 3-Clause BSD license.Legal disclaimer noticeTECHNICAL AND RELIABILITY DATA FOR RASPBERRY PI PRODUCTS (INCLUDING DATASHEETS) AS MODIFIED FROMTIME TO TIME (“RESOURCES”) ARE PROVIDED BY RASPBERRY PI LTD (“RPL”) “AS IS” AND ANY EXPRESS OR IMPLIEDWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESSFOR A PARTICULAR PURPOSE ARE DISCLAIMED. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW IN NOEVENT SHALL RPL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIALDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OFTHE USE OF THE RESOURCES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.RPL reserves the right to make any enhancements, improvements, corrections or any other modifications to theRESOURCES or any products described in them at any time and without further notice.The RESOURCES are intended for skilled users with suitable levels of design knowledge. Users are solely responsible fortheir selection and use of the RESOURCES and any application of the products described in them. User agrees toindemnify and hold RPL harmless against all liabilities, costs, damages or other losses arising out of their use of theRESOURCES.RPL grants users permission to use the RESOURCES solely in conjunction with the Raspberry Pi products. All other useof the RESOURCES is prohibited. No licence is granted to any other RPL or other third party intellectual property right.HIGH RISK ACTIVITIES. Raspberry Pi products are not designed, manufactured or intended for use in hazardousenvironments requiring fail safe performance, such as in the operation of nuclear facilities, aircraft navigation orcommunication systems, air traffic control, weapons systems or safety-critical applications (including life supportsystems and other medical devices), in which the failure of the products could lead directly to death, personal injury orsevere physical or environmental damage (“High Risk Activities”). RPL specifically disclaims any express or impliedwarranty of fitness for High Risk Activities and accepts no liability for use or inclusions of Raspberry Pi products in HighRisk Activities.Raspberry Pi products are provided subject to RPL’s Standard Terms. RPL’s provision of the RESOURCES does notexpand or otherwise modify RPL’s Standard Terms including but not limited to the disclaimers and warrantiesexpressed in them.Raspberry Pi Pico Python SDKLegal disclaimer notice 1
Table of contentsColophon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Legal disclaimer notice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11. The MicroPython Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.1. Getting MicroPython for RP2040 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.2. Installing MicroPython on Raspberry Pi Pico. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.3. Building MicroPython From Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42. Connecting to the MicroPython REPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.1. Connecting from a Raspberry Pi over USB. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.2. Connecting from a Raspberry Pi using UART . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.3. Connecting from a Mac . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.4. Say “Hello World”. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.5. Blink an LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.6. What next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113. The RP2040 Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123.1. Blinking an LED Forever (Timer) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123.2. UART. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.3. ADC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.4. Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143.5. Multicore Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153.6. I2C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153.7. SPI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.8. PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173.9. PIO Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183.9.1. IRQ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203.9.2. WS2812 LED (NeoPixel) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223.9.3. UART TX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233.9.4. SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243.9.5. PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253.9.6. Using pioasm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263.10. Wireless Support. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274. Using an Integrated Development Environment (IDE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304.1. Using Thonny. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304.1.1. Blinking the LED from Thonny . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314.2. Using Visual Studio Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324.3. Using rshell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Appendix A: App Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Using a SSD1306-based OLED graphics display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Wiring information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35List of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Bill of Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Using a SH1106-based OLED graphics display. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Wiring information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37List of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38Bill of Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Using PIO to drive a set of NeoPixel Ring (WS2812 LEDs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Wiring information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43List of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43Bill of Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Using UART on the Raspberry Pi Pico . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46Wiring information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46List of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46Bill of Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47Appendix B: Documentation release history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48Raspberry Pi Pico Python SDKTable of contents 2
Chapter 1. The MicroPythonEnvironmentPython is the fastest way to get started with embedded software on Raspberry Pi Pico. This book is about the officialMicroPython port for RP2040-based microcontroller boards.MicroPython is a Python 3 implementation for microcontrollers and small embedded systems. Because MicroPython ishighly efficient, and RP2040 is designed with a disproportionate amount of system memory and processing power forits price, MicroPython is a serious tool for embedded systems development, which does not compromise onapproachability.For exceptionally demanding pieces of software, you can fall back on the SDK (covered in Getting started withRaspberry Pi Pico and Raspberry Pi Pico C/C++ SDK), or an external C module added to your MicroPython firmware, towring out the very last drop of performance. For every other project, MicroPython handles a lot of heavy lifting for you,and lets you focus on writing the code that adds value to your project. The accelerated floating point libraries inRP2040’s on-board ROM storage are used automatically by your Python code, so you should find arithmeticperformance quite snappy.Most on-chip hardware is exposed through the standard machine module, so existing MicroPython projects can be portedwithout too much trouble. The second processor core is exposed through the _thread module.RP2040 has some unique hardware you won’t find on other microcontrollers, with the programmable I/O system (PIO)being the prime example of this: a versatile hardware subsystem that lets you create new I/O interfaces and run them athigh speed. In the rp2 module you will find a comprehensive PIO library which lets you write new PIO programs at theMicroPython prompt, and interact with them in real time, to develop interfaces for new or unusual pieces of hardware(or indeed if you just find yourself wanting an extra few serial ports).MicroPython implements the entire Python 3.4 syntax (including exceptions, with, yield from, etc., and additionally async/await keywords from Python 3.5). The following core datatypes are provided: str (including basic Unicode support),bytes, bytearray, tuple, list, dict, set, frozenset, array.array, collections.namedtuple, classes and instances. Builtin modulesinclude sys, time, and struct, etc. Note that only a subset of Python 3 functionality is implemented for the data types andmodules.MicroPython can execute scripts in textual source form (.py files) or from precompiled bytecode, in both cases eitherfrom an on-device filesystem or “frozen” into the MicroPython executable.1.1. Getting MicroPython for RP2040Pre-built BinaryA pre-built binary of the latest MicroPython firmware is available from the MicroPython section of thedocumentation.The fastest way to get MicroPython is to download the pre-built release binary from the Documentation pages. If youcan’t or don’t want to use the pre-built release for example, if you want to develop a C module for MicroPython youcan follow the instructions in Section 1.3 to get the source code for MicroPython, which you can use to build your ownMicroPython firmware binary.Raspberry Pi Pico Python SDK1.1. Getting MicroPython for RP2040 3
1.2. Installing MicroPython on Raspberry Pi PicoRaspberry Pi Pico has a BOOTSEL mode for programming firmware over the USB port. Holding the BOOTSEL buttonwhen powering up your board will put it into a special mode where it appears as a USB Mass Storage Device. First makesure your Raspberry Pi Pico is not plugged into any source of power: disconnect the micro USB cable if plugged in, anddisconnect any other wires that might be providing power to the board, e.g. through the VSYS or VBUS pin. Now holddown the BOOTSEL button, and plug in the micro USB cable (which hopefully has the other end plugged into yourcomputer).A drive called RPI-RP2 should pop up. Go ahead and drag the MicroPython firmware.uf2 file onto this drive. Thisprograms the MicroPython firmware onto the flash memory on your Raspberry Pi Pico.It should take a few seconds to program the UF2 file into the flash. The board will automatically reboot when finished,causing the RPI-RP2 drive to disappear, and boot into MicroPython.By default, MicroPython doesn’t do anything when it first boots. It sits and waits for you to type in further instructions.Chapter 2 shows how you can connect with the MicroPython firmware now running on your board. You can read on tosee how a custom MicroPython firmware file can be built from the source code.The Getting started with Raspberry Pi Pico book has detailed instructions on getting your Raspberry Pi Pico intoBOOTSEL mode and loading UF2 files, in case you are having trouble. There is also a section going over loading ELFfiles with the debugger, in case your board doesn’t have an easy way of entering BOOTSEL, or you would like to debug aMicroPython C module you are developing. NOTEIf you are not following these instructions on a Raspberry Pi Pico, you may not have a BOOTSEL button. If this is thecase, you should check if there is some other way of grounding the flash CS pin, such as a jumper, to tell RP2040 toenter the BOOTSEL mode on boot. If there is no such method, you can load code using the Serial Wire Debuginterface.1.3. Building MicroPython From SourceThe prebuilt binary which can be downloaded from the MicroPython section of the documentation should serve mostuse cases, but you can build your own MicroPython firmware from source if you’d like to customise its low-levelaspects. TIPIf you have already downloaded and installed a prebuilt MicroPython UF2 file, you can skip ahead to Chapter 2 tostart using your board. IMPORTANTThese instructions for getting and building MicroPython assume you are using Raspberry Pi OS running on aRaspberry Pi 4, or an equivalent Debian-based Linux distribution running on another platform.It’s a good idea to create a pico directory to keep all pico-related checkouts in. These instructions create a pico directoryat /home/pi/pico.$ cd ~/$ mkdir pico$ cd picoThen clone the micropython git repository. These instructions will fetch the latest version of the source code.Raspberry Pi Pico Python SDK1.2. Installing MicroPython on Raspberry Pi Pico 4
$ git clone https://github.com/micropython/micropython.git –branch masterOnce the download has finished, the source code for MicroPython should be in a new directory called micropython. TheMicroPython repository also contains pointers (submodules) to specific versions of libraries it needs to run on aparticular board, like the SDK in the case of RP2040. We need to fetch these submodules too:$ cd micropython$ make -C ports/rp2 submodules NOTEThe following instructions assume that you are using a Raspberry Pi Pico. Some details may differ if you are buildingfirmware for a different RP2040-based board. The board vendor should detail any extra steps needed to buildfirmware for that particular board. The version we’re building here is fairly generic, but there might be somedifferences like putting the default serial port on different pins, or including extra modules to drive that board’shardware.To build the RP2040 MicroPython port, you’ll need to install some extra tools. To build projects you’ll need CMake, across-platform tool used to build the software, and the GNU Embedded Toolchain for Arm, which turns MicroPython’s Csource code into a binary program RP2040’s processors can understand. build-essential is a bundle of tools you needto build code native to your own machine this is needed for some internal tools in MicroPython and the SDK. You caninstall all of these via apt from the command line. Anything you already have installed will be ignored by apt.$ sudo apt update$ sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi build-essentialFirst we need to bootstrap a special tool for MicroPython builds, that ships with the source code:$ make -C mpy-crossWe can now build the port we need for RP2040, that is, the version of MicroPython that has specific support for ourchip.$ cd ports/rp2$ makeIf everything went well, there will be a new directory called build-PICO (ports/rp2/build-PICO relative to the micropythondirectory), which contains the new firmware binaries. The most important ones are:firmware.uf2 A UF2 binary file which can dragged onto the RPI-RP2 drive that pops up once your Raspberry PiPico is in BOOTSEL mode. The firmware binary you can download from the documentation pageis a UF2 file, because they’re the easiest to install.firmware.elf A different type of binary file, which can be loaded by a debugger (such as gdb with openocd) overRP2040’s SWD debug port. This is useful for debugging either a native C module you’ve added toMicroPython, or the MicroPython core interpreter itself. The actual binary contents is the sameas firmware.uf2.You can take a look inside your new firmware.uf2 using picotool, see the Appendix B in the Getting started withRaspberry Pi Pico Python SDK1.3. Building MicroPython From Source 5

mode and flash the UF2 file.3. Building MicroPython From Source.

See also  How Hackers Can Steal Your Passwords!
Step 1. Boot the Raspberry Pi Pico in BOOTSEL mode

We are going to show you to boot the Raspberry Pi Pico in BOOTSEL mode by connecting to a Raspberry Pi. However, you can boot the Pico using any other Windows or Linux computer either if you don’t have Raspberry Pi with you. The reason to connect the Pico to the Pi is that Thonny Python IDE comes in Pi as part of the default packages. If you want to do this using Windows or Linux computer, then you should install Thonny Python IDE on your computer.
Connect your Raspberry Pi Pico to the Raspberry Pi through a micro USB cable by holding the BOOTSEL button (White color button near the micro USB connector) located on the Pico board.
Your Pico will be mounted as a mass storage drive on the Raspberry Pi as an RPI-RP2 drive.

Boot The Raspberry Pi Pico In Bootsel Mode 1
Step 2. Open Thonny Python IDE on the Raspberry Pi Pico

Click on the Raspberry Pi icon then go to Programming and click on  Thonny Python IDE

Open Thonny Python Ide On The Raspberry Pi Pico
Step 3. Connect your Raspberry Pi Pico to Thonny Python IDE

It is simple to connect your Pico to Thonny Python IDE. All you need to do is, click on the Python at the bottom right corner and select the MicorPython (Raspberry Pi Pico)

Connect Your Raspberry Pi Pico To Thonny Python Ide
Step 4.  Install MicroPython on the Raspberry Pi Pico

Immediately after you select the MicorPython (Raspberry Pi Pico), the installation wizard will pop up. Click on the Install button to begin the installation of MicruPython.

Install Micropython On The Raspberry Pi Pico
Step 5. Raspberry Pi Pico is ready to program

After the completion of the installation of MicroPython, Raspberry Pi Pico is ready to program.

Raspberry Pi Pico Is Ready To Program
Step 6. Turn on the LED on the Raspberry Pi Pico board using Python

We will show you how to use Pin number 25 to operate LED on the Raspberry Pi Pico board.
You just need to import Pin and create a variable method name ‘led’. And, call the high method to turn the LED on.
>>> from machine import Pin>>> led = Pin(25,Pin.OUT)>>> led.high()>>>

Turn On The Led On Raspberry Pi Pico Board Using Python
Step 7. Turn off the LED on the Raspberry Pi Pico board using Python

Call log method to turn the LED off
>>> from machine import Pin>>> led = Pin(25,Pin.OUT)>>> led.high()>>> led.low()>>>

Turn Off The Led On Raspberry Pi Pico Board Using Python
Step 8. Blink the LED on the Raspberry Pi Pico using the Python script

Run this small script to blink the LED on your Pico board.
from machine import Pinimport utime
led = Pin(25,Pin.OUT)
delay = 0.25
while True:led.high()utime.sleep(delay)led.low()utime.sleep(delay)

Ezgif Com Gif Maker

We have shown you how to blink the LED as an example to program your Raspberry Pi Pico board to control hardware. You can program any PIN using Python like this.

Leave a Reply

Your email address will not be published. Required fields are marked *