Thursday, 12 February 2015

Controlling stuff with dht11 arduino



This is a small sensor which measures humidity and temperature, when you setup this with Arduino you can control heaters, fans or what what ever you want, in this sketch I've used leds but if you replace the leds with relays you can easily control electrical stuffs. Let's see the sketch then I'll explain bit more about the sketch.

Note: You might need to install dht11 library so that this sketch can work properly. You can get it from http://playground.arduino.cc/main/DHT11Lib. When you go to the site to get the files you will find 4 things. The sketch, dht11.h file, dht11.cpp and test_dewpoint.ino but all the files are in text form and in one page so you will need to make them in different files and to do that you might need to get help from this post.

The sketch
#include <dht11.h>

dht11 DHT11;

#define DHT11PIN 2
int led = 3;
int led2 = 4;

void setup()
{
  Serial.begin(9600);
  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);
  Serial.println();
 
pinMode (led,OUTPUT); 
pinMode (led2,OUTPUT); 
}

void loop()
{
  Serial.println("\n");

  int chk = DHT11.read(DHT11PIN);

  Serial.print("Read sensor: ");
  switch (chk)
  {
    case DHTLIB_OK:
        Serial.println("OK");
        break;
    case DHTLIB_ERROR_CHECKSUM:
        Serial.println("Checksum error");
        break;
    case DHTLIB_ERROR_TIMEOUT:
        Serial.println("Time out error");
        break;
    default:
        Serial.println("Unknown error");
        break;
  }

  Serial.print("Humidity (%):  ");
  Serial.println((float)DHT11.humidity, 2);

  Serial.print("Temperature (*C):  ");
  Serial.println((float)DHT11.temperature, 2);


 if (DHT11.humidity >60){
   digitalWrite (led, HIGH);
 } else {
      digitalWrite (led, LOW);
 }

  if (DHT11.temperature >30){
   digitalWrite (led2, HIGH);
 } else {
      digitalWrite (led2, LOW);
 }

   delay(3000);
}


 * The light blue highlighted arrow > you can see after humidity and temperature that means if the humidity or temperature above then 60 or 30 it will turn the led 1 and 2 on otherwise off, and if you put the left arrow < that means if humidity or temperature lower then the number you put after the arrow then it turn the led 1 and 2 on otherwise off.

* "delay(3000)" As you can see delay 3000 that means it will take a measurement after every 3 seconds.

DHT22 with sound alert
Ad


3 comments:

  1. Arduino:1.6.12 Hourly Build 2016/09/14 08:20 (Windows 7), Scheda:"Arduino/Genuino Uno"

    C:\Users\Ciccio\AppData\Local\Temp\arduino_modified_sketch_649797\sketch_dec09a.ino:1:19: fatal error: dht11.h: No such file or directory

    #include

    ^

    compilation terminated.

    exit status 1
    Errore durante la compilazione per la scheda Arduino/Genuino Uno.

    Questo report potrebbe essere più ricco di informazioni con l'opzione
    "Mostra un output dettagliato durante la compilazione"
    abilitata in File -> Impostazioni

    ReplyDelete
  2. Do you have dht11 library in zip format? You give me the link. THANK YOU!

    ReplyDelete
    Replies
    1. Please read my post How to create a .h file for Arduino library and then visit this link Arduino Playground - DHT11Lib to get the code to make the files dht11.cpp and dht11.h you need.

      Delete