Native real-time and multithreaded programming on the Arduino Nano 33 BLE (Mbed OS)

A long time ago, I released a series about real-time programming on a BeagleBone Black. I then decided to use the BBB to control the CRT display of an old Macintosh Classic computer. As you can imagine, I was thrilled when the new Arduino Nano series with built-in real-time capabilities was announced and in this article, I’d like to revisit the old topic and discuss what has changed over the years.

The new Arduino Nano

And this is where the new Arduino Nano series, which was announced earlier this year, comes into play. The new Nano 33 BLE uses a customized version of the Mbed OS, which is a real-time operating system.

Figure 1: The new Arduino Nano 33 BLE

The Arduino IDE was adapted so that it now fully supports the core features of the Mbed OS core. That means, that you can now have multiple threads running in your applications. Furthermore, Mbed OS also supports different synchronization techniques (like semaphores and mutex mechanisms).

However, I was surprised to see that there is no documentation from Arduino that discusses this matter. And, while the official Mbed API is a great starting point, the Arduino has a few quirks that are not documented. So I thought it’d be great to upload a few examples that should make it easier for non-Mbed developers to get started.

Getting started guide and multithreaded programs

As mentioned above, the standard Arduino IDE can be used to access the new features on the Nano 33 BLE series of boards. A quick start guide, together with a few examples, is available here.

The example programs can be downloaded at the end of this article that I wrote. I discussed the programs there as well and I also added comments to the source code so I won’t discuss them another time on this page. However, if you have any questions, feel free to leave a comment!

Real-time and timing features

One thing, that was always missing on this platform, is the ability to precisely wait for a short amount of time. I discussed this issue earlier, when I wrote about precise timings on a Raspberry Pi.

Now, that the new Arduino boards have a real-time OS, this issue should have been resolved, right? Well, kind of. While there are different functions in the new Arduino core, that can be called without any problem at all, the waiting is not as precise as one would expect from a real-time system.

Let’s look at the following code example:

#include <mbed.h>
#include <mbed_wait_api.h>

using namespace mbed;

void setup()
{
  Serial.begin(9600);

  pinMode(5, OUTPUT);
}

void loop()
{
  digitalWrite(5, HIGH);
  wait_ms(100);
  digitalWrite(5, LOW);
  wait_ms(100);
}

As you can see, the application simply pulls pin 5 high, waits for 100 milliseconds and then pulls the pin low again before waiting for another 100 ms. The oscilloscope measures something like this:

Figure 2: Waiting 100 milliseconds works perfectly fine

With a long delay like this, there are no issues with the precision. However, that’s nothing new. The older Arduino core was capable of the same thing. Anyway, when using the wait_ns functions of the Mbed OS to wait 100 nanoseconds, the following happens:

Figure 3: Waiting 100 nanoseconds causes problems

So as you can see, it still doesn’t manage to wait precisely. Even though it is better than it used to be.

However, the OS puts the Arduino hardware to a sleep mode whenever it has to wait. This will at least make the Arduino consume less power when it’s not needed.

Conclusion

Even though the Mbed OS still didn’t allow me to wait precisely, it adds multithreading and synchronization capabilities to the standard Arduino environment and I love that. The fact, that these new features can be mixed with all the well-established other Arduino libraries and standard features makes this the best update that the platform has received in a long time.

Figure 4: Top view of the Arduino 33 BLE

Unfortunately, it’s still not possible to wait precisely for shorter periods of time. However, a few milliseconds worked perfectly fine when I tested it.

2 thoughts on “Native real-time and multithreaded programming on the Arduino Nano 33 BLE (Mbed OS)

  1. I found your article useful but struggling to find documentation on how to suspend, terminate and resume thread according to requirement. can you please guide on the same.

    Like

Leave your two cents, comment here!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.