Monday, 28 November 2016

DHT22 with sound alert Arduino

After testing Dht11 temperature and humidity sensor I thought why not try with the next one which is high quality and can get the measurement more precisely. And that is DHT22/AM2302 Temperature Humidity Sensor. Let me give you some specification of it then I'll go for the sketch and at end give you some explanations.


Specification of DHT22/AM2302:
  • Type: AM2302
  • Accuracy resolution: 0.1
  • Humidity range: 0-100%RH
  • Temperature range: -40~80℃
  • Humidity measurement precision: ±2%RH
  • Temperature measurement precision: ±0.5℃
  • 4-pin package
  • Ultra-low power
  • No additional components
  • Excellent long-term stability
  • All calibration, digital output
  • Completely interchangeable
  • Long distance signal transmission
  • Relative humidity and temperature measurement
What does the sketch do?
If you can successfully upload the sketch to your Arduino broad then it will measure the temperature and humidity. And on the certain degree (which ever you set) it will turn on the LEDs and at same time it will give you a sound alert (what ever audio file you've saved) so that you can hear without looking at it.

The sketch

#include <SoftwareSerial.h>
#define ARDUINO_RX 10 //should connect to TX of the Serial MP3 Player module
#define ARDUINO_TX 9 //connect to RX of the module
SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);
static int8_t Send_buf[8] = {0} ;

#define CMD_SEL_DEV 0X09
#define DEV_TF 0X02
#define CMD_PLAY_W_VOL 0X22
#include <dht.h>
dht DHT;
#define DHT22_PIN 2
int led1 = 3;
int led2 = 4;

void setup()
  Serial.println("DHT TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");

pinMode (led1,OUTPUT);
pinMode (led2,OUTPUT);
delay(500);//Wait chip initialization is complete
        sendCommand(CMD_SEL_DEV, DEV_TF);//select the TF card
delay(200);//wait for 200ms

void loop()
  Serial.print("DHT22, \t");
  int chk = DHT.read22(DHT22_PIN);
  switch (chk)
    case DHTLIB_OK:
        Serial.print("Checksum error,\t");
        Serial.print("Time out error,\t");
        Serial.print("Unknown error,\t");
  Serial.print(DHT.humidity, 1);
  Serial.println(DHT.temperature, 1);

if (DHT.humidity >60){
   digitalWrite (led1, HIGH);
   sendCommand(CMD_PLAY_W_VOL, 0X1E002); //play the sound track
   delay(4000); // length of the sound track
 } else {
      digitalWrite (led1, LOW);

  if (DHT.temperature >24){
   digitalWrite (led2, HIGH);
   sendCommand(CMD_PLAY_W_VOL, 0X1E001); //play the sound track
   delay(4000); // length of the sound track
 } else {
      digitalWrite (led2, LOW);

  delay(1000); //set how much time you want between readings

void sendCommand(int8_t command, int16_t dat)
  Send_buf[0] = 0x7e;
  Send_buf[1] = 0xff;
  Send_buf[2] = 0x06;
  Send_buf[3] = command;
  Send_buf[4] = 0x00;//0x00 = no feedback, 0x01 = feedback
  Send_buf[5] = (int8_t)(dat >> 8);//datah
  Send_buf[6] = (int8_t)(dat); //datal
  Send_buf[7] = 0xef; //ending byte
  for(uint8_t i=0; i<8; i++)//
    mySerial.write(Send_buf[i]) ;

You may need few files in the Arduino library so that this sketch can work properly. You can get it from Once you go there you will find a sketch and one dht.h and dht.cpp grab those two files and put it in a folder and call that folder DHT and then put that folder in the Arduno libraries folder. Anyways, this post is combination of my other two other posts DHT11 and Catalex MP3 player  if you find this post hard to understand then please have a look at those two posts first I've explained it in detail.

Things you need:
  • 2x LEDs
  • Catalex mp3 player
  • 1x 10k ohm resistor
  • 1x speaker
  • Adruino board
  • 1x DHT22
  • 1x micro sd card with sound files named 001and 002
That's all I think, I hope it helps you. And special thanks to for providing me DHT22 and they have got so many other interesting things about Arduino. If are interested then please have a look at their site you might as well like it.

Temperature And Humidity, Arduino Humidity, digital temperature humidity sensor, humidity and temperature sensor. 

No comments:

Post a Comment