The new Arduino Nano 33 BLE development boards come with a different core that’s based on the Mbed real-time OS, allowing access to the many features of the RTOS and its functions in your Arduino sketches.
In this article, I’ll discuss the steps needed to get started with real-time programming on your Arduino and give you a few examples to get you up and running in no time.
Configuring the Arduino IDE
Thankfully, the Arduino engineers decided to keep all the features from the old Arduino core. That means that you can still use the classic Arduino IDE and program your new Arduino Nano like you always have.
To get started, connect the Arduino to your computer. The necessary drivers should be automatically installed. Once that’s done, open the Arduino IDE and navigate to “Tools”, then “Boards” and choose the “Boards Manager”. A new window will appear. In it, search for “Arduino Nano 33 BLE” and install the following package:
Then, choose the newly installed board from the boards menu:
A Hello World Application with Nano 33 BLE
Let’s start with a simple application that’ll help us to determine whether the Mbed libraries were properly installed and are functioning as intended. The following code will simply output a square wave signal on the D5 pin of the Arduino Nano 33 BLE:
As you can see, you can easily mix old Arduino functions with the new Mbed methods. The code doesn’t look like a lot, but if you’re able to compile and upload it to your Arduino, you’re ready to go. It’s also worth noting that, unlike the standard Arduino delay, the Mbed wait functions try to put the Arduino into a low-power mode.
The MBed API and Available Features
As I previously mentioned, you can access many features of the Mbed OS. Unfortunately, I was unable to find an official source that states what features are available.
If you take a look at the MBed API, you’ll find a few device-specific APIs (for example, storage and NFC). I think it’s obvious that those won’t work.
However, the platform and RTOS features are implemented and the USB, Drivers, and Bluetooth BLE libraries should work as well, but I haven’t tested them. But as stated above, you can mix the MBed features with the classic Arduino calls so it shouldn’t be hard to find a solution.
The Platform API
The following example uses the CircularBuffer from the MBed platform API:
The program simply writes ten values to a ring buffer and then prints the stored values.
Creating a Multithreaded Application
Now let’s take a look at the main feature of the new core: multi-threaded Arduino sketches. You can use the RTOS namespace to create, synchronize, and manage threads. The following example will spawn two threads that will modify a shared buffer. The synchronization is done by a semaphore.
The main thread will then read the buffer and print its contents to the console if it’s not empty:
The writeBuffer method is executed by the two child-threads and, once started, will run forever. Each thread asks the semaphore for permission to write to the buffer before modifying it. If no other thread is using it, the access is granted and the thread writes its name to the buffer.
In the setup function, two child-threads are created and started. The loop method represents the third thread which will read the contents from the buffer and print it to the console:
As you can see, each name gets printed correctly and nothing gets overwritten by another thread. You can also observe that Bill appears more often. That’s perfectly normal as this is not handled by our application.
However, the RTOS namespace offers many more locking mechanisms that can be used to overcome this problem.
Utilizing the Mbed Core Features Takes Practice
The new Arduino Core offers many useful additions to the well-established platform, especially when it comes to multithreading and concurrent access control for shared resources. However, due to the nature of parallel computing, these features can be complicated to work with.