Article Index

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.

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 micro:bit has 32 General Purpose I/O GPIO lines, although not all of them are available for use. Finding out how to use them is an important first step and you have to master both the hardware and the software aspects. 


Despite the ease of making connections to the micro:bit using ad-hoc cables and clips the GPIO lines are very low power and sensitive to voltages and currents outside of their normal range.
Do not connect anything to the micro:bit, especially for output, without considering the voltage and current applied to the GPIO line.

The voltage should always be 3.3 or less and the current should be 0.5mA or less.  

Pin Outs

The big irritation with working with any device that has GPIO lines is what do you call them. The manufacture of the electronic will have given them names like GPIO 01, GPIO 02 and so on but when the processor is used in a finished product like the micro:bit these lines are brought out to physical pins or connections on the printed circuit board and these are then often called Pin01, Pin02 and so on. The point is that there is no need for Pin01 to correspond to GPIO 01 and indeed in most cases pin numbers and GPIO numbers are fairly independent.

Why does this matter?

The main reason is that the low level documentation provided by the chip manufacturer generally uses GPIO numbers and the documentation provided by the device manufacture generally uses Pin number.

You can see the potential for confusion and mistakes. 

The framework functions all expect you to specify the GPIO number of the line you want to use but it provides constants that map Pin number to GPIO number. 

The definition of the constants can be found in MicroBitPin.h and as you can see there is no numerical logic in the assignments even more so when you remember that the pins on the micro:bit's edge connector are not sequential!

Pin                                GPIO
​MICROBIT_PIN_P0                    P0_3
MICROBIT_PIN_P1                    P0_2
MICROBIT_PIN_P2                    P0_1
MICROBIT_PIN_P3                    P0_4
MICROBIT_PIN_P4                    P0_5        
MICROBIT_PIN_P5                    P0_17
MICROBIT_PIN_P6                    P0_12
MICROBIT_PIN_P7                    P0_11
MICROBIT_PIN_P8                    P0_18
MICROBIT_PIN_P9                    P0_10
MICROBIT_PIN_P10                   P0_6
MICROBIT_PIN_P11                   P0_26
MICROBIT_PIN_P12                   P0_20
MICROBIT_PIN_P13                   P0_23
MICROBIT_PIN_P14                   P0_22
MICROBIT_PIN_P15                   P0_21
MICROBIT_PIN_P16                   P0_16
MICROBIT_PIN_P19                   P0_0 
MICROBIT_PIN_P20                   P0_30

The P0_n constants are the actual GPIO numbers and these are defined in PinNames.h as an enumeration with P0_0=0, P0_1=1 and so on. 

This would be the end of the story if it wasn't for the fact that some of the GPIO lines are used for specific jobs within the micro:bit. The standard pin out diagram is:


As you can see most of the GPIO lines already have jobs. If you want to use them for your own purposes then what ever they are doing for the micro:bit has to be turned off. For example if you use any of the GPIO lines that are assigned to the LED display then you cannot use the LED display and if you do then this will take over the GPIO lines that you are nominally using. 

The only GPIO lines that are completely free to use are

Free GPOIO Pins 

If you are prepared not to use the LED display then you can add to this list:

LED Display Pins

If you are prepared not to use Button A and B you can add:

Button Pins

In most cases you probably don't want to make use of the SPI bus so you can to the list:

SPI Bus Pins

Finally we have three pins dedicated to the I2C bus which you can use but you need to know that these are connected to the accelerometer and magnetometer which you can use if you take them over. Notice that you can use them for your devices on the I2C bus without disabling the on board devices:

I2C Bus Pins

There are also some GPIO lines used internally and not brought out onto the edge connector. 

For example as well as six pins on the edge connector the LED display makes  use of:

    COL1 = p4,
    COL2 = p5,
    COL3 = p6,
    COL4 = p7,
    COL5 = p8,
    COL6 = p9,
    COL7 = p10,
    COL8 = p11,
    COL9 = p12,

    ROW1 = p13,
    ROW2 = p14,
    ROW3 = p15,

The USB serial interface uses:

    TGT_TX = p24,
    TGT_RX = p25,

Finally three GPIO lines are used as interrupt lines from the accelerometer and magnetometer:

    ACCEL_INT2 = p27,
    ACCEL_INT1 = p28,

    MAG_INT1 = p29,

Notice that while you can't use any of these internal GPIO lines for your own devices knowing what they control can still be useful.