Getting Started With C/C++ On The Micro:bit
Written by Harry Fairhead   
Thursday, 21 April 2016
Article Index
Getting Started With C/C++ On The Micro:bit
A simple example project

Your First Program

At the moment the samples provide the best starting template for you own projects. Create a new project based on the samples template and call it pulsetest

Next delete all of the source files in the Source directory. The quickest way to do this is to delete the Source directory and then add it back as a new directory. Create a new file called main.cpp - you can call it anything you like but the name main gives you the clue that it is the start of the program. 

Enter the following code:

#include "MicroBit.h"

MicroBit uBit;

int main() {
 uBit.display.scroll("NEW PROGRAM");
 while (1) {;;





Compile the project, download the program and if you connect a logic analyser or oscilloscope to P0 you will see the fastest pulse that the micro:bit can produce.  




So programming in C/C++ gives you a 3.5 microsecond pulse or a 133kHz square wave. There is also a 0.1ms pause every 6ms, probably the system timer interrupting as the documentation gives the granularity of the system timer as 6ms.

This should be compared with the same program in MicroPython that produces a 72 microsecond pulse, i.e around 20 times slower. 

Is it possible to do better than 3.5 microseconds?


The answer to the question is yes, quite a lot better. With only little additional work you can shorten the pulse width to around 0.5 microseonds. If anyone wants to know how - just ask. 


Finally if you want a standard "blinky" test program - which as mentioned is the real hello world of mbed programming why not try:


The display function makes use of the display buffer implemented by the micro:bit library. There are ways of getting at the raw LEDs without using the display buffer but this is slightly more complicated. 


You now know how to create and run a C/C++ program for the micro:bit. At this point you could start to code anything you want. If you want to work offline however then you will need to do a little more work. Exactly how to work off line and how to use NetBeans as your development environment is described in the next article


More Information


Related Articles

Offline C/C++ Development With The Micro:bit

Commando Jump Game For The Micro:bit In Python 

The BBC Micro:bit Is An Mbed Device In C/C++ 

BBC Micro To micro:bit 


To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, FacebookGoogle+ or Linkedin



200 Years Ago Charles Babbage Proposed His Difference Engine

Two hundred years ago today, on June 14, 1822 Charles Babbage presented a one-page note to the Royal Astronomical Society in London setting out his plans to build a Difference Engine that wo [ ... ]

New Database Drivers for Oracle and PostgreSQL Released

The PostgreSQL JDBC Team team has announced the release 42.4.0 of the JDBC driver for PostgreSQL. Meanwhile Oracle has released a Python-based driver for Oracle. Python and Java users rejoice!

More News






or email your comment to:


Last Updated ( Sunday, 10 July 2016 )