728x90 AdSpace

­
Latest News
Sunday, February 1, 2015

Measuring temperature and humidity












Being able to sense the physical environment around your Arduino can be the source of lots of great project ideas. Last time, we looked at how to measure distances using an ultrasonic sensor and now we’ll look at how to use a temperature and humidity sensor.
We’re using a low-cost option that’s easy to plug in and can be the basis of a range of projects including as an auto-activated fan, a weatherstation or an automatic garden watering system.

How they work

The sensor is a simple four-pin block that not only contains temperature and humidity measuring elements, but also a simple microcontroller chip that feeds out the data onto one of sensor pins, which we can read using an Arduino Uno or Freetronics Eleven board.
dht11There are two versions of these on the market – the blue DHT11 and the white DHT22. The DHT22 has higher accuracy, the DHT11 is cheaper – we’ll be using the DHT11 but they essentially work the same way (we’ll point out the differences as we go).
The DHT11 has four pins and you must connect it up the right way, otherwise you’ll blow it up – but don’t worry, it’s easy to work out. With the DHT11 label facing away from you, pin 1 is on the left, pin 4 on the right. Pin 1 is Vcc (or 5V power), pin 2 is ‘data’, pin 3 is not connected (N/C) and pin 4 is ground (GND, or 0V). Some sensor ‘modules’ on the market drop pin 3 altogether.

Quick connect

But we’re going to use the cheat’s way of connecting it up to our Arduino board – this technique won’t work with every device, but it works here and we’ll explain why. The DHT11 is a very low-power device – it consumes no more than 2.5-milliamps (2.5mA) of current. The digital I/O (input/output) ports of an Arduino can supply up to 40mA, so if we set the Arduino’s D3 pin high (that is, to 5V) and the D6 pin to GND (0V), we can power a device between those two pins, up to 40mA. We’ve just said the DHT11 only needs 2.5mA, so the D3/D6 pins are more than capable of powering our sensor.
unoThe DHT11 can measure temperature from 0 to 50-degreesC and 20-90% relative humidity (the DHT22, also called the AM2302, can go from -40 to 80-degreesC and 0-100% relative humidity). But to read the data from sensor’s Pin 2, we need to apply a five-kiloohm (5kΩ) resistor connecting that pin to the 5V supply line or ‘rail’ – in this instance, it’s called a ‘pull-up resistor’ because it’s ‘pulling up’ the sensor’s Pin 2 to the supply rail.
But this is where we’ll use another hidden trick – the Arduino Uno’s I/O pins all have built-in pull-up resistors and we activate the one on Pin D4 using one line of code:
pinMode(4, INPUT_PULLUP);
That tells the Arduino we want Pin D4 as an input (so we can read data from the sensor) and to activate its internal pull-up resistor. Pretty sneaky, huh? The upshot of all this is we’ve made it incredibly easy to install the DHT11 into the Arduino – all we need to do is just plug it in! With the DHT11 label facing in, it plugs into pins D3 to D6.

Software sketch

outputThat’s the hardware done, now for the software! You’ll need the Arduino integrated development environment (IDE), which you can download free fromhttp://arduino.cc/en/main/software. For an Arduino Uno/Freetronics Eleven board, you just need the latest version 1.0.6. Next, grab the zip file from our webpage atwww.apcmag.com/magstuff. Download it, unzip it and copy the contents of the libraries subfolder into the libraries folder of the Arduino IDE. Restart the IDE and load in the ab02_temphumid_v1.ino sketch file. Remove the DHT11 sensor if you have it plugged into the Arduino board, then plug the board into your computer via the USB cable. (If using a Freetronics Eleven board, get the driver software first fromhttp://tinyurl.com/elevenusb.)
In the IDE menu, select ‘Tools’, ‘Board’, choose ‘Arduino Uno’, then select ‘Tools’ again, but this time, go ‘Serial Port’ and select the COM port for your board (it won’t likely be COM1 – that’s your mouse!).

Flashing your Arduino

When that’s done, click on the right arrow icon, second from the left and the code will be compiled and flashed to your Arduino board. When you get the ‘upload completed’ sign in the bottom status window, unplug the Arduino board, plug in the DHT11 sensor into pins D3 to D6 with the label facing in.
Plug the USB cable back in, check that the COM port is still selected, then press the Serial Monitor icon on the top right of the IDE – this will launch the Serial Monitor window. On the bottom right of the new window, make sure the baud rate is set to ‘57600’. Your Arduino board will automatically restart and within a second or two, you should start seeing output. If you see ‘=== sensor ok’ and readings appear, congrats – you’re up and running!
altsensor

How Arduino code works

We talked about this last time, but remember, an Arduino ‘sketch’ code has to have a minimum of two main code sections or ‘methods’ – setup() and loop(). The setup() method runs first and runs once; the loop() method takes over and loops continuously happily ever after (or until power is removed).

DHTLIB library

We’re standing on the shoulders of those who’ve gone before us by using the DHTLIB code library by Rob Tillaart (that’s what you’ve copied to your libraries subfolder). It makes it coding the sensor far easier. If you look at the code, you’ll see we get the temperature and humidity readings from two code lines: DHTtemp = DHT.temperature and DHThumid = DHT.humidity. The ‘DHT’ is the object and ‘temperature’ and ‘humidity’ are functions in that object. If you’re using the DHT22 sensor, change the DHT.read11(DHTdata) code line to DHT.read22(DHTdata).
You can read more info here.

Try it yourself

The DHT11 sensor can be had from eBay for under $2 including shipping and it’s a great way to add in environmental feedback into your projects in a low-cost, low-power way. And thanks to that free library, it’s also extremely easy to code
  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment

Item Reviewed: Measuring temperature and humidity Rating: 5 Reviewed By: Unknown