Skip to content

SimonWaldherr/rp2040-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RP2040 Examples

DOI

test the new Blockly Micropython Editor!

Watch the video

Welcome to the RP2040 Examples repository! This collection showcases various projects and code samples for the Raspberry Pi Pico and other RP2040-based microcontroller boards. Whether you're a beginner looking to get started with MicroPython or an experienced developer exploring Golang on embedded systems, you'll find something useful here.

If you're also interested in Raspberry Pi projects using Golang, be sure to check out my Raspberry Pi Golang examples.


Table of Contents


Where to Buy

The Raspberry Pi Pico is a powerful yet affordable microcontroller that you can purchase from various retailers worldwide. Here are some trusted sources:


Documentation & Resources

To get the most out of your Raspberry Pi Pico, you can refer to the following documentation and resources:


MicroPython

MicroPython is an efficient and beginner-friendly way to program your Raspberry Pi Pico. Follow the links below to download the latest firmware:

Getting Started

  1. Install MicroPython firmware: Download and flash the .uf2 file to your Pico
  2. Choose an IDE: We recommend Thonny for beginners
  3. Connect your Pico: Use a USB cable to connect to your computer
  4. Run examples: Copy and paste code directly into the REPL or save as .py files

MicroPython Examples

Explore these example scripts to get hands-on with MicroPython:


Advanced Examples

The repository includes advanced examples that demonstrate more complex integrations and use cases:

Sensors & Environmental Monitoring

  • DHT22/BME280: Temperature, humidity, and pressure monitoring
  • Real-time data logging: Store sensor readings to EEPROM memory
  • ADC applications: Multi-channel analog sensor reading

Display & Output

  • OLED displays: SSD1306 driver with text and graphics
  • LED matrices: MAX7219 8x8 matrix control with patterns
  • Advanced LED effects: WS2812 animations and Hub75 visualizations

Motor Control & Robotics

  • DC motor control: PWM speed control with direction
  • Servo positioning: Precise servo motor control
  • Stepper motors: Step-by-step positioning control

Communication Protocols

  • I2C device scanning: Automatic detection of connected devices
  • UART communication: Serial data transmission and GPS simulation
  • SPI interfaces: High-speed peripheral communication

Timing & Memory

  • Real-time clocks: DS3231 RTC for timekeeping
  • External memory: EEPROM storage for persistent data
  • Interrupt handling: Efficient button and sensor response

Project Categories

This repository covers a wide range of project categories to help you learn and build:

Category Examples Skill Level
Basic I/O LED blinking, button input, ADC reading Beginner
Sensors Distance (HC-SR04), weight (HX711), environmental (DHT22, BME280) Beginner-Intermediate
Communication I2C, SPI, UART, RFID (RC522) Intermediate
Displays OLED (SSD1306), LED matrices (MAX7219, Hub75) Intermediate
Motor Control DC motors, servos, steppers Intermediate
Networking WiFi connectivity, web data fetching Intermediate
Advanced Game simulations, real-time systems, data logging Advanced

Golang

For those who prefer to code in Golang, this section provides examples of using Golang on the Raspberry Pi Pico with the TinyGo compiler.

Getting Started with TinyGo

  1. Install TinyGo: Follow the installation guide
  2. Verify installation: Run tinygo version in your terminal
  3. Connect your Pico: Hold BOOTSEL button while connecting USB
  4. Build and flash: Use tinygo flash -target=pico your_program.go

TinyGo Commands

# Build only (creates .uf2 file)
tinygo build -o program.uf2 -target=pico program.go

# Build and flash directly
tinygo flash -target=pico program.go

# Monitor serial output
tinygo flash -target=pico -monitor program.go

Golang Examples

  • blink.go
    Blink an LED using Golang. Install TinyGo and compile with:

    tinygo build -o blink.uf2 -target=pico blink.go

    Flash directly to the Pico with:

    tinygo flash -target=pico blink.go

    Add -monitor to the command to see the program’s output in the terminal.

  • blink_with_ws2812.go
    Control WS2812 LEDs using Golang, creating vibrant lighting effects.

  • blink_with_ws2812_struct.go
    An advanced example for controlling WS2812 LEDs using structured Golang code.

  • read_rfid_with_rc522.go
    Read RFID cards and tags using the RC522 module and Golang.

  • button_control.go
    Simple button input handling with debouncing using TinyGo.

  • adc_reading.go
    Read analog values from multiple ADC channels and convert to voltage.

  • pwm_patterns.go
    Generate various PWM patterns including sawtooth, triangle, and square waves.

  • i2c_scanner.go
    Scan I2C bus for connected devices and attempt basic communication.


Troubleshooting

Common Issues

MicroPython:

  • "No module named 'machine'": Make sure MicroPython firmware is installed correctly
  • Connection issues: Check USB cable and ensure Pico is recognized as a serial device
  • Import errors: Verify all required files are uploaded to the Pico

TinyGo:

  • "tinygo command not found": Ensure TinyGo is properly installed and in your PATH
  • Flash errors: Make sure Pico is in BOOTSEL mode (hold button while connecting)
  • Build errors: Check TinyGo version compatibility with target features

Hardware:

  • No response from sensors: Verify wiring and power connections
  • I2C issues: Check pull-up resistors (often 4.7kΩ) on SDA and SCL lines
  • PWM not working: Ensure pins support PWM functionality

Getting Help


Contributing

Contributions are welcome! If you have a project or code sample you'd like to share, feel free to submit a pull request.


License

This repository is licensed under the MIT License. For more details, see the LICENSE file.


Feel free to explore the examples and contribute to this growing repository! Whether you're building a simple LED blinker or a complex sensor network, these examples are designed to help you get the most out of your Raspberry Pi Pico.