Pages: [1]   Go Down
Author Topic: DHT11 sensor logging ?  (Read 1720 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Guys
    I am a newbe and have been working on a sketch to display and log temperature and humidity using a dht11 sensor and a ii2c display.  I have got to the point where the Arduino UNO sees that SD card and shows up on the serial monitor but I haven't figured out how to get the readings to store on the SD card.  I would like to be able to store the temperature and humidity at something like every 10 miuutes.  I have included the sketch of the LCD and the DHT11 sensor sketch that works so far.  I tried combining the sketch that reads and write on the SD card but haven't had any luck.  Can someone please give me some help ?

                               Thanks GD

Code:
#include <Wire.h>
#include <dht11.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 20 chars and 4 line display


/*-----( Declare objects )-----*/
dht11 DHT11; //5 volts, Grnd, Pin 2

/*-----( Declare Constants, Pin Numbers )-----*/
#define DHT11PIN 2

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  lcd.init();                      // initialize the lcd
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(3,0);
  

  Serial.begin(57600);
  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);
  Serial.println();
}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  Serial.println("\n");

  int chk = DHT11.read(DHT11PIN);

  Serial.print("Read sensor: ");
  switch (chk)
  {
    case 0: Serial.println("OK"); break;
    case -1: Serial.println("Checksum error"); break;
    case -2: Serial.println("Time out error"); break;
    default: Serial.println("Unknown error"); break;
  }

  Serial.print("Temperature (F): ");
  Serial.println(Fahrenheit(DHT11.temperature), 2);

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

  Serial.print("Dew Point (C): ");
  Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));

  Serial.print("Dew PointFast (C): ");
  Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
  
  lcd.setCursor(3,0);
  lcd.print("Gary   Davis!");
  
  lcd.setCursor(3,1);
  lcd.print("Temp F:");
  lcd.println(Fahrenheit(DHT11.temperature));
  
  
   lcd.setCursor(1,2);
   lcd.print("Humidity: ");
   lcd.println((float)DHT11.humidity, 2);
   lcd.setCursor(0,3);
   lcd.print("Dew Point:C ");
   lcd.println(dewPoint (DHT11.temperature, (DHT11.humidity)));
   //lcd.println(dewPoint(DHT11.temperature, DHT11.humidity));
    
  delay(10000);
}/* --(end main loop )-- */

/*-----( Declare User-written Functions )-----*/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
        return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
        return celsius + 273.15;
}

// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm
double dewPoint(double celsius, double humidity)
{
        double A0= 373.15/(273.15 + celsius);
        double SUM = -7.90298 * (A0-1);
        SUM += 5.02808 * log10(A0);
        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
        SUM += log10(1013.246);
        double VP = pow(10, SUM-3) * humidity;
        double T = log(VP/0.61078);   // temp var
        return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
        double a = 17.271;
        double b = 237.7;
        double temp = (a * celsius) / (b + celsius) + log(humidity/100);
        double Td = (b * temp) / (a - temp);
        return Td;
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 208
Posts: 8855
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should probably look at File->Examples->SD->Datalogger
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi John
    I looked at the Datalogger and tried to put the DHT11 sensor that I am using into it and wasn't able to compile it.  Is there a chance to get some help please ?  I have included the sketch as far as I have got it so far.

                              Thanks
                                GD

Code:
  /*
    SD card datalogger
   
   This example shows how to log data from three analog sensors
   to an SD card using the SD library.
   
   The circuit:
   * analog sensors on analog ins 0, 1, and 2
   * SD card attached to SPI bus as follows:
   ** MOSI - pin 11
   ** MISO - pin 12
   ** CLK - pin 13
   ** CS - pin 4
   
   created  24 Nov 2010
   modified 9 Apr 2012
   by Tom Igoe
   
   This example code is in the public domain.
   
   */
 
  #include <SPI.h>
  #include <SD.h>
  #include <Wire.h>
  #include <dht11.h> /*-----( Import needed libraries )-----*/
 
  // On the Ethernet Shield, CS is pin 4. Note that even if it's not
  // used as the CS pin, the hardware CS pin (10 on most Arduino boards,
  // 53 on the Mega) must be left as an output or the SD library
  // functions will not work.
  const int chipSelect = 10;
 
 
  dht11 DHT11;  /*-----( Declare objects )-----*/
 
  #define DHT11PIN 2  /*-----( Declare Constants, Pin Numbers )-----*/
 
  void setup()  /*----( SETUP: RUNS ONCE )----*/
  {
   // Open serial communications and wait for port to open:
    Serial.begin(9600);
     while (!Serial) {
      ; // wait for serial port to connect. Needed for Leonardo only
      Serial.println("DHT11 TEST PROGRAM ");
    Serial.print("LIBRARY VERSION: ");
    Serial.println(DHT11LIB_VERSION);
    Serial.println();
  }/*--(end setup )---*/
   
 
 
    Serial.print("Initializing SD card...");
   
   
   
    // make sure that the default chip select pin is set to
    // output, even if you don't use it:
    pinMode(10, OUTPUT);
   
    // see if the card is present and can be initialized:
    if (!SD.begin(chipSelect)) {
      Serial.println("Card failed, or not present");
      // don't do anything more:
      return;
    }
    Serial.println("card initialized.");
  }
 
  void loop()
  {
    Serial.println("\n");
   
    int chk = DHT11.read(DHT11PIN);
    // make a string for assembling the data to log:
    String dataString = "";
 
    Serial.print("Read sensor: ");
    switch (chk)
    {
      case 0: Serial.println("OK"); break;
      case -1: Serial.println("Checksum error"); break;
      case -2: Serial.println("Time out error"); break;
      default: Serial.println("Unknown error"); break;
    }
   
    Serial.print("Temperature (F): ");
    Serial.println(Fahrenheit(DHT11.temperature), 2);
   
    Serial.print("Temperature (oC): ");
    Serial.println((float)DHT11.temperature, 2);
   
    Serial.print("Humidity (%): ");
    Serial.println((float)DHT11.humidity, 2);
 
    Serial.print("Temperature (K): ");
    Serial.println(Kelvin(DHT11.temperature), 2);
 
    Serial.print("Dew Point (oC): ");
    Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));
 
    Serial.print("Dew PointFast (oC): ");
    Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
    delay(2000);
    }
 
    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.
   
    File dataFile = SD.open("datalog.txt", FILE_WRITE);

    // if the file is available, write to it:
      if (dataFile)
      dataFile.println(dataString);
      dataFile.close();
      // print to the serial port too:
      Serial.println(dataString);
     
    // if the file isn't open, pop up an error:
     else {
      Serial.println("error opening datalog.txt");
    }
  }
 
  /*-----( Declare User-written Functions )-----*/
  //
  //Celsius to Fahrenheit conversion
  double Fahrenheit(double celsius)
  {
          return 1.8 * celsius + 32;
  }
 
  //Celsius to Kelvin conversion
  double Kelvin(double celsius)
  {
          return celsius + 273.15;
  }
 
  // dewPoint function NOAA
  // reference: http://wahiduddin.net/calc/density_algorithms.htm
  double dewPoint(double celsius, double humidity)
  {
          double A0= 373.15/(273.15 + celsius);
          double SUM = -7.90298 * (A0-1);
          SUM += 5.02808 * log10(A0);
          SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
          SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
          SUM += log10(1013.246);
          double VP = pow(10, SUM-3) * humidity;
          double T = log(VP/0.61078);   // temp var
          return (241.88 * T) / (17.558-T);
  }
 
  // delta max = 0.6544 wrt dewPoint()
  // 5x faster than dewPoint()
  // reference: http://en.wikipedia.org/wiki/Dew_point
  double dewPointFast(double celsius, double humidity)
  {
          double a = 17.271;
          double b = 237.7;
          double temp = (a * celsius) / (b + celsius) + log(humidity/100);
          double Td = (b * temp) / (a - temp);
          return Td;
  }
 
  /* ( THE END ) */
 
 
 
 
 
 
Logged

Miramar Beach, Florida
Online Online
Faraday Member
**
Karma: 146
Posts: 6027
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The datalogger example included with the IDE crashes with IDE v1.0.3. It is due to the String data type. You should read this:
http://arduino.cc/forum/index.php/topic,151849.0.html
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi SurferTim
      I read that and tried to run the sketch and get a compiler error.

                             GD
Logged

Miramar Beach, Florida
Online Online
Faraday Member
**
Karma: 146
Posts: 6027
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you use my code in reply #5? It compiles for me using IDE v1.0.4. I just checked it.
http://arduino.cc/forum/index.php/topic,151849.msg1144214.html#msg1144214
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi SurferTim

         That is the sketch that I tried to run and got a compiler error but I am using IDE 1.5.2 so that might be my problem.  I'll try downloading the earlier version and see if it compiles then.

                                     Thanks
                                        GD
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi SurferTim
      I am able to access the SD card and write to it and read it with the example sketches but I haven't had any luck logging the information from my DHT11 sensor.

                                       Thanks GD
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

  I had a thought that part of my problem might be that I don't have the correct files in my library. I know that when I was trying to get my II2C display working I had this issue.  If that is one of my problems how can I tell ?  I copied the sketch that SuferTim mentioned and I  get a compiler error on a sketch that works for other people.

                                       Gary
Logged

Pages: [1]   Go Up
Jump to: