Programming with Mbed on Arduino (part 2)

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.


Here are the pins mappings from Arduino to Mbed-

Arduino PinsCorresponding Mbed name
D0/TXP1_3 (not recommended for use)
D1/RXP1_10 (not recommended for use)
D13/Built in LEDP0_13
Power LEDP1_9


  1. 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.

    Liked by 1 person

  2. 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.

    Liked by 1 person

Leave a Reply

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

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

Google photo

You are commenting using your Google 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