There are many types of sensors available for different measurements. Today we gonna use humidity and temperature (bonus feature) sensor DHT22 aka AM2302.
Sensor has following key features:
- 3~6V Power and I/O Voltages
- Signal type: digital
- Humidity: 0..100% RH (±2%)
- Temperature: -40..80°C (±0.5°C)
- Sensing period: ~2s
So it’s very convenient to use with MCU due to digital data protocol, suitable voltages. I’ve baught my sensor in dx.com site for ~10$. This version already contains some auxiliary parts such as 4-wire cable and pull-up resistor, soldered on PCB.
Accordingly to datasheet sensor has digital communication protocol with bits encoded as pulses lengths. You can read more information about all communication details here. We have two common approaches to implement MCU part of communication process. One way for implementation is using plain cycle delays when communicating with sensor. That way sensor utilizes MCU time into nothing, bad to say. Second approach uses timer modules in MCU. Thus, controller should be in standby/inactive mode for needed delays or do other things in the same time (except some interruptions, of course, which leads to overlaps with DHT22 interrupts).MSP430 also has timers support. I use MSP430G2553 chip with two timers (Timer0 and Timer1) and different work modes. I wrote my own little project to control L293D motor driver and read DHT22 sensor periodically by timer (project uses second Timer-based approach). You can get sources in github. Note that I haven’t implemented standby mode in that project, but it’s very easy modification.
Code contains comments and tested with real sensor and environment changes.