How to Use Raspberry Pi Pico: Getting Started Guide
To use
Raspberry Pi Pico, connect it to your computer via USB and program it using MicroPython or C/C++. Start by installing the Thonny IDE for MicroPython, write your code, and upload it to the Pico to control pins and sensors.Syntax
Raspberry Pi Pico programming with MicroPython uses simple commands to control pins and hardware. The main parts are:
- import machine: Access hardware functions like pins.
- Pin(pin_number, mode): Set a pin as input or output.
- pin.value(1 or 0): Turn pin on or off.
- time.sleep(seconds): Pause the program for a set time.
python
import machine import time led = machine.Pin(25, machine.Pin.OUT) # Onboard LED while True: led.value(1) # Turn LED on time.sleep(1) # Wait 1 second led.value(0) # Turn LED off time.sleep(1) # Wait 1 second
Example
This example blinks the onboard LED on the Raspberry Pi Pico every second. It shows how to set up a pin and use a loop to turn it on and off.
python
import machine import time led = machine.Pin(25, machine.Pin.OUT) # Onboard LED for _ in range(5): # Blink 5 times led.value(1) # LED on print("LED ON") time.sleep(1) led.value(0) # LED off print("LED OFF") time.sleep(1)
Output
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
LED ON
LED OFF
Common Pitfalls
Common mistakes when using Raspberry Pi Pico include:
- Not installing MicroPython firmware on the Pico before programming.
- Using the wrong pin numbers (Pico pins differ from Raspberry Pi pins).
- Forgetting to set pin mode (input/output) before using it.
- Not saving the script as
main.pyto run automatically on boot.
python
import machine # Wrong: Using pin number 0 without setting mode pin = machine.Pin(0) pin.value(1) # This will cause an error # Correct: pin = machine.Pin(0, machine.Pin.OUT) pin.value(1) # This works fine
Quick Reference
| Command | Description |
|---|---|
| import machine | Access hardware features |
| machine.Pin(pin, mode) | Set pin number and mode (IN/OUT) |
| pin.value(1 or 0) | Turn pin on (1) or off (0) |
| time.sleep(seconds) | Pause program for given seconds |
| main.py | Filename to run code automatically on boot |
Key Takeaways
Install MicroPython firmware and use Thonny IDE to program Raspberry Pi Pico.
Use machine.Pin to control GPIO pins by setting mode and value.
Save your script as main.py to run it automatically when Pico starts.
Check pin numbers carefully; Pico pins differ from other Raspberry Pi models.
Test simple examples like blinking the onboard LED to confirm setup.