Guide no. 79
From installing MicroPython to your first LED, a button, a fading light and the chip temperature, with Thonny.
By Ada Intelisano · informaticait.it technical team
- To install MicroPython, hold down BOOTSEL and drag a .uf2 file onto the board.
- Thonny sends programs to the board and shows what they print.
- LED on GP15 with 220 Ω, button on GP16 with the internal resistor.
- The pins work at 3.3 V; the onboard LED is called “LED”.
The Raspberry Pi Pico 2 costs a few euros and you program it in Python, the language many people learn at school. Nothing to compile: write a line, press a key and the board runs it right away. In an hour you will blink an LED, read a button, dim a light and measure the chip temperature.
1The parts
- a Raspberry Pi Pico 2 with the pins already soldered (a Pico H works too) and a micro USB cable that also carries data;
- a breadboard, a red LED, a 220 Ω resistor, a breadboard pushbutton and three jumper wires;
- a computer with Thonny, a free program for writing Python.
2Meet the board
Forty pins, numbered 1 to 40: 1 to 20 down the left side from the top, 21 to 40 up the right side from the bottom. The GPIOs are called GP0, GP1… and work at 3.3 V. Next to the USB connector you'll find the onboard LED and the BOOTSEL button, which you'll need in a moment.
3Install MicroPython
- Download the MicroPython .uf2 file for your model from micropython.org (Pico 2: “RPI_PICO2”; Pico: “rp2-pico”).
- Hold down BOOTSEL and plug the USB cable into the computer, then let go of the button.
- A drive shows up, like a USB stick, called RP2350 (on the Pico it's RPI-RP2). Drag the file onto it: the board restarts by itself.
- In Thonny, at the bottom right, choose the interpreter “MicroPython (Raspberry Pi Pico)”.
4Your first program
Type these lines in Thonny and press the green triangle (Run).
# The first program: the LED on the board turns on and off every half second. from machine import Pin import time led = Pin("LED", Pin.OUT) # "LED" works on the Pico 2 and on the W models while True: led.toggle() # if it was on it turns off, and the other way around time.sleep(0.5)
5LED and button on the breadboard
- The Pico straddles the center channel, with the USB on the right.
- LED: from pin 20 (GP15) through the 220 Ω resistor to the long leg of the LED; the short leg goes to the − rail.
- Button: one leg to pin 21 (GP16), the other to the − rail. The two − rails go to the Pico GND pins (pins 18 and 23).
# The button turns on the red LED: when pressed, pin GP16 goes to GND and reads 0. from machine import Pin import time led = Pin(15, Pin.OUT) # GP15, physical pin 20 # GP16, physical pin 21, with the internal resistor to 3.3 V pulsante = Pin(16, Pin.IN, Pin.PULL_UP) while True: if pulsante.value() == 0: led.on() else: led.off() time.sleep_ms(10)
6The breathing light
PWM turns the pin on and off a thousand times a second. By changing how long it stays on, you make the LED look brighter or dimmer. In MicroPython that “how long” goes from 0 to 65535.
# The LED slowly fades on and off: # PWM changes how much of the time the pin stays on. from machine import Pin, PWM import time led = PWM(Pin(15)) led.freq(1000) # 1000 times a second: the eye only sees the average while True: for livello in range(0, 65536, 512): # from off (0) to fully on (65535) led.duty_u16(livello) time.sleep_ms(8) for livello in range(65535, -1, -512): led.duty_u16(livello) time.sleep_ms(8)
7The hidden thermometer
Inside the chip there is a temperature sensor, connected to channel 4 of the analog converter. The formula comes from the datasheet. It measures the chip, not the room, but touch it with a finger and you'll see the number go up.
# The thermometer inside the chip: converter channel 4 and the datasheet formula. from machine import ADC import time sensore = ADC(4) fattore = 3.3 / 65535 # from number (0-65535) to volts while True: volt = sensore.read_u16() * fattore gradi = 27 - (volt - 0.706) / 0.001721 print("Chip temperature: %.1f °C" % gradi) time.sleep(1)
- Thonny doesn't see the board: the cable is charge-only, or the interpreter isn't selected.
- The external LED stays off: it's in backwards, or the resistor isn't in the same column.
- No RP2350 drive: you let go of BOOTSEL too early, try again.
8Challenge yourself
- Make the red LED blink three times every time you press the button.
- Turn on the external LED when the chip temperature goes above 30 °C.
- Build the traffic light from guide no. 39 again, with three LEDs on the Pico.
- you install MicroPython by dragging a .uf2 file;
- pins, PWM and the ADC take just a few lines of Python;
- main.py starts by itself at power-up;
- the Pico pins work at 3.3 V.
Useful products
Other guides
- Resistor colour code and tolerances
- N-channel and P-channel MOSFETs
- Bipolar transistor (BJT) or MOSFET?
- Relays: how to drive them and how much current they draw
- THT and SMD/SMT components
- XIAO board pinouts
- LEDs: forward voltage and series resistor
- The multimeter: measuring voltage and current
- Wemos D1 mini: first steps
- MOSFET module with Arduino and ESP32
- Relay module with Arduino and ESP32
- 3D printing filaments: PLA, PETG and fibre-filled versions
- Analog and digital: the differences
- ADS1115: 16-bit converter with Arduino and ESP32
- From bits to real values: converting sensor readings
- DS3231 RTC clock with Arduino and ESP32
- Linear or switching regulator: which one to choose
- Step-down, step-up and buck-boost: choosing the right module
- Voltage divider: reading voltages higher than 5 V or 3.3 V
- Pull-up, pull-down and bounce: wiring a button properly
- 3.3 V and 5 V logic levels: getting an ESP32 and an Arduino to talk
- The I²C bus: addresses, pull-ups and the scanner
- Lithium batteries: voltages, charging and protection
- ESP32 power consumption and deep sleep: how long the battery lasts
- Temperature sensors compared
- ESP32: which pins to use and which to avoid
- Getting started with the ESP32 in Arduino IDE 2 and PlatformIO
- SSD1306 OLED display and 16×2 I²C LCD with Arduino and ESP32
- PIR motion sensor and HC-SR04 ultrasonic sensor
- Date and time from the Internet with the ESP32: NTP, Italian time zone and daylight saving time
- MicroSD data logger: saving measurements to CSV
- Solar-powered ESP32: panel, charger and the numbers for Italy
- Web server on the ESP32: control a relay from your phone
- Your first LED: wire it up and make it blink
- The button: let Arduino decide
- The potentiometer: turn a knob and read its value
- Soft light: adjusting the brightness of an LED
- The buzzer: playing notes and melodies
- The traffic light: three LEDs in sequence
- The thermometer: reading the temperature with the TMP36
- The servo motor: moving an arm with Arduino
- The night light: the photoresistor and your first decision
- The relay: switching on a light bulb with Arduino
- The LCD display: making Arduino write
- The electronic dice: your first complete project
- The RGB LED: every color from three lights
- The joystick: two-axis control with a shield
- The LED ring: twelve colors from a single pin
- The DC motor: driving it with a MOSFET
- Soil moisture: the plant that asks for water
- The stepper motor: turn exactly as far as you want
- The parking sensor: faster and faster beeps
- The 7-segment display: big, bright numbers
- Automatic watering: the plant that waters itself
- The keypad: a lock with a secret code
- Networked weather station: values and chart on your phone
- A Telegram bot on the ESP32: your home from your phone
- MQTT with the ESP32: the language of connected devices
- Updating the ESP32 over Wi-Fi (OTA)
- ESP-NOW: two ESP32 boards talking without a router
- WebSocket with the ESP32: the LED that follows the slider
- Readings on a Google sheet, with deep sleep
- WiFiManager: choose the Wi-Fi from your phone
- Bluetooth LE with the ESP32: the phone without a network
- The camera in your browser with the ESP32-S3
- A portable Bluetooth speaker you build yourself
- Bluetooth desktop speakers, 2 × 20 W
- Stereo MP3 player with two buttons
- The shelf that plays music: a Visaton exciter
- How to solder well
- How much does it draw? Current and power with the INA219
- Load-cell scale with the HX711
- CO2 traffic light: when to open the windows
- RFID card door opener
- GPS: track logger on microSD
- LoRa: how far does it go? A range test with two boards
- Battery weather sign with an e-paper display
- Web radio with an ESP32 and an I2S amplifier
- Your first amplifier: the LM386 and a speaker
- Talking Arduino: the DFR0760 speech synthesis module
- The spectrum of your music on an LED matrix
- Drawing with 64 LEDs: the 8×8 matrix with the MAX7219
- A real voltage: the 12-bit MCP4725 DAC
- The heartbeat on Arduino: an ECG with the AD8232