element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Program RPi Pico using Mbed library with Arduino IDE
  • Blog
  • Forum
  • Documents
  • Events
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Raspberry Pi requires membership for participation - click to join
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: ralphjy
  • Date Created: 19 Apr 2021 3:36 PM Date Created
  • Views 4939 views
  • Likes 10 likes
  • Comments 9 comments
Related
Recommended
  • rpi pico
  • rtos
  • arduino mbed
  • multi-threading

Program RPi Pico using Mbed library with Arduino IDE

ralphjy
ralphjy
19 Apr 2021

When I wrote my initial blog about the Arduino-Pico library, fmilburn mentioned that Arduino had just released an Mbed library that had support for the RPi Pico https://github.com/arduino/ArduinoCore-mbed/releases/tag/2.0.0 .   The github installation instructions require cloning the ArduinoCore-mbed repo and the ArduinoCore-API repo and then adding a symlink to that API.  I had been wanting to use the Mbed RTOS functions with the Nano 33 BLE Sense board, but I guess I'll try it with the RPi Pico first.

 

Before I tried using the installation instructions, I decided to check the Boards Manager for Mbed supported boards.

image

 

And there is support the RPi Pico.  I also noticed that the previous entry that was used for the Nano 33 BLESense (Arduino Mbed OS Boards) has been deprecated.  So, I decided to do the install from the Boards Manager for the RPi Pico and the Nano 33 BLE Sense.

image

 

And it looks like it is the current version (2.0.0).  I don't have a Portenta yet so I didn't install that.

 

Then I took a look at available examples and there were only a few.

image

image

image

 

I found it interesting because the first two examples use an onboard PDM microphone and onboard RGB led - neither of which exist on an RPi Pico image.

 

It runs out that none of the examples will compile.  The first two for obvious reasons and the third I think because there is a missing architecture definition (this was a red herring).

image

image

image

 

Then I noticed that all the Mbed supported boards have the same examples.

image

image

 

All 3 examples will compile for the Nano 33 BLE Sense.  The first two examples were written for the Nano 33 BLE Sense and the third one was written for the Portenta.  They need to provide new examples for the RPi Pico...

 

So, not being an Mbed programmer, I go looking for Arduino IDE Mbed examples.  I find a few, but I run into problems with deprecated functions that no longer are available - simple things like wait() and wait_ms() - because the examples are too old.  And I get tripped up trying to replace the functions because of namespace issues, i.e. knowing where functions exist.  Just learning curve issues, but I realize I should verify I can do something simple and then build from there.

 

I go back to the basic Blinky example and blink the onboard green LED. Two seconds on, one second off.  Even that confused me for a second because I thought that I needed to specify the arduino namespace in order to use the delay() function, but I didn't.   LED1 is defined to be the onboard LED pin, but I haven't figured out where (not in pins_arduino.h).

 

RPi_Pico_Mbed_Blink.ino

#include <mbed.h>
using namespace mbed;

DigitalOut led(LED1);

void setup(){
}

void loop(){
  led.write(1);   // set LED1 HIGH
  delay(2000);

  led.write(0);   // set LED1 LOW
  delay(1000);
}

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

Then I modified a multi-threaded example to fade the onboard LED and an external Red LED on pin 22 using separate threads.

 

RPi_Pico_Mbed_2thread_LED_Fade.ino

/**
 * A basic example to show multithreading in mbed OS.
 * Two threads are created to fade the external red and onboard green LEDs.
 */
#include "rtos.h"

#define RED 22
#define GREEN 25

#define MS(x) chrono::milliseconds(x)

using namespace std; // we will be using std::chrono
using namespace rtos; // we will be using rtos::ThisThread

// Declaring the Thread objects
Thread led_red_thread;
Thread led_green_thread;

void setup_leds() {
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
}

// function to be attached to led_red_thread
void led_red_function() {
  for (;;) {
    // delay while blue fades out and green fades in
    ThisThread::sleep_for(MS(512));
    // Fade red out
    for(int i = 255; i >= 0; i--) {
      analogWrite(RED, 255 - i);
      ThisThread::sleep_for(MS(1));
    }
    // delay while green fades out and blue fades in
    ThisThread::sleep_for(MS(512));
    // Fade red back in
    for(int i = 05; i < 255; i++) {
      analogWrite(RED, 255 - i);
      ThisThread::sleep_for(MS(1));
    }
  }
}

// function to be attached to led_green_thread
void led_green_function() {
  for (;;) {
    // delay while blue fades out
    ThisThread::sleep_for(MS(256));
    // fade green in 
    for(int i = 0; i < 255; i++) {
      analogWrite(GREEN, 255 - i);
      ThisThread::sleep_for(MS(1));
    }
    // delay while red fades out and blue fades in
    ThisThread::sleep_for(MS(512));
    // fade green out
    for(int i = 255; i>= 0; i--) {
      analogWrite(GREEN, 255 - i);
      ThisThread::sleep_for(MS(1));
    }
    // delay while red fades in
    ThisThread::sleep_for(MS(256));
  }
}

int main() {
  setup_leds();
  led_red_thread.start(led_red_function); // start red thread
  led_green_thread.start(led_green_function); // start green thread
}

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

It seems the RPi Pico Mbed installation is okay, I just need to learn how to use it.

 

I think I'm finally getting my head wrapped around this.  It seemed that I should be able to take an unmodified Arduino sketch that I had working with the Arduino-Pico library and compile it with the Mbed library and everything should still work.  This is almost true.  To test it out, I tried compiling the GFX demo for the OLED - and it compiled but there was no display.

 

I then tried the NeoPixel test and that worked - the same code actually seems to run faster with the Mbed library.

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

I pondered why the OLED test didn't work.  I checked the pins_arduino.h file in the Mbed library and saw that the default Wire library I2C pins for SDA and SCL were GPIO 6 and 7.  For the Arduino-Pico library they are GPIO 0 and 1.  I moved the jumper wires over - and now it works!

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

 

So, I guess I just need to learn the Mbed functions, namespaces and pin assignments and I'll be able to use the RTOS and multi-threading with the RP2040 image.  It would be nice if there was a simple way to use both cores.  I'm sure someone has figured that out.

 

I should also mention that using the Mbed library requires that that you do Reset-Boot sequence every time you upload.  The Arduino-Pico library just required that you do it the first time and it handled it after that.  It's nice to just hit the Upload arrow...

  • Sign in to reply

Top Comments

  • kmikemoo
    kmikemoo over 1 year ago +4
    ralphjy I appreciate it when you share how you solve problems that you run across. Thanks.
  • fmilburn
    fmilburn over 1 year ago +4
    Hi ralphjy That's great that you figured out so much so quickly and thanks for posting. I think it would take me a lot longer. Hopefully Arduino will get the issues you found fixed and the documentation…
  • BigG
    BigG over 1 year ago +4
    I just did a deep dive and with some low level mods to the core Arduino pico libraries I got the 2 I2C ports working together via Wire and Wire1. Just tested with 2 Wii nunchucks... Once the two cores…
  • ralphjy
    ralphjy over 1 year ago in reply to BigG

    Sounds great - I have a couple of Nunchuck adapters that I haven’t used - just have to find them image.

     

    Look forward to your demo...

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • BigG
    BigG over 1 year ago

    I just did a deep dive and with some low level mods to the core Arduino pico libraries I got the 2 I2C ports working together via Wire and Wire1.

     

    Just tested with 2 Wii nunchucks...

     

    image

     

    Once the two cores can be used via Arduino, this will be pretty cool as each core could handle one nunchuck.

     

    Hmmm, I can feel a blog coming along to demo.

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • BigG
    BigG over 1 year ago in reply to BigG

    I've had my first success using mbed.

     

    I had a look at the Arduino github page for the board (as per link you gave above). They have done an impressive job so far creating the TARGET folder for this board. I opened up Mbed Studio and created the default blinky example. I had a look at the mbed os folder and I see this config for the Pico board is not implemented within.

     

    So back to Arduino IDE. Here you can still use Mbed OS code syntax. I did a quick test and discovered that only the bare metal option works as thread method (ThisThread::) is not implemented within the Arduino TARGET... but I now see that you've picked this up and I need to include #include "rtos.h"

     

    Learning by doing...

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • BigG
    BigG over 1 year ago in reply to ralphjy

    This is pure speculation on my part, but I'm convinced that computers are like dogs - if they smell fear they play up. So, your blog gave me the confidence to go for it... and the Blinky program has just been successfully uploaded onto my Pico board via the Arduino IDE without any issue. Happy days. My next mission... the world image

     

    ... ok maybe just I2C (mbed).

     

    Will update on status once attempted.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • ralphjy
    ralphjy over 1 year ago in reply to BigG

    Good to know.  I'll look at those links.  I've had good and bad experiences with RPC over UART between processors, but would be worth trying Embedded RPC as I imagine it's more robust between cores.

     

    I guess they're really serious about eliminating deprecated functions.  I really should invest the time to read the documentation (RTFM image).

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube