PlatformIO Community

Do I understand this correctly: program the rPi Pico in C with Arduino libraries?

The Pico has 32k RAM and 2MB flash.
I created a program by copying existing an Arduino code, which barely fitted into an UNO, into a main.cpp for the Pico. The project folder was created withe the Pico board selected.

For a while now, libraries need to be added to each project. So I added the PubSubClient to the project.
But where do I find the avr/wdt.h file?
And more so, how do I add it to the Pico project.
This step was not necessary under the Arduino framework/project.

The main question is though: can I program the Pico by using the Arduino libraries?

I thought I add some code:

#include <Arduino.h>                            // basic Arduino definitions
#include <avr/wdt.h>                            // watch dog


What library that you use requires the AVR watchdog functions? Because I don’t see it in the pubsubclient.

The Pico, based on the RP2040 chip, does have a watchdog (see datasheet chapter 4.7), but I would not expect an Arduino core to expose that under a faked AVR wdt.h header if the functionality can’t be mapped 1:1. The Arduino-Pico core has a “AVR compatibility layer” at arduino-pico/cores/rp2040/api/deprecated-avr-comp/avr at master · earlephilhower/arduino-pico · GitHub, but even it does not fake wdt.h.

If you want to use the watchdog, in ArduinoCore-mbed, use Watchdog.h, in Arduino-Pico, use the SDK directly with hardware/watchdog.h.

Thanks. I did some further digging and found the watchdog.h, which could simply be called in the program.

I think the bigger question is: cane the Pico run unaltered Arduino code, including the use of libraries written for the Arduino?
This is how I understood the Pico; program it in MicroPython or program it in the Arduino IDE; or PlatformIO for that matter.

So far, the watchdog seems to require the Pico watchdog.

Source-code compatibility can only be expected if the libraries only use the common Arduino API – if a library decides to include AVR specific header and call functions in it, or even worse, do direct register manipulations, that fails to compile. Arduino cores try to partially remedy this because they know libraries just love to e.g. #include <pgmspace.h> for PROGMEM etc, which is an AVR compiler specific thing, by providing headers that hack the macros and functions in such a way that it compiles to sensible code (see e.g. pgmspace.h from Arduino-Pico, same header in STM32Duino). If it goes beyond that (like avr/interrupt.h, avr/watchdog.h), the cores usually don’t handle that and source-code compatibility is not given.


I see… and see how little do I know… ten years Arduino only…

At least I managed to get my program to compile :slight_smile:


Now I need to test, whether it actually works.