The micro:bit is a capable IoT device and to make the point we show how to connect and use the very popular DS18B20 temperature sensor.


The Maxim 1-Wire bus is a proprietary bus that is very easy to use and has a lot of useful devices you can connect to it including the iButton security devices.  However, probably the most popular of all 1-wire devices is the DS18B20 temperature sensor - it is small, very cheap and very easy to use. 

Easy to use, but only if the processor supports the 1-wire bus protocol and the micro:bit doesn't. However, the protocol is easy enough to program in C and the micro:bit is fast enough to work with it without needing anything extra. Of course, we have to work in C; Python, for example, just isn't fast enough.

The micro:bit does have an onboard temperature sensor, but this measures the temperature of the CPU and only gives you the air temperature very approximately. The DS18B20 gives you an accurate temperature and you can have multiple sensors. 

The Hardware

The DS18B20 is available in an number of formats but the most common makes it look just like a standard BJT - which can sometimes be a problem when you are trying to find one.

You can also get them made up into waterproof sensors complete with cable. 

No matter how packaged, they will work at 3.3V or 5V, but not at 1.8V without a level converter.  Its basic specification is:

  • Measures Temperatures from -55°C to +125°C (-67°F to +257°F) 
  • ±0.5°C Accuracy from -10°C to +85°C 
  • Thermometer Resolution is User Selectable from 9 to 12 Bits 
  • Converts Temperature to 12-Bit Digital Word in 750ms (Max)

It can also be powered from the data line making the bus physically need only two wires - data and ground - however this "parasitic power" mode is difficult to make work reliably and best avoided in an initial design. 

In normal powered mode there are just three connections:


Ground needs to be connected to the system ground, VDD to 3.3V and DQ to the pull-up resistor of an open collector bus. 



There can be multiple devices on the bus and each one has a unique 64-bit lasered ROM code, which can be used as an address to select the active devices. For simplicity, it is better to start off with a single device and avoid the problem of enumerating the devices on the bus - although once you know how everything works this isn't difficult to implement. 

However there is a problem we have to solve - the need for fast switching between input and output. 

The problem is that changing between output and input mode is done using the system and it is comparatively slow - approximately 80 microseconds. 

In some cases this does matter because the protocol requires data from the master and then receives data from the slave. For example, the DHT22 temperature and humidity sensor asks the processor to write to it to start a measurement and then sends data back without the processor writing anything else. In this case we could use a single GPIO line, send the data, switch to input and read the data. 

In the case of the 1-wire bus things are very different.

The master has to send a fast pulse on the line for every bit received and it has to be able to read the response in around 15 microseconds. This is too fast to allow for time to change the line from output to input in time to read the data. It could be done by writing directly to the GPIO registers 

A secure and simple solution that should work with all 1-wire bus devices is to use two GPIO lines - one set to output and one set to input. 

If you can't afford to use two GPIO lines then there is a way to do the job with one but only by resorting to directly accessing the GPIO line and not using the mbed library functions. See the end of this chapter for more details. 

You can use any pair of GPIO lines but for simplicity this example uses P0 and P1. The DS18B20 can be directly connected to the micro:bit's PADs. The only complication is that you do need the pull up resistor. The internal pull up resistor is around 10K to 15K and is just too large to work. You need to put a 4.7K resistor in parallel with it to decrease the resistance. 

The circuit is:




You can build it in a variety of ways. You can solder the resistor to the temperature sensor and then use some longer wires with clips to connect to the micro:bit. You could also solder directly to the micro:bit or use a prototyping board.