pico-micropython-examples
Examples to accompany the "Raspberry Pi Pico Python SDK" book.
Top Related Projects
CircuitPython - a Python implementation for teaching coding with microcontrollers
Python IDE for beginners
Quick Overview
The raspberrypi/pico-micropython-examples repository is a collection of MicroPython code examples specifically designed for the Raspberry Pi Pico microcontroller. It provides a wide range of sample scripts demonstrating various features and capabilities of the Pico board, helping developers get started with MicroPython programming on this platform.
Pros
- Comprehensive collection of examples covering many Pico features
- Well-organized repository structure for easy navigation
- Official examples from Raspberry Pi, ensuring reliability and accuracy
- Regularly updated to include new features and improvements
Cons
- Limited documentation within individual example files
- Some examples may be too basic for advanced users
- Lack of more complex, real-world project examples
- No standardized error handling or best practices demonstrated
Code Examples
- Blinking an LED:
import machine
import utime
led = machine.Pin(25, machine.Pin.OUT)
while True:
led.toggle()
utime.sleep(0.5)
This example blinks the onboard LED of the Raspberry Pi Pico every half second.
- Reading an analog sensor:
from machine import ADC, Pin
import utime
sensor = ADC(Pin(26))
while True:
reading = sensor.read_u16()
print("Sensor reading:", reading)
utime.sleep(1)
This code reads values from an analog sensor connected to GPIO pin 26 and prints the results.
- Using I2C communication:
from machine import Pin, I2C
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=100000)
devices = i2c.scan()
if devices:
for device in devices:
print(f"I2C device found at address: 0x{device:02X}")
else:
print("No I2C devices found")
This example scans for I2C devices connected to the Pico and prints their addresses.
Getting Started
- Download and install the Raspberry Pi Pico MicroPython firmware.
- Connect your Pico to your computer via USB.
- Copy the desired example script to the Pico.
- Open a serial terminal to interact with the Pico.
- Run the script using the MicroPython REPL:
>>> import example_script
Replace example_script
with the name of the script you want to run.
Competitor Comparisons
CircuitPython - a Python implementation for teaching coding with microcontrollers
Pros of CircuitPython
- Larger ecosystem with extensive libraries and community support
- Easier for beginners with simpler syntax and built-in REPL
- Better compatibility across different microcontroller boards
Cons of CircuitPython
- Slightly slower execution compared to MicroPython
- Less memory-efficient, which can be a limitation on some boards
- Fewer low-level hardware access options
Code Comparison
CircuitPython:
import board
import digitalio
import time
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
while True:
led.value = True
time.sleep(0.5)
led.value = False
time.sleep(0.5)
Pico MicroPython:
from machine import Pin
import time
led = Pin(25, Pin.OUT)
while True:
led.toggle()
time.sleep(0.5)
The CircuitPython example uses higher-level abstractions, making it more beginner-friendly but potentially less efficient. The Pico MicroPython example is more concise and offers direct hardware access, which can be beneficial for advanced users and performance-critical applications.
Python IDE for beginners
Pros of Thonny
- Comprehensive IDE with built-in debugger and shell
- Cross-platform support (Windows, macOS, Linux)
- User-friendly interface suitable for beginners
Cons of Thonny
- Larger resource footprint compared to simple example repositories
- May include features unnecessary for basic Raspberry Pi Pico development
- Steeper learning curve for users only interested in Pico-specific examples
Code Comparison
Thonny (Python script execution):
from thonny import get_runner
def run_script(script_path):
runner = get_runner()
runner.execute_script(script_path)
run_script("my_pico_script.py")
pico-micropython-examples (LED blink example):
import machine
import utime
led = machine.Pin(25, machine.Pin.OUT)
while True:
led.toggle()
utime.sleep(0.5)
Thonny provides a more comprehensive development environment, while pico-micropython-examples offers simple, focused examples for Raspberry Pi Pico. Thonny is better suited for larger projects and beginners, while pico-micropython-examples is ideal for quick reference and specific Pico-related tasks.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
MicroPython Examples
Examples to accompany the "Raspberry Pi Pico Python SDK" book published by Raspberry Pi Ltd, which forms part of the technical documentation in support of Raspberry Pi Pico and the MicroPython port to RP2040.
Note: There are also additional examples for the RP2040 port of MicroPython here in the upstream MicroPython repo.
Contributions
While we welcome pull requests to contribute further example code, please do not link to personal sites or to your social media. Contributions which are fully documented with an AsciiDoc description and a Fritzing wiring diagram, e.g. see the NeoPixel Ring example, stand more chance of inclusion.
Our example code is under the BSD-3-Clause License: any contributions must be under the same license.
Top Related Projects
CircuitPython - a Python implementation for teaching coding with microcontrollers
Python IDE for beginners
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot