The MMBasic 5.0 HUMID command will read the humidity and temperature from a DHT22 humidity/temperature sensor.
The DHT22 can be powered from 3.3V or 5V (5V is recommended) and it should have a pullup resistor (4.7 kohm) on the data line. This is suitable for long cable runs (up to 20 meters) but for short runs the resistor can be omitted as the CGMICROMITE2 (CGMICROKIT) or CGMICROBOARD also provides an internal weak pullup.
To get the temperature or humidity you use the HUMID command with three arguments as follows:
HUMID pin, tVar, hVar
Where:
‘pin’ is the I/O pin to which the sensor is connected. You can use any I/O pin but if the DHT22 is powered from 5V it must be 5V capable. The I/O pin will be automatically configured by MMBasic.
‘tVar’ is a floating point variable in which the temperature is returned
‘hVar’ is a second variable for the humidity.
The temperature is returned as degrees C with a resolution of one decimal place (eg, 23.4) and the humidity is returned as a percentage relative humidity (eg, 54.3). If there is an error (eg, the sensor is not connected or communicating correctly) the command will return 1000 for both the temperature and humidity.
For example:
HUMID pin, temp, humid
PRINT "The temperature is" temp " and the humidity is" humid
The datasheet recommends a one second delay after power up before the first reading is made and a two second interval between each measurement.