Approved content

The content below is provided by a partner.

alankrantas/pxt-dht11_dht22 0.0.3 GitHub

BBC micro:bit MakeCode editor extension for DHT11 and DHT22 humidity/temperature sensors

DHT11 and DHT22 are two popular sensors among microcontroller users, especially the cheap DHT11s. DHT22s cost more but have better data resolution.

My codes are based on MonadnockSystems/pxt-dht11 but modified the pin pull-up time calculating method based on some Arduino examples with several additional features. The extension also support DHT22s.

To import this extension, go to Advanced -> +Extension and enter “DHT11” or “DHT22” in the search box, or copy/paste https://github.com/alankrantas/pxt-DHT11_DHT22 into the search box. Press enter and click the extension.

img_0001

img_0002

Blocks

The extension can be used for DHT11 or DHT22 sensors. Use the first block to select module type and read data (it is recommended to wait between queries; at least 1 second for DHT11 and 2 seconds for DHT22), then use the second one to read either humidity level (%) or temperature (celsius).

This extension also verifies checksum from sensors. If the checksum is wrong, you’ll get -999 for both temperature and humidity readings, and “Last query successful?” block would return false.

1

microbit-screenshot

basic.forever(function () {
    DHT11_DHT22.queryData(
    DHTtype.DHT11,
    DigitalPin.P0,
    true,
    false,
    true
    )
    basic.showString("T: " + DHT11_DHT22.readData(dataType.temperature) + " *C")
})

You can also choose to output data via serial port:

DHT11 query completed in 25848 microseconds                   
Checksum ok                   
Humidity: 77 %                
Temperature: 22.01 *C         
----------------------------------------
DHT11 query completed in 25849 microseconds                   
Checksum ok                   
Humidity: 77 %                
Temperature: 22.01 *C         
----------------------------------------
DHT11 query completed in 25838 microseconds                   
Checksum ok                   
Humidity: 77 %                
Temperature: 22 *C            
----------------------------------------
DHT11 query completed in 25861 microseconds                   
Checksum ok                   
Humidity: 77 %                
Temperature: 22 *C            
----------------------------------------
DHT22 query completed in 26162 microseconds                   
Checksum ok                   
Humidity: 74.500000000000001 %
Temperature: 21.899999999999999 *C                            
----------------------------------------
DHT22 query completed in 26162 microseconds                   
Checksum ok                   
Humidity: 74.299999999999997 %
Temperature: 21.899999999999999 *C                            
----------------------------------------
DHT22 query completed in 26120 microseconds                   
Checksum ok                   
Humidity: 74.099999999999992 %
Temperature: 21.899999999999999 *C                            
----------------------------------------
DHT22 query completed in 26048 microseconds                   
Checksum ok                   
Humidity: 74 %                
Temperature: 21.899999999999999 *C                            
----------------------------------------

Temperature is default outputed in Celsius. You can also change it to Fahrenheit with the block “Temperature type” in the advanced panel.

Sensor versions and wiring

There are two types of DHT11/DHT22s, one without PCB boards and have 4 pins; another have PCB boards, built-in pull up resistors and only 3 pins. Personally I would recommend you to use PCB versions.

For the PCB version it’s easy: Vcc (+) to 3.3V or 5V (both works), GND (-) to GND, Data (out) to any GPIO pins you like.

dht11-pinout-for-three-pin-and-four-pin-types-2

Be noted that some PCB version sensors may have different order of pins.

If you are using the 4-pin version, you’ll need to add a resistor (in my test 220 Ω - 10 KΩ works under 3.3V; above that the sensor won’t response) between Vcc and Data to pull up the voltage of the data pin. Or you can simply use the pull up option in the query block - the micro:bit has internal resistors about 12-13 KΩ.

The third pin on the 4-pin sensor from the left is not used.

untitled sketch_bb

If a hardware pull-up resistor already exists, use the internal pull up resistor dosen’t seems to affect the results.

Powering up issue

The DHT modlue might not be responsive if you query it immediatly after powering up. In this case you’ll need to manually restart micro:bit while connecting to power, or add a small delay (like basic.pause(100)) in “on start” block.

The driver also checks if the sensor respond to protocol before querying (in case the sensor somehow disconnected). This result can be read by using a block in the advanced panel called “Last query sensor responding?”. (Note: the querying function may still freeze if the sensor disconnected right in the 20-30ms querying process.)

License

MIT

Supported targets

DHT11_DHT22=github:alankrantas/pxt-dht11_dht22#v0.0.3