Friday, 1 August 2014

Arduino pocket hygrometer/thermometer using DHT22 (AM2302)

As I promised this post is about my own Arduino project. I call it Arduino but actually it's not. I mean, I used my Homemade Arduino board only to write sketch and debug. All parts I've mounted on PCB and placed in small plastic enclosure. That's how it looks









Features 

-measures humidity (0 - 100 RH%  +-2 %)
-measures temperature (-40 to +80 Celsius +-0.2C)
-it's small (can fit in any pocket)
-easy to operate (only one button)
-has backlight (don't have to switch on the lights to see measurements)
-indicates battery power balance in percentage
-turns off itself after 18 seconds (prevents battery discharge)

You might ask, why do I need such device at home or office? Well, Here's my story.
In winter (when heating is on) or in summer (when using air conditioning) the air in the room is very dry. And it's getting hard to breathe especially for little children. So, I and my wife have decided to buy the humidifier. It really helped! But I became wondering, how long should I humidify the air to reach about 50 to 70% (normal humidity)? After all, to much is not good for health too. That's when hygrometer comes to help! I've decided to make one myself. After hours of searching in the net I didn't find any suitable projects. Some were too complicated, some too expensive... So, I've came up with this device. Here is the schematic diagram
and PCB (downloadable)
and sketch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <LiquidCrystal.h> //lcd library
#include "DHT.h"  //sensor library
#define DHTPIN 2   //  pin 32 (TQFP-32)
#define DHTTYPE DHT22   // type of sensor DHT 22  (AM2302)
byte degreeChar[8] =
{ 
  B01100,
  B10010,
  B10010,
  B01100,
  B00000,
  B00000,
  B00000,
  B00000, };
DHT dht(DHTPIN, DHTTYPE);
//LCD-rs,e,d4,d5,d6,d7 (TQFP-9,10,11,12,13,14)
LiquidCrystal lcd(5,6,7,8,9,10);
unsigned int bat = 0; // ADC value
byte volt = 0;  // battery %

void setup() {
  lcd.begin(8, 2);
  analogReference(INTERNAL);
  pinMode(0, OUTPUT);
  digitalWrite(0, HIGH); // power on
  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH); // backlight on
  dht.begin(); //  start sensor
  lcd.setCursor(0,0);
  lcd.print("Battery");
  for(byte i=0; i<=10; i++) 
  {
    bat=analogRead(1);//battery sensor pin 17 (TQFP-32)
    if (bat<=800)
    {  
      bat = 800;  
    }
    if (bat>=900)
    {  
      bat = 900;  
    }
    volt = bat-800;
    lcd.setCursor(0,1);
    lcd.print(volt);
    lcd.print("%");
    delay(200); 
  }
  ADCSRA = 0; // Disable ADC
}
void loop()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(t) || isnan(h)) {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("SENSOR");   
    lcd.setCursor(0,1);
    lcd.print("ERROR");
  }
  else {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("T");
    lcd.print(t);
    lcd.createChar(0, degreeChar);
    lcd.setCursor(6,0);
    lcd.write(byte(0));
    lcd.print("C");
    lcd.setCursor(0,1);
    lcd.print("H");
    lcd.print(h);
    lcd.print(" %");
  }
  delay(2000);
  if (millis()>=8000)
  {   
    digitalWrite(13, LOW);  
  } // backlight off
  if (millis()>=16000)
  {   
    digitalWrite(0, LOW);  
  } // power off
}

But first you have to dowload the library for the sensor here https://github.com/adafruit/DHT-sensor-library
Then go to folder ...\arduino-1.0.5\libraries and create tere folder "DHTsensor" and copy files "DHT.cpp", "DHT.h", "README.txt" into it. Run Arduino IDE and find the library like this


About sensor you can read here https://learn.adafruit.com/dht
Now you can make your own hygrometer.

How to use it

Just press the button! DHT22 needs to warm up for 2 seconds, during that time Battery power balance will be indicated. After 8 seconds backlight will switch off and after 8 seconds more the device will shut down. Simple, isn't it! Well, that was the idea!

Caution

Do not try to use it right after you take it out of pocket! The sensor is a little bit slow, and hygrometer will indicate humidity and temperature that's in your pocket. Just put it on the table and wait a few minutes, then go ahead.

P.S. It would be nice if you let me know if this post was helpful or if you had some kind of trouble making your device. I'll do my best to help you! Next I'll tell you how to upload sketches into controller in TQFP32 package.