Arduino API for the Raspberry Pico

Overview

Raspberry PI Pico - Arduino API

On Friday I was receiving my Raspberry PI Pico and I had the opportunity to play around with it. Actually most of the time I spent reading the Manuals... The conclusion: interesting - but a little bit too complicated - So I was quite happy to read that Arduino plans to add an official support for this Microcontroller.

However I did not want to wait for the official support to be available, so as a temporary solution, I decided to make my own Arduino API availale as a separate project.

Design goals

  • Provide a full implementation of the Arduino API
  • Provide additioal easy to use C++ classes for the functionaity which is specific to the Pico
  • Keep the pico specific build process using cmake
  • Provide examples

Installation

  1. First you need to install the Pico C Framwork. Please follow the instructions and do not forget to define the PICO_SDK_PATH environment variable.

  2. Clone this project with

git clone https://github.com/pschatzmann/pico-arduino
  1. Define the environment PICO_SDK_ARDUINO_PATH variable to point to the project location.
  2. Build the arduino library (optional). The project already contains the compiled library. However I would recommend to rebuild it from the source.

To build the arduino library go to the pico-arduino/Arduino subdirectory

cd pico-arduino/Arduino

and execute the steps below:

mkdir build
cd build install
cmake ..
make

You can find the resulting library in the pico-arduino/lib directory.

Examples

Let's have a look at the blink example. It conisists of the source code blink.cpp

#include "Arduino.h"

// the setup function runs once when you press reset or power the board
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

This looks like a standard Arduino sketch. Please note the following differences however:

  • The sketch file name must end with .cpp and not .ino
  • The #include "Arduino.h" is mandatory
  • You need to provide a CMakeLists.txt file

To build the example go to the pico-arduino/examples/blink directory and execute the build steps:

mkdir build
cd build
cmake ..
make

The result can be found in the arduino/examples/blink/build directory.

Here is the list of the provided examples

The Pico has 2 processors, so here are the examples which use this Pico specific functionality:

And finally some other Pico specific functionality:

Documentation

The generated Class documentation can be found in the doc folder

Pinout

It helps to know the functions of the pins. Here is a copy of the pinout provided by the official Raspberry Pi Pico Documentation:

Raspberry Pi Pico pin out diagram

If a method is requiring a pin you can provide the GPIO number as integer or use the corresponding GP name as given in the sheet above.

The following calls are all identical:

  digitalWrite(25, HIGH);                      // turn the LED on using integer
  digitalWrite(GP25, HIGH);                    // the same with GP25 define
  digitalWrite(LED_BUILTIN, HIGH);             // the same with Arduino defines 
  digitalWrite(PICO_DEFAULT_LED_PIN, HIGH);    // the same with Pico defines 

Deploying a compiled Sketch

Deployment is very easy - just press the button on your Pico while plugging it to your computer via USB. This will automatically mount the Pico as external drive (RPI-RP2). Finally you just need to copy the generated uf2 file from your build directory to the drive.

That's it!

Project Status

  • Basic Arduino API (e.g. String, Serial...) - done
  • digital input/output API - done
  • analog input/output API (PWM) - done
  • Multiprocessor support: queue, semaphore - done
  • tone - in progress
  • PIO API class - in progress
  • Arduino SPI API - open
  • Arduino I2C API - open
  • I2S API using PIO - open
  • Software Serial using PIO - open
Issues
  • HardwareSerial Baud rate problem

    HardwareSerial Baud rate problem

    I have meet a baud rate problem, even if I set rate to 115200, it wouldn't work on the exact rate, but work in a random rate. Some times I can communicate with esp8266 successfully, but commonly it will failed with ERROR or simply no response(shows like timeout). SoftwareSerial works well. image

    opened by Dracanrage 18
  • typeinfo for arduino::Print error

    typeinfo for arduino::Print error

    [build] [ 12%] Built target ELF2UF2Build
    [build] Scanning dependencies of target pico
    [build] [ 13%] Building CXX object CMakeFiles/pico.dir/pico.cpp.obj
    [build] [ 14%] Linking CXX executable pico.elf
    [build] /Applications/ARM/bin/../lib/gcc/arm-none-eabi/10.2.1/../../../../arm-none-eabi/bin/ld: CMakeFiles/pico.dir/esp8266/WiFiClient.cpp.obj:(.rodata._ZTIN7arduino6StreamE[_ZTIN7arduino6StreamE]+0x8): undefined reference to `typeinfo for arduino::Print'
    [build] collect2: error: ld returned 1 exit status
    [build] make[2]: *** [pico.elf] Error 1
    [build] make[1]: *** [CMakeFiles/pico.dir/all] Error 2
    [build] make: *** [all] Error 2
    

    When I try to use the Arduino Lib uses Print, it shows this error.

    opened by Dracanrage 10
  • Fix getTemperature()

    Fix getTemperature()

    I have tested this func in pico, it returns negative value. I find it forget to change ADC read value to ADC_Voltage, so I made this commit to totally fixed this function, return with double value in centigrade.

    opened by Dracanrage 5
  • How to compile LiquidCrystal_I2C ?

    How to compile LiquidCrystal_I2C ?

    Hello, I just tried to add LiquidCrystal_I2C to my pico-arduino test project. It ended with some symbol definition errors. But maybe I made something wrong. May you try to make the simple LiquidCrystal_I2C project, please?

    opened by omikron88 5
  • temperature read bug

    temperature read bug

    The pico onboard temperature sensor reading is well when the program is flashed in the first time, but when the pico rebooted, it's value turn to be wrong. For example, the temperature is 26°C, when it rebooted, it will be 406°C. Seems weird, I tried many ways to figure out its reason but no result. Is it just a problem only I encountered?

    opened by Dracanrage 4
  • analogWrite() is fail after use digitalWrite()

    analogWrite() is fail after use digitalWrite()

    When I want to use build in LED fade after blink, it was failed to use PWM wave on LED, I have checked the log, seems well, but the LED not perform as usual. I used a timer to let LED blink, after setup() I want LED to fade but not work. If only use blink or fade single, it works well. Here is the log:

    17:08:43.406 -> PicoGPIOFunction::PinMode
    17:08:43.444 -> PicoGPIOFunction::PinMode -> has changed - pin: 2
    17:08:43.444 -> PicoGPIOFunction::usePin 2
    17:08:43.444 -> PinSetupGPIO::setupPin
    17:08:43.623 -> PicoGPIOFunction::usePin 2
    17:08:43.909 -> PicoGPIOFunction::usePin 25
    17:08:44.129 -> PicoHardwareSerial::begin 115200
    17:08:44.129 -> setupDefaultRxTxPins
    17:08:44.129 -> Using UART: 0 
    17:08:44.129 -> txPin is 0
    17:08:44.129 -> rxPin is 1
    17:08:44.129 -> set_config
    17:08:44.129 -> SERIAL_8N1 - UART_PARITY_NONE
    17:08:44.129 -> baud_rate requested: 115200
    17:08:44.129 -> baud_rate effective: 115207
    17:08:44.129 -> uart_is_enabled: true
    17:08:44.129 -> [WiFiEsp] Initializing ESP module
    17:08:44.414 -> PicoGPIOFunction::usePin 25
    17:08:44.920 -> PicoGPIOFunction::usePin 25
    17:08:45.420 -> PicoGPIOFunction::usePin 25
    17:08:45.928 -> PicoGPIOFunction::usePin 25
    17:08:46.438 -> PicoGPIOFunction::usePin 25
    17:08:46.908 -> PicoGPIOFunction::usePin 25
    17:08:47.419 -> PicoGPIOFunction::usePin 25
    17:08:47.567 -> [WiFiEsp] Initilization successful - 1.5.4
    17:08:47.567 -> Attempting to connect to WPA SSID: IoT-WiFi
    17:08:47.928 -> PicoGPIOFunction::usePin 25
    17:08:48.432 -> PicoGPIOFunction::usePin 25
    17:08:48.913 -> PicoGPIOFunction::usePin 25
    17:08:49.410 -> PicoGPIOFunction::usePin 25
    17:08:49.919 -> PicoGPIOFunction::usePin 25
    17:08:50.419 -> PicoGPIOFunction::usePin 25
    17:08:50.933 -> PicoGPIOFunction::usePin 25
    17:08:51.438 -> PicoGPIOFunction::usePin 25
    17:08:51.905 -> PicoGPIOFunction::usePin 25
    17:08:52.412 -> PicoGPIOFunction::usePin 25
    17:08:52.595 -> [WiFiEsp] Connected to  IoT-WiFi
    17:08:52.595 -> You're connected to the network
    17:08:52.595 -> SSID:  IoT-WiFi
    17:08:52.595 -> IP Address: 192.168.100.104
    17:08:52.595 -> Signal strength (RSSI):-65 dBm
    17:08:52.595 -> Attempting MQTT connection...
    17:08:52.595 -> [WiFiEsp] Connecting to mqtt.lmen.com
    17:08:52.740 -> connected
    17:08:52.916 -> PicoGPIOFunction::usePin 25
    17:08:53.428 -> PicoGPIOFunction::usePin 25
    17:08:53.932 -> PicoGPIOFunction::usePin 25
    17:08:54.438 -> PicoGPIOFunction::usePin 25
    17:08:54.912 -> PicoGPIOFunction::usePin 25
    17:08:55.412 -> PicoGPIOFunction::usePin 25
    17:08:55.919 -> PicoGPIOFunction::usePin 25
    17:08:56.427 -> PicoGPIOFunction::usePin 25
    17:08:56.922 -> PicoGPIOFunction::usePin 25
    17:08:57.432 -> PicoGPIOFunction::usePin 25
    17:08:57.796 -> PicoGPIOFunction::usePin 25
    17:08:57.796 -> PicoGPIOFunction::PinMode
    17:08:59.794 -> [WiFiEsp] TIMEOUT: 4
    17:08:59.794 -> adc_init
    17:08:59.794 -> adc_select_input 4
    17:08:59.794 -> Temp of Pico: 27°C
    17:08:59.831 -> PicoPWMWriter::begin 25
    17:08:59.831 -> PicoPWMWriter::setupConfig
    17:08:59.831 -> Period ns: 2040816
    17:08:59.831 -> Tick period ns: 2040.000000
    17:08:59.831 -> PWM hz: 490.000092
    17:08:59.831 -> Systemclock hz: 0.000000
    17:08:59.866 -> Tick divider: 254.999985
    17:08:59.866 -> PWM wrap: 1000
    17:08:59.866 -> PicoGPIOFunction::usePin 25
    17:08:59.866 -> PicoPWMWriter::setDutyCycle
    17:08:59.866 -> PWM duty cycle ns: 0
    17:08:59.866 -> PWM duty cycle(internal): 0
    17:08:59.866 -> PicoGPIOFunction::usePin 28
    17:08:59.866 -> PinSetupGPIO::setupPin
    17:08:59.866 -> PicoGPIOFunction::usePin 3
    17:08:59.866 -> PinSetupGPIO::setupPin
    17:08:59.866 -> Desktop has people: 0
    17:08:59.866 -> PicoPWMWriter::begin 25
    17:08:59.866 -> PicoGPIOFunction::usePin 25
    17:08:59.866 -> PicoPWMWriter::setDutyCycle
    17:08:59.866 -> PWM duty cycle ns: 40016
    17:08:59.866 -> PWM duty cycle(internal): 19
    17:08:59.866 -> PicoGPIOFunction::usePin 28
    17:08:59.900 -> PicoPWMWriter::begin 25
    17:08:59.900 -> PicoGPIOFunction::usePin 25
    17:08:59.900 -> PicoPWMWriter::setDutyCycle
    17:08:59.900 -> PWM duty cycle ns: 80032
    17:08:59.900 -> PWM duty cycle(internal): 39
    17:08:59.900 -> PicoGPIOFunction::usePin 28
    17:08:59.934 -> PicoPWMWriter::begin 25
    17:08:59.934 -> PicoGPIOFunction::usePin 25
    17:08:59.934 -> PicoPWMWriter::setDutyCycle
    17:08:59.934 -> PWM duty cycle ns: 120048
    17:08:59.934 -> PWM duty cycle(internal): 58
    17:08:59.934 -> PicoGPIOFunction::usePin 28
    17:08:59.934 -> PicoPWMWriter::begin 25
    17:08:59.934 -> PicoGPIOFunction::usePin 25
    17:08:59.970 -> PicoPWMWriter::setDutyCycle
    17:08:59.970 -> PWM duty cycle ns: 160064
    17:08:59.970 -> PWM duty cycle(internal): 78
    17:08:59.970 -> PicoGPIOFunction::usePin 28
    17:08:59.970 -> PicoPWMWriter::begin 25
    17:08:59.970 -> PicoGPIOFunction::usePin 25
    17:08:59.970 -> PicoPWMWriter::setDutyCycle
    17:08:59.970 -> PWM duty cycle ns: 200080
    17:08:59.970 -> PWM duty cycle(internal): 98
    17:08:59.970 -> PicoGPIOFunction::usePin 28
    17:09:00.008 -> PicoPWMWriter::begin 25
    17:09:00.008 -> PicoGPIOFunction::usePin 25
    17:09:00.008 -> PicoPWMWriter::setDutyCycle
    17:09:00.008 -> PWM duty cycle ns: 240096
    17:09:00.008 -> PWM duty cycle(internal): 117
    17:09:00.008 -> PicoGPIOFunction::usePin 28
    17:09:00.044 -> PicoPWMWriter::begin 25
    17:09:00.044 -> PicoGPIOFunction::usePin 25
    17:09:00.044 -> PicoPWMWriter::setDutyCycle
    17:09:00.044 -> PWM duty cycle ns: 280112
    17:09:00.044 -> PWM duty cycle(internal): 137
    17:09:00.044 -> PicoGPIOFunction::usePin 28
    17:09:00.080 -> PicoPWMWriter::begin 25
    17:09:00.080 -> PicoGPIOFunction::usePin 25
    17:09:00.080 -> PicoPWMWriter::setDutyCycle
    17:09:00.080 -> PWM duty cycle ns: 320128
    17:09:00.080 -> PWM duty cycle(internal): 156
    17:09:00.080 -> PicoGPIOFunction::usePin 28
    17:09:00.114 -> PicoPWMWriter::begin 25
    17:09:00.114 -> PicoGPIOFunction::usePin 25
    17:09:00.114 -> PicoPWMWriter::setDutyCycle
    17:09:00.114 -> PWM duty cycle ns: 360144
    17:09:00.114 -> PWM duty cycle(internal): 176
    17:09:00.114 -> PicoGPIOFunction::usePin 28
    17:09:00.114 -> PicoPWMWriter::begin 25
    17:09:00.114 -> PicoGPIOFunction::usePin 25
    17:09:00.148 -> PicoPWMWriter::setDutyCycle
    17:09:00.148 -> PWM duty cycle ns: 400160
    17:09:00.148 -> PWM duty cycle(internal): 196
    17:09:00.148 -> PicoGPIOFunction::usePin 28
    17:09:00.148 -> PicoPWMWriter::begin 25
    17:09:00.148 -> PicoGPIOFunction::usePin 25
    17:09:00.148 -> PicoPWMWriter::setDutyCycle
    17:09:00.148 -> PWM duty cycle ns: 440176
    17:09:00.148 -> PWM duty cycle(internal): 215
    17:09:00.148 -> PicoGPIOFunction::usePin 28
    
    opened by Dracanrage 2
  • Definition of default pins

    Definition of default pins

    Hello, It work fine now. Thank you. Is it possible to define the default uart, spi and i2c pins in project's CMakeLists.txt or in some top-level header file?

    opened by omikron88 2
  • make: *** No targets specified and no makefile found.  Stop.

    make: *** No targets specified and no makefile found. Stop.

    After editing the sdfat lines in CMakeList.txt (see other Issue) Make does not work and gives

    make: *** No targets specified and no makefile found. Stop.

    opened by spinalcode 1
  • Make failing, SdFat CMakeLists.txt missing

    Make failing, SdFat CMakeLists.txt missing

    Hi,

    The cmake seems to fail:

    CMake Error at CMakeLists.txt:60 (add_subdirectory):
      The source directory
    
        /Volumes/Samsung_T5/Development/pico/pico-arduino/Arduino/SdFat
    
      does not contain a CMakeLists.txt file.
    
    
    CMake Error at CMakeLists.txt:68 (install):
      install TARGETS given target "sdfat" which does not exist.
    

    Cheers

    Andy

    opened by AndrewCapon 1
Releases(v0.8)
  • v0.8(Apr 2, 2021)

    • We can use the project as external library and just pull it in e.g. with the help of FetchContent
    • Example how to use this project as (external) library
    • Tensorflow example
    Source code(tar.gz)
    Source code(zip)
  • v0.7(Mar 29, 2021)

    Implemented Reading of PWM Support for PWM in analogRead() Added documentation on how to override the predefined framework #defines Redesign of internal PicoPinFunction: getting rid of ugly switch case. Examples for reading PWM Example for watchdog DMA support with examples Implementation of namespace using pico_arudino Drone example

    Source code(tar.gz)
    Source code(zip)
  • v0.6(Mar 22, 2021)

    PWM, error corrections and additional examples:

     - Provide easy to use PicoPWM class which is used by Arduino analogWrite
     - Implement Servo class with examples
     - Removed libarduino: all sources are complied as part of the sketch, so that we can overwrite default defines 
     - Example how to change default pins in cmake 
    
    Source code(tar.gz)
    Source code(zip)
  • v0.5(Mar 19, 2021)

     - HardwareSerial: Corrected slow print and println
     - analogWrite correction provided by Duke Dylan
     - Moved examples documentation into examples folder
     - Examples for HC-05 and ESP01
     - This release contains the following __Breaking Changes__
        - Changed signature of SoftwareSerial.begin() by switching the tx and rx sequence to be consistent with HardwareSerial
        - Removed unnecessary subfolders in ArduinoCore-Pico 
    
    Source code(tar.gz)
    Source code(zip)
  • v0.4(Mar 15, 2021)

  • v0.3(Mar 15, 2021)

  • v0.2(Mar 14, 2021)

Owner
Phil Schatzmann
Phil Schatzmann
Breakout game for Raspberry Pi Pico with Pimoroni Pico Display pack

breakout_rpi_pico Breakout game for Raspberry Pi Pico with Pimoroni Pico Display pack Prebuilt binary (breakout.uf2) is here. To build your own binary

null 18 Jan 17, 2022
Pico-uart-bridge - Raspberry Pi Pico UART-USB bridge

Raspberry Pi Pico USB-UART Bridge This program bridges the Raspberry Pi Pico HW UARTs to two independent USB CDC serial devices in order to behave lik

Álvaro Fernández Rojas 104 Jun 21, 2022
Digital rain animation gif with glow squeezed into a raspberry pi pico and pimoroni pico-display

pico-display-matrix Digital rain animation gif with glow squeezed into a raspberry pi pico and pimoroni pico-display or how to actually use all Flash

null 28 Apr 3, 2022
Prueba del Raspberry PI PICO con un display Raspberry PI TFT 3.5"

Raspberry-PI-PICO-display-RPI35 Prueba del Raspberry PI PICO con un display Raspberry PI TFT 3.5" Con ayuda de la libreria https://github.com/khoih-pr

null 1 Nov 10, 2021
Raspberry Pi Pico Arduino core, for all RP2040 boards

Arduino-Pico Raspberry Pi Pico Arduino core, for all RP2040 boards This is a port of the RP2040 (Raspberry Pi Pico processor) to the Arduino ecosystem

Earle F. Philhower, III 553 Jun 22, 2022
📚🪛 Arduino library to calibrate and improve ADC measurements with the Raspberry Pi Pico.

Arduino-Pico-Analog-Correction Arduino library to calibrate and improve ADC measurements with the Raspberry Pi Pico. Can compensate ADC offsets, calcu

NuclearPhoenix 8 Jun 20, 2022
Raspberry Pi Pico (RP2040) and Micro-ROS (ROS 2) Integration

The Pico is an amazing microcontroller and I couldn't wait for ROS 2 support or Arduino Core, so here is my approach. Once the Arduino Core for RP2040 is out it will be easier to use micro_ros_arduino.

Darko Lukić 19 Jun 19, 2022
built-in CMSIS-DAP debugger tailored especially for the RP2040 “Raspberry Pi Pico”

RP2040 has two ARM Cortex-M0+ cores, and the second core normally remains dormant. pico-debug runs on one core in a RP2040 and provides a USB CMSIS-DAP interface to debug the other core. No hardware is added; it is as if there were a virtual debug pod built-in.

null 189 Jun 25, 2022
🦠 µnix is a UNIX-like operating system for the raspberry pi pico.

The µnix Operating System "µnix", "munix" or, "micro unix" aims to be a micro kernel based operating system targeting the Raspberry Pi Pico. "µnix" is

Sleepy Monax 57 Jun 19, 2022
Fractal rendering for Raspberry Pi Pico microcontroller

picofract Mandelbrot Set rendering demo for Raspberry Pi Pico microcontroller with Pico Display Pack. Building If you already have the Pimoroni SDK bu

null 20 Apr 29, 2022
x86 emulator on Raspberry Pi Pico

picox86 x86 emulator on Raspberry Pi Pico https://user-images.githubusercontent.com/10139098/110543817-13299080-812b-11eb-9c88-674cdae919fc.mp4 PCB fr

null 34 Apr 7, 2022
A laser cut Dreamcast Pop'n Music controller and integrated memory card using the Raspberry Pi Pico's Programmable IO

Dreamcast Pop'n Music Controller Using Raspbery Pi Pico (RP2040) Intro This is a homebrew controller for playing the Pop'n Music games on the Sega Dre

null 31 Jun 6, 2022
Web Server based on the Raspberry Pico using an ESP8266 with AT firmware for WiFi

PicoWebServer This program runs on a Raspberry Pico RP2040 to provide a web server when connected to an Espressif ESP8266. This allows the Pico to be

null 44 Jun 10, 2022
A programming environment for Lua for the Raspberry Pi Pico microcontroller

picolua A programming environment for Lua for the Raspberry Pi Pico microcontroller. Version 0.3, April 2021 What is this? picolua is a proof-of-conce

Kevin Boone 47 May 23, 2022
a little hobby raspberry pi pico emulator

PICO-EMU a little raspberry pi pico emulator note: we use the provided bootrom from raspberry pi at the repo: https://github.com/raspberrypi/pico-boot

supercyp 21 Jun 5, 2022
Raspberry Pi Pico AutoHotkey Streamdeck / Keyboard with LEDs

Raspberry Pi Pico AutoHotkey Streamdeck / Keyboard with LEDs

Build Comics 17 Feb 10, 2022
A FAT filesystem with SPI driver for SD card on Raspberry Pi Pico

no-OS-FatFS-SD-SPI-RPi-Pico Simple library for SD Cards on the Pico At the heart of this library is ChaN's FatFs - Generic FAT Filesystem Module. It a

Carl J Kugler III 88 Jun 21, 2022
Enable LoRaWAN communications on your Raspberry Pi Pico or any RP2040 based board. 📡

pico-lorawan Enable LoRaWAN communications on your Raspberry Pi Pico or any RP2040 based board using a Semtech SX1276 radio module. Based on the Semte

Sandeep Mistry 62 Jun 11, 2022
PACMAN for Raspberry Pi Pico (QVGA LCD)

PACMAN for Raspberry Pi Pico ラズベリーPi PicoにQVGAの液晶を接続して動作する本格的なパックマンです。 小型スピーカーを接続して音声も鳴らせます。 とてもシンプルな回路なので、簡単に製作することができます。 液晶は240x320ドット、コントローラにILI934

null 32 Jun 8, 2022