This is only a preview of the February 2017 issue of Silicon Chip. You can view 44 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Articles in this series:
Items relevant to "GPS-synchronised Analog Clock Driver":
Items relevant to "Ultra-low-voltage Versatile LED Flasher":
Items relevant to "El Cheapo Modules from Asia - Part 4":
Items relevant to "High Power DC Motor Speed Control – Part 2":
Items relevant to "New SC200 Audio Amplifier – Part 2":
Purchase a printed copy of this issue for $10.00. |
Measuring
Temperature and
Relative Humidity
U s in g
Ch e a p
A s ian
El e c t
r
M o d u o nic
l
Par t 4e s
The AM2302/DHT22 digital temperature and
relative humidity (RH) sensing module provides
about the simplest way to make a microcontroller
project with temperature and RH sensing
capabilities.
by JIM ROWE
L
ow-cost modules capable of sensing and measuring both temperature and relative humidity (RH) have
been available for a few years now.
Initially these modules appeared
as peripherals for Arduino and similar microcomputers but they soon became an almost standard add-on for
just about any micro-based project.
How humidity is measured
Relative humidity is the ratio of the
amount of water vapour per volume of
air at a particular temperature to the
maximum amount of water which can
be contained by that volume of air at
that same temperature without condensation.
Another way to state this is that
RH is approximately the ratio of the
actual vapour pressure to the saturation vapour pressure. The saturation
vapour pressure depends on the dew
point temperature, which is the highest temperature for a given humidity
level at which water vapour will condense and form dew.
This means that RH depends on
three factors: the amount of water vapour in the air, air temperature and
atmospheric pressure at the time of
measurement.
Since the module described here
measures both RH and temperature,
if you assume a fixed barometric pressure (eg, at sea level it is typically
close to 1 bar), you can compute the
absolute humidity based on these
two readings.
Just about all of these temperature/
RH sensing modules are based on
integrated digital sensors made by
Chinese firm Aosong Electronics
(based in Guangzhou), which also goes
Fig.1: close-up of the humidity sensor, showing the two capacitor plates. Note the
darker plate marked with red is much smaller than the gold one underneath.*
Fig.2 (below): complete connection diagram
for the AM2302/DHT22 sensor module. The
4.7kΩ pull-up resistor allows for bidirectional
communication with a single DATA pin.
AM2302/DHT22
RH & TEMPERATURE
SENSOR MODULE
4.7kΩ
100nF
DATA
VCC
GND
46 Silicon Chip
1
2
3
4
VDD
DATA
(NC)
GND
by the name MaxDetect Technology.
What’s inside
Most modules currently available
use their improved AM2302 sensor,
which has alternative names: DHT22
or RHT03.
Aosong/MaxDetect say little about
what's inside the AM2302/DHT22/
RHT03 but mention that it contains
a dedicated 8-bit microcontroller (see
Fig.5), a temperature sensor and one
RH sensor, the latter being based on a
special polymer capacitor.
Curious to know more, I carefully
cut away the slotted upper section
of the plastic device body. All this
achieved was to reveal the two sensors, fitted on the top of a very small
PCB (18 x 14mm) which is potted inside the remaining part of the plastic
body (see photo and Fig.6 at right).
The polymer capacitor humidity sensor (Fig.1) and it works by
measuring the relative change in the
dielectric constant of the capacitor
with varying humidity.
Since the change in value differs
between capacitors, sensor calibration
is required to provide accurate results.
A thermistor provides temperature
sensing. The thermistor used is an
NTC (negative temperature coefficient)
type, made of a conductive material
which decreases in resistance proportionally as the temperature rises.
The microcontroller measures the
RH sensor capacitance and the thermistor resistance, then converts the
siliconchip.com.au
BUSBUS
RELEASED
RELEASED
FOR
FOR
20µ20
s µs
VCC
VCC
CODING
CODING
FORFOR
DATA
DATA
BIT 'BIT
0' '0'
s µs
80µ80
s µs 80µ80
START
START
SIGNAL
SIGNAL
FROM
FROM
MICRO
MICRO
(1ms
(1ms
RECOMMENDED)
RECOMMENDED)
CODING
CODING
FORFOR
DATA
DATA
BIT 'BIT
1' '1'
28µ28
s µs
70µ70
s µs
VCC
VCC
LOGIC
LOGIC
HIGH
HIGH
LOGIC
HIGH
LOGIC
HIGH
LOGIC
LOW
LOGIC
LOW
LOGIC
LOW
LOGIC
LOW
GND
GND
GND
GND
FORMAT
OFOF
START
REQUEST
SIGNAL
FROM
MICRO,
FORMAT
START
REQUEST
SIGNAL
FROM
MICRO,
'OK
WILL
START'
RESPONSE
FROM
AM2302
SENSOR
'OK
WILL
START'
RESPONSE
FROM
AM2302
SENSOR
50µ50
s µs
SENSOR
SENSOR
RESPONSE
RESPONSE
SIGNAL
SIGNAL
50µ50
s µs
DATA
DATA
BITBIT
CODING
CODING
FOR
FOR
'READ'
'READ'
SIGNALS
SIGNALS
FROM
FROM
AM2302
AM2302
Fig.3: to wake the sensor from standby mode, the micro pulls Fig.4: the micro differentiates between what type of bit it
RH HIGH
RH HIGH
BYTE
RH LOW
RHaLOW
BYTE
BYTE
PARITY
BYTE
BYTE
TEMP
TEMP
HIGH
HIGH
BYTE
BYTE
TEMP
TEMP
LOW
LOW
BYTE
BYTE
has
received
based
on
the pulsePARITY
time;
a data bit of value
the DATA line low for a minimum
ofBYTE800µs and
maximum
zero has a pulse time of 78µs while a one has a pulse time
of 20ms. The DATA line then goes high for 20µs. This is
of 120µs.
regarded as a start request sent to the AM2302.
LSB
LSB
MSB
MSB
LSB
LSB
MSB
MSB
LSB
LSB
MSB
MSB
LSB
LSB
MSB
uring range is from -40 to +80°C with capacitor from VCC to ground and a
a resolution of 0.1°C and an accuracy 4.7kΩ pullup resistor between the digiSINGLE
SINGLE
'READ
'READ
FROM
FROM
AM2302'
AM2302'
TRANSACTION
TRANSACTION
DATA
DATA
FORMAT
FORMAT
of ±0.5°C.
The
long-term
RH
stability
tal data bus line and VCC.
is rated as ±0.5% per year.
The reason for that resistor leads us
The device is designed to run from to discuss the way the device commu3.3-5.5V DC, with operation from nicates with an external micro, over
5V recommended. It has a nominal that single-wire bus.
current drain of 1.5mA when measuring, or 50µA when in standby. It How it handles data
needs at least two seconds between
Although it's poorly explained in
measurements.
the AM2302 data sheet, here's the
T h e A M 2 3 0 2 / D H T 2 2 / R H T 0 3 basic idea: when the DATA line is
module itself measures only 25.1 allowed to float at logic high levx 15.1 x 7.7mm, while the PCB for el (pulled high by the 4.7kΩ resisthe most common module using it tor), the sensor effectively sleeps in
measures 39 x 23mm, as shown in standby mode.
our picture.
To wake it up, the external micro
The sensor has four connection pins, must pull the DATA line down to logalthough one is labelled “NC” (no con- ic low for at least 800µs, but no more
nection) in Aosong's data sheet.
than 20ms. In fact, they recommend
As you can see from Fig.2, there's that it be pulled down for 1ms.
very little in a typical sensing modThen the micro should release the
ule apart from the AM2302/DHT22/ DATA line, allowing it to float high
RHT03 device itself.
again for about 20µs.
There are just two passive compoThis “1ms-low-followed-by-20µsnents on the board: a 100nF bypass high” sequence is regarded as the
MSB
LSB
LSB
MSB
MSB
analog readings to digital values.
'OK'OK
WILL
START
START
' '
START
SIGNAL
SIGNAL
ThisSTART
micro
and
aWILL
number
of associRESPONSE
RESPONSE
FROM
FROM
MICRO
MICRO
FROM
FROM
Am2302
Am2302
ated components are mounted on the
underside of the PCB; we can’t determine their exact configuration as
it’s impossible to remove the potting
without destroying most of the circuit.
However, there is a YouTube video
where someone has removed all the
components from the device. Some of
the pictures from that video are shown
in this article, and the link to the video
is at the end of this article.
Aosong/MaxDetect state that every
AM2302 sensor is temperature compensated and calibrated in an accurate
calibration chamber, during or after
which the calibration coefficients are
saved in the micro's one-time programmable memory.
Considering its low price, the
claimed performance of the AM2302
is quite impressive. The RH measuring range is from 0 to 100%, with a
resolution of 0.1% and an accuracy
of ±2%, while the temperature meas-
Fig.5 (above): the internal layout of the micro
in the AM2302 sensor.*
The module in question with the case
still intact. The module has a fairly
low profile, measuring only 7.7mm
high.
siliconchip.com.au
Fig.6 (right): the sensor module with the top
of the case removed. The bead type sensor
is an NTC themistor and to the right is the
capacitive humidity sensor.*
February 2017 47
MSB
LSB
LSB
PARITY BYTE
TEMP LOW BYTE
MSB
MSB
TEMP HIGH BYTE
LSB
MSB
'OK WILL START'
RESPONSE
FROM AM2302
RH LOW BYTE
LSB
START SIGNAL
FROM MICRO
MSB
RH HIGH BYTE
50µs
DATA BIT CODING FOR 'READ' SIGNALS FROM Am2302
LSB
FORMAT OF START REQUEST SIGNAL FROM MICRO,
'OK WILL START' RESPONSE FROM AM2302 SENSOR
50µs
SENSOR
RESPONSE
SIGNAL
SINGLE 'READ FROM AM2302' TRANSACTION DATA FORMAT
Fig.7: once there has been a start response from the sensor, the AM2302 sends out its measurement data in 40 bit sets. The
first 16 bits is the relative humidity, the 16 bits after is the temperature and the final 8 bits are parity bits to pad the length
of the data to 40 bits total.
micro sending a start request signal to
the AM2302.
If the AM2302 responds to this
wake up call, it pulls the DATA line
down to logic low for 80µs, and then
allows it to float high again for another 80µs.
This is regarded as its “OK, will
start” response. This “start request”
and “OK will start” sequence is shown
in Fig.3.
Soon after this startup sequence, the
AM2302 sends out its current measurement data as a sequence of 40 bits
of data, grouped in five bytes as shown
in Fig.7.
The relative humidity reading is in
the first two bytes (RH HIGH and RH
LOW), followed by the temperature
reading in the next two bytes (TEMP
HIGH and TEMP LOW), and finally
there's a checksum or parity byte to
allow error checking.
All of these bytes are sent MSB (most
significant bit) first and LSB (least significant bit) last.
It's also worth noting that both the
RH and temperature readings have a
resolution of 16 bits.
While this single-wire-bus transaction may look fairly straightforward,
it isn't quite that simple – because of
the special encoding that Aosong uses
for the data bits themselves.
As shown in Fig.4, a binary zero is
coded as a logic low of 50µs followed
by a logic high of 28µs, whereas a binary one is coded as the same logic
low of 50µs, but followed by a logic
high of 70µs.
So both a zero and a one begin with
a logic low lasting for 50µs but a logic
high that follows lasts for only 28µs in
the case of a zero rather than 70µs in
the case of a one.
As a consequence, data bits with a
value of 0 last for a total of 78µs, while
those with a value of 1 last for 120µs.
So the time taken by each of those
data bytes as shown in Fig.7 will not
48 Silicon Chip
be fixed but will vary, depending on
the data bit values.
For example, a byte consisting
of all zeroes (00000000) will last
for only 624µs, while a byte of all
ones (11111111) will last for 960µs.
So in practice, the duration of each
data byte will vary between 624 and
960µs.
The micro connected to the AM2302
needs to take this rather unusual
coding system into account when it
decodes RH and temperature data.
How it's used
You shouldn't have to worry about
decoding the AM2302 measurement
data yourself, because many people
have already worked it out for most of
the popular microcomputers.
For example, if you want to hook
up an AM2302-based module to a
Maximite or Minimite, Geoff Graham
has already solved this problem and
provided a special command in his
MMBasic programming language. It
looks like this:
HUMID pin, tVar, hVar
Where HUMID is the command keyword and “pin” is the micro's I/O pin
to which the module's DATA line is
connected.
“tVar” is the name of the floating-point variable you want to receive the returned temperature (in
°C) and “hVar” is the name of a second floating-point variable to receive
the returned relative humidity (as a
percentage). It's that easy!
If you're running the module from a
5V supply, you do have to make sure
that you connect the module's DATA
line to a Micromite pin that is 5V
tolerant – ie, one of pins 14 to 18, 21
or 22 on the 28-pin Micromite.
So if you have connected the
module's DATA line to pin 18 of the
Micromite and have declared the temperature and RH variables as say temp!
and RH! respectively, you'll be able to
read the sensor's data with this oneline command:
HUMID 18, temp!, RH!
If you want to take a sequence of
say 10 readings spaced apart by the
recommended minimum of two seconds and print them to the console,
here's the kind of simple program
you'll need:
DIM nbr% = 10
DIM temp! = 0.0
DIM RH! = 0.0
PAUSE 1000
DO
HUMID 18, temp!, RH!
PRINT "Temperature = "temp!
"C & humidity = " RH! "%"
nbr% = nbr% - 1
PAUSE 2000
LOOP UNTIL nbr% = 0
If you want to hook up an AM2302based module to any of the Arduino
versions, it's almost as easy. You have
quite a choice when it comes to prewritten applications, some of which
you'll find using these links:
https://github.com/RobTillaart/
Arduino/tree/master/libraries/DHTlib
https://github.com/nethoncho/
Arduino-DHT22
https://github.com/sparkfun/
SparkFun_RHT03_Particle_Library/
blob/master/firmware/examples/
RHT03-Example-Serial.ino
There are also sample programs on
both of these websites:
www.aosong.com
www.humidity.com
So it's not at all difficult to use one
of these low cost AM2302/DHT22/
RHT03 based modules with a readily
available microcomputer.
* these pictures have been taken
from the video at: http://youtu.be/
C7uS1OJccKI by www.youtube.com/
SC
user/electronupdate
siliconchip.com.au
|