Introduction to Raspberry Pi Pico programming and MicroPython
Introduction
When discussing embedded system prototyping, two popular platforms come to mind: these are Arduino and Raspberry Pi platforms. Arduino is usually the recommended option for beginners because it's easy to set up and used for simple embedded prototyping projects, while the Raspberry Pi is for more seasoned users and for more advanced and high-processing projects.
Raspberry Pi is popular for making single-board computers (development boards with the capability of a full-sized computer in a tiny form factor): with each upgrade getting more powerful than the last. But, in January 2021, the Raspberry Pi in the UK released the Raspberry Pi Pico board. This time, instead of a single-board computer running Linux, the Pico is a microcontroller board like the Arduino boards.
Let's take a look at the breakdown of today's topic:
What we will learn
Raspberry Pi Pico hardware
Installation and configuration of Thonny IDE
What is MicroPython?
Adding MicroPython firmware to the Pico board
Program Raspberry Pi Pico onboard LED to blink using MicroPython
Hardware components we will need
Laptop computer or PC (with an internet connection)
Raspberry Pi Pico board
Micro USB cable
1. Raspberry Pi Pico Hardware specs:
21mm x 51mm form factor.
RP2040 microcontroller chip designed by Raspberry Pi in the UK.
Dual-core Arm Cortex-M0+ processor, flexible clock running up to 133MHz.
264KB on-chip SRAM
2MB on-board QSPI Flash
26 multifunction GPIO pins, including 3 analog pins.
2 x UART, 2 x SPI controllers, 2x I2C controllers, 16 x PWM channels.
1 x USB 1.1 controller and PHY, with host and device support.
8 x Programmable I/O (PIO) state machines for custom peripheral support.
Supported input power 1.8-5.5V DC.
Operating temperature -20*C to +85*C.
The castellated module allows soldering directly to carrier boards.
Drag-and-drop programming using mass storage over USB.
Low-power sleep and dormant modes.
Accurate on-chip clock.
Temperature sensor.
Accelerated integer and floating-point libraries on-chip.
The one downside of the Raspberry Pi Pico is the absence of wireless connectivity
(no on-board Bluetooth and Wi-Fi modules), but this can always be handled by connecting to external wireless modules. (or you can use the Pico W)
Fortunately, on 30 June 2022 Raspberry Pi launched the Raspberry Pi Pico W (W for WiFi). The Pico W is exactly the same as the previous Pico board: but with the added feature of Bluetooth and WiFi.
We will discuss the Pi Pico W in subsequent posts (remember to tune in)
2. Install and configure Thonny IDE
Install the latest version of Thony on your OS (Windows, macOS, and Linux)
Head to the Thonny website and download the Thonny version for your OS. (We'll be using a Windows PC for this tutorial.)
Once you have successfully installed Thonny on your computer, open Thonny and you should see a similar screen to the one below:
3. What is MicroPython?
MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimized to run on microcontrollers and in constrained environments.
MicroPython is packed full of advanced features such as an interactive prompt, arbitrary precision integers, closures, list comprehension, generators, exception handling, and more. Yet it is compact enough to fit and run within just 256k of code space and 16k of RAM.
MicroPython aims to be as compatible with normal Python as possible to allow you to transfer code with ease from the desktop to a microcontroller or embedded system.
4. Adding MicroPython firmware to the Pico board
If you have never used MicroPython on your Raspberry Pi Pico, you will need to add the MicroPython firmware. You can do this by just dragging and dropping a file onto the board. A downloadable UF2 file has been made available to let you install MicroPython more easily, with the following steps below:
Steps (in the demo above)
Download the MicroPython UF2 file (provided above).
Find the BOOTSEL button on your Raspberry Pi Pico. Press the BOOTSEL button and hold it while you connect the other end of the micro USB cable to your computer. Release the BOOTSEL button after your Pico is connected.
It will mount as a Mass Storage Device called RPI-RP2.
Drag and drop the previously downloaded MicroPython UF2 file onto the RPI-RP2 volume. Your Pico will reboot. You are now running MicroPython on your Pico.
Follow the simple steps in the images below to select (MicroPython) in your Thonny IDE:
5. Blink Raspberry Pi Pico onboard LED with MicroPython
Now, we will program the LED on the Pi Pico board (connected to pin 25) to turn ON and OFF with MicroPython codes written with Thonny. MicroPython adds hardware-specific modules, such as machines, that you can use to program your Raspberry Pi Pico (for example - accessing the GPIO pins). Firstly, we need to import needed libraries: machine for accessing GPIO pins and sleep to add delay to the code.
Next, we will create a led object where we initialize the onboard LED pin (GP25).
For the blink to take effect continuously, we will introduce a loop:
If we set the value of the LED to 1, it turns on and if we set the value to 0, it will turn off.
Then we pause the operation for 1 second using the "sleep function".
Enter the code below in your Thonny IDE:
from machine import Pin # for accessing GPIO pins
from time import sleep # for adding delay
led = Pin(25, Pin.OUT) # set pin 25 for output signal
while True:
led.value(1) # turn on LED
sleep(1) # delay for one second
led.value(0) # turn off LED
sleep(1) # delay for one second
Run code
Follow the simple steps below to save and run your code:
Demo video
LED beside the USB port
Raspberry Pi Pico onboard LED blinking.
That's it for today. I hope we learned something new as always.
Stay tuned for more Raspberry Pi Pico projects and MicroPython tutorials!
Online summer camp and summer camps in Takoma Park Maryland
You and your children are looking for nice activities to have fun and learn new things and skills. Come join us starting in May and this summer for more online coding courses for kids such as:
Python coding for kids and teens
Coding for elementary school students
Make video games
Electronics
Digital modeling (Create cars, rockets, rings, etc) for 3D printing and much more with the following link: Online summer camp
And the STEM summer camp in Maryland for your kids is organized in Takoma Park Maryland nearby Washington DC to have fun and learn more about robotics, coding, droning, and more. Check the link right here:
Other cute things to make and hand-crafts for kids
If you are looking for more electronics programming and STEM tutorials to do with your kids, take a look at these other activities:
Learn Electronics for Kids: How to control light with an electronics board - arduino
Projects Arduino: How to measure the temperature with an Arduino board and a temperature sensor
Fun things to do indoors: How to make a Cartesian diver with a bottle of water
Electronics projects for kids: How to create an Arduino timer with a seven-segment display
Newsletter, follow, subscribe, and like the social media
If you like online STEM activities, consider subscribing to the newsletter and social media for updates, and don't miss any STEM events. Don't forget to subscribe to the newsletter at the bottom of our website ''www.makersgeneration.net'' for more events, tutorials, and freebies.
Subscribe to the Facebook group if you have yet registered. Content and tutorials are shared daily: Create and build STEM projects for kids
We can be reached at: contact@makersgeneration.net if any questions.
See you on soon.
Comentários