Article Index

Anyone who wants to use the BBC micro:bit to its full potential as an IoT device needs to look outside the coding environments provided by its own website. As an mbed device, however, the micro:bit  is capable of being programmed in C/C++. Here we look at how to use the mbed online compiler for a simple demo program.

Now On Sale!

You can now buy a print edition of micro:bit IoT in C.

You can buy it from:

USA and World


 The full contents can be seen below. 

Chapter List

  1. Getting Started With C/C++
    Anyone who wants to use the BBC micro:bit to its full potential as an IoT device needs to look outside the coding environments provided by its own website. As an mbed device, however, the micro:bit  is capable of being programmed in C/C++. Here we look at how to use the mbed online compiler for a simple demo program.

  2. Offline C/C++ Development  
    We have already discovered how to use the online editor to create a C/C++ program. Now we are going to move to the desktop with an offline approach. This has the advantage that we can use any tools we care to select and no Internet connection is needed.
  3. First Steps With The GPIO 
    The most basic task when working with the micro:bit is controlling the I/O lines. This isn't difficult if you use the framework provided but there some subtle points to watch out for. This chapter looks a the basics of using the GPIO.

  4. Working Directly With The Hardware - Memory Mapping. 
    The framework makes working with the GPIO and other devices as easy as it can be but there are many layers of software to go through before you get to the hardware. Writing directly to the hardware can make things up to ten times faster and give you access to things that their framework doesn't. It is also an educational experience to deal with the raw hardware directly.

  5. Pulse Width Modulation, Servos And More
    In this chapter we take a close look at pulse width modulation PWM including, sound, driving LEDs and servos.

  6. I2C
    The I2C bus is one of the most useful ways of connecting moderately sophisticated sensors and peripherals to the any processor. The only problem is that it can seem like a nightmare confusion of hardware, low level interaction and high level software. There are few general introductions to the subject because at first sight every I2C device is different, but here we present one.

  7. I2C Temperature Measurement
    Using I2C devices is fairly easy once you have successfully used one - and hence know what information you need and what to look for in a working system. In this chapter we use the HTU21D temperature and humidity sensor as a case study of I2C in action. It also happens to be a useful sensor.

  8. A Custom Protocol - The DHT11/22

  9. The DS18B20 - One Wire Bus

  10. The SPI Bus
    The SPI bus can be something of a problem because it doesn't have a well defined standard that every device conforms to. Even so if you only want to work with one specific device it is usually easy to find a configuration that works - as long as you understand what the possibilities are. 

  11. SPI MCP3008/4 AtoD   
    The SPI bus can be difficult to make work at first but once you know what to look for about how the slave claims to work it gets easier. To demonstrate how its done let's add eight channels of 12 bit AtoD using the MCP3008.

  12. Serial Connections
    The serial port is one of the oldest of ways of connecting devices together but it is still very, very useful. The micro:bit has a single serial interface but it can be directed to use any of the GPIO piins as Rx and Tx. 

  13. WiFi 
    The micro:bit has a radio that works in Bluetooth LE and point-to-point ad-hoc mode, but at the moment it lacks WiFi connectivity. The solution is to use the low cost ESP8266 to make the connection via the micro:bit's serial port. 

  14. LED Display 
    The micro:bit's LED display may only be 5x5 but it is very versatile. If you want to make use of it directly then you are going to have to master some lower level functions.



The fact that the micro:bit is an mbed device brings with it both good and not so good news. The good news is that you can program the micro:bit in C/C++ because it is an mbed system. The bad news is that the documentation only gives you a hint as to how to do it. Here we explore how to use the online compiler. In a follow-on tutorial we'll find out how to do the same job offline using NetBeans. While I'm specifically looking at the Micro:bit, much of this will apply to any mbed device.

If you want to get started withC/C++ on the micro:bit quickly and don't mind signing up to an online service then it is all very easy. On the other hand, if you have a preference for a desktop IDE or just a command line interface things are a bit more difficult. The reason is that mbed comes with a brand new make replacement called yotta. It's not that yotta is bad, it is just another thing to learn. It is also surprising how complicated embedded programming has - become so much so that it actually need something this complicated. We seem to be a long way from the C programming running on almost bare metal. Once upon a time the hardware was the tough part - now it's the software as well.

So if you want an easy life go for the online compiler. 

If you want more control, but perhaps more complexity, go for the offline approach. 

I personally prefer the off line approach using NetBeans and suggest that after you have tried things out with the online compiler you spend a few hours getting things set up for NetBeans development, which is described in the next article.

Why C/C++?

Before you spend time finding out how to program in C/C++ there is the good question of why bother? After all, you have the choice of Touch Develop, JavaScript and MicroPython to program the micro:bit and they are all fairly easy to use. 

The answer to the question of why C/C++ is that it gets you closest to the hardware. Not all of the features and facilities of the micro:bit are accessible from the higher level languages. For example, at the moment you can't get access to the micro:bit's radio. Using C/C++ and the microbit library you can use the BlueTooth radio as a simple point to point radio connection between micro:bits. This makes it possible to build radio control robots and similar gadgets - something much harder if all of the connections have to go through the BlueTooth stack. 

Another example is the LED control. In MicroPython you have nine brightness levels. In the micro:bit's other supplied languages the LEDs are either on or off. In C/C++ you can set any of 255 levels. 

Going beyond the standard hardware you can also implement missing hardware facilities. For example, there is no SPI bus available under any of the supplied languages, only an I2C bus. Using C/C++ you can access a standard mbed SPI driver and work with SPI peripherals. You can even use your own code to implement protocols that are not supported such as the one-wire bus and the DHT11/22 temperature humidity sensor. 

The reason you can implement these additional protocols is that C/C++ is about as fast as you can get on any machine. You might be able to get some more speed out of an assembly language version of a program, but it would only be a few percent. Using C/C++ lets your programs run tens of times faster. For example, the blinky program given below runs about 20 times faster than the equivalent MicroPython program. When you are writing embedded programs often speed really does matter. You might not be able to write it in MicroPython and have to move to C/C++. If you can't write it in C/C++ then there is a good chance it can't be done using the hardware you have. 

C/C++ isn't significantly harder to work with than other languages. C is particularly simple and easy to use. The micro:bit library is written in C++, but for most of the time you can make use of it in a dialect of C which is quite good enough for the task.

The online compiler

The online compiler for the micro:bit is the best way to get started. 

To use the online compiler you have to sign up with mbed.

Go to:

and sign up for an account. At the time of writing, to find the online compiler a you have to go to the mbed Classic Developer site, but this could change. 

Once you have an account and have signed in, your first task is to get the compiler set up with the micro:bit as its target. You can either go to Platforms and select the vendor as BBC to see the one and only product in that category, i.e. the micro:bit'


Alternatively you can go to the compiler and select the target device icon in the top right, which initially shows as "No device Selected". Click the Add Platform button and you are back to the Platform selection page. Select the BBC micro:bit and you are back in the same place.

The micro:bit page contains a lot of information but if you just want to work with it in the compiler simply select the Add to your mbed Compiler at the right of the screen:




Following this when you go back to the compiler you will see the BBC micro:bit as the target platform in the top right hand corner:


Hello Blinky

You are now ready to start work on a project but at the moment there isn't a good starter project template. However Lancaster University has provided a template called Blinky to flash an LED - the equivalent of a Hello World program for the device.

There are two way to work with the micro:bit as a pure mbed device or using the specific library that Lancaster University have built. As long as you don't want to change to a different device the Lancaster library is the one to use. The mbed library makes it easy to write a single program that will work on different hardware and this is its big advantage. You can use the mbed library with the micro:bit but unless you really do want to move to another piece of hardware it is better to use the micro:bit library.

To get a starter project you have to select the samples template because the blinky templates use the mbed library.


It takes some time to build the project but eventually you will see the finished project ready for you to edit. 

The C/C++ programs are all in the source directory as usual but the difference is that you are using the microbit library rather than the usual mbed library. 



If you want to you can compile (ignore the compiler warnings) and run the sample without modification just to check everything works. If you select the compile button the program is compiled and then downloaded to your machine. You then have to download the file - microbit-samples_NRF51_MICROBIT.hex - the the micro:bit via the usb drive in the usual way. When you run the program you will discover that by default you will see a Hello World message scroll across the screen. 

If you want to see any of the other demos in action simply open the MicroBitSamples.h file and uncomment the line corresponding to the one you want to run.