In this chapter we make use of all of the ideas introduced in earlier chapters to create a raw interface with the low cost DHT11/22 temperature and humidity sensor. It is an exercise in implementing a custom protocol directly in C. 

Now On Sale!

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

You can buy it from:

USA and World  Amazon.com
Canada              Amazon.ca
UK                      Amazon.co.uk
France                Amazon.fr
Germany            Amazon.de
Spain                  Amazon.es
Brazil                  Amazon.br
Italy                    Amazon.it
Japan                 Amazon.co.jp
Mexico               Amazon.com.mx 

 

 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 Device

The DHT22 is a more accurate version of the DHT11 and it is used in this project but the hardware and software will work with both version and with the AM2302 which is similar to the DHT22. 

Model AM2302/DHT22

Power supply 3.3-5.5V DC
Output signal digital signal via 1-wire bus 
Sensing element Polymer humidity capacitor
Operating range
  humidity 0-100%RH;
  temperature -40~80Celsius
Accuracy
 humidity +-2%RH(Max +-5%RH);
 temperature +-0.5Celsius
Resolution or sensitivity
 humidity 0.1%RH;
 temperature 0.1Celsius
Repeatability
 humidity +-1%RH;
 temperature +-0.2Celsius
 

 

So the device will work at 3.3V and it makes use of a 1-wire open collector style bus which makes it very easy to make the physical connection to the micro:bit.

The one wire bus used isn't standard and is only used by this family of devices so we have little choice but to implement the protocol in C. 

 

 The pin outs are:

  1. VDD
  2. SDA serial data
  3. not used
  4. GND

and the standard way of connecting the device is:

Although the recommended pull up resistor is 1K a higher value works better with the micro:bit - typically 4.7K but larger will work.

The serial protocol is also fairly simple:

  • The host pulls the line low for between 0.8 and 29 ms,
    usually 1ms
     
  • It then releases the bus which is pulled high 
     
  • After between 20 and 200 microseconds, usually 30 microseconds, the device starts to send data by pulling the line down for around 80 microseconds and then lets float high for another 80 microseconds. 
     
  • Next 40 bits of data are sent using a 70 microsecond high for a 1 and a 26 microsecond high for a zero the high pluses are separated by around 50 microsecond low period. 
     

 

 

So what we have to do is pull the line low for 1ms or so to start the device sending data and this is very easy. Then we have to wait for the device to pull the line down and let it pull up again - about 160 microsecond and then read the time that the line is high 80 times.

A one corresponds to 70 microseconds and a zero corresponds to 26 microseconds.

This is within the range of pulse measurement that can be achieved using standard library function. There is also a 50 microsecond period between each data bit an this can be used to do some limited processing. Notice that we are only interested in the time that the line is held high.