In the previous blog, I showed how to get all the dependencies required to be able to use Mbed with the Arduino IDE (if you haven’t read that yet, click here). In this blog I will be showing how to get into programming with a few examples. Let’s get started!
Simple Blink Example
First, let’s rewrite the the Blink example (which could be called the “Hello World” of Arduino programming) using Mbed functions instead of the regular Arduino functions. For reference, here is the regular Blink example-

Start by opening a new sketch and including the Mbed header file. Then, type in the following code before the setup, which creates an object of the DigitalOut class to control the state of pin LED1. I have named it led but you can use any valid identifier name. LED1 refers to the pin name of the built in led of your Arduino (pin 13 in this case). A list of all the pin numbers and their corresponding Mbed pin names is given at the end of this blog for reference.

Next, enter the following code in the loop, which sets the state of led to 1 (or HIGH), waits for 1 second, sets it to 0 (or LOW), then waiting for another second, with everything repeating indefinitely. Setup can be left empty.

The completed code should now look like this-

Finally, compile and upload the code. You should start to see the built in LED of your Arduino blink. You can even try playing around with the wait values and see the differences in the output, to really make sure it is working.
Spawning Threads
In this example, we will spawn two threads which can perform multiple tasks simultaneously, giving the effect of multitasking.
Once again, start by including the Mbed header file and the Mbed namespace, but this time also include the rtos namespace, under which the thread class and all its functions are present. Now, create two threads as shown-

I have named the threads t1 and t2, however you can use any valid identifier name. These threads will require code to execute, so create two functions to pass to them. I have named the functions func1 and func2. These are the thread functions which our two threads will execute respectively. Remember that these functions must not require any parameter or return any value.

In the above example, the func1 and func2 print “A” and “B” and wait for 2 and 3 seconds respectively. To make them work indefinitely, while(1) loops have been used.
Finally, in the setup, begin Serial communication and start the threads by passing them the functions as parameters. The main loop function can be kept empty as the two threads will execute some code.

Here is how the completed code should look-

Now, compile and upload the program and open the Serial monitor. You should see the letters A and B being printed onto the Serial monitor asynchronously.

And that’s it, you’re all done!
If you found this blog useful, I would love to hear your opinions/suggestions in the Comments below.
Reference
Here are the pins mappings from Arduino to Mbed-
Arduino Pins | Corresponding Mbed name |
D0/TX | P1_3 (not recommended for use) |
D1/RX | P1_10 (not recommended for use) |
D2 | P1_11 |
D3 | P1_12 |
D4 | P1_15 |
D5 | P1_13 |
D6 | P1_14 |
D7 | P1_23 |
D8 | P1_21 |
D9 | P1_27 |
D10 | P1_2 |
D11 | P1_1 |
D12 | P1_8 |
D13/Built in LED | P0_13 |
A0 | P0_4 |
A1 | P0_5 |
A2 | P0_30 |
A3 | P0_29 |
A4 | P0_31 |
A5 | P0_2 |
A6 | P0_28 |
A7 | P0_3 |
Power LED | P1_9 |
For whatever reason wait() and wait_ms() do not work for me. I have resorted to wait_ns() to get your examples working here. Not sure what might have changed. Using arduino 1.8.13 on linux.
LikeLiked by 1 person
Which version of the Mbed core are you using? You can check this in the Boards manager next to the Mbed core installation.
LikeLike
I too am having issues with the “wait(n)” command. I’m also using IDE 1.8.13 and I’m using the latest board version (1.3.0). I noticed in your previous “getting started” page, you are using an much older version, 1.1.4. I’ve tried reverting to this older version and it did work.
LikeLiked by 1 person
The function wait is deprecated in favor of explicit sleep functions. To sleep, replace wait with ThisThread::sleep_for (C++) or thread_sleep_for (C). To wait (without sleeping), call wait_us. wait_us is safe to call from ISR context.
LikeLiked by 1 person
Hi Mario,
Thank you for your comment. I have updated the programs to use the latest APIs and removed the deprecated one.
LikeLike
Thank you for you blog, it help me to start using my Arduino Portenta H7. Just a small comment on the Threads example, “using namespace rtos” instead of “usint namespace rtos”
LikeLike
Hey Nguyen,
Glad you liked the blog! Thanks for pointing out the typo, I’ve made the appropriate changes.
LikeLike