External Links

Creative Science Centre

 

DHTnn

The DHTnn is a temperature and relative humidity sensor. This currently deals with the DHT11 a very low cost device. The main disadvantage is that the temperature only reads down to 0 which is no good for outside in the UK. The DHT22 is a much better device that will read sub zero temperatures but is five times the cost.

The library is for both the DHT11 and DHT22.

// #include "http://www.byvac.com/mBlib/flb/Library/2016/lib_DHTnn.bas"

This is a plug-in so will need saving to flash if not already.

Hardware

This applies to the DHT11 and DHT22, the DAT pin should be connected to 3v3 or 5V (requires a 5V tolerant pin) vie a 4k7 resistor.

Software

  • dht.init(*RB1(),*TIMER2()) Initialises the port used and variables, must be used first.
  • dht.read() returns a packed 32 bit value an example as follows:

The above uses a plug in to read the sensor direct, ByPic is then used to interpret the 32 bit word returned.

DHT11: Example value returned 0x76543210
76   is integer humidity
54   is decimal humidity

32   is integer temperature
10   is decimal temperature

DHT22: Example returned value 0x028c015f

0x28c is a 16 bit * 10 humidity value

0x15f is a 16 bit temperature value. If bit 15 is 1 then it is a negative value

  • dht.temp11$(x) x is the value returned by dht11_read(), this returns a string representing the read temperature.
  • dht.rh11$(x) x is the value returned by dht11_read(), this returns a string representing the read humidity.
  • dht.temp22$(x) x is the value returned by dht22_read(), this returns a string representing the read temperature.
  • dht.rh22$(x) x is the value returned by dht22_read(), this returns a string representing the read humidity.

Example of use:

dim x
   dhtnn_init(*RB1(),*TIMER2()) // only needed once per reset
   x = dht.read() // this can only be called once every 2 seconds
   print "\ntemp=",dht.temp11$(x)
   print "\nrh=",dht.rh11$()

 

Error Codes

When reading the device 'dht11_read()' it should return the required data however it may return one of the following error codes.

  • 0xfffffff0 This is when the device did not pull the bus low after the initial start condition sent by the ByPic. The device may be faulty.
  • 0xfffffff1 The device pulls the bus low for 80uS and then pulls it high again. This codes is sent if the device did not pull the bus high after the initial low above.
  • 0xfffffff2 Start bit was not received
  • 0xfffffff3 Time out error whilst receiving data