Humidity Sensor Datalogger

In the current code Move

float t = DHT.temperature; // Gets the values of the temperature
float h = DHT.humidity; // Gets the values of the humidity

After the read22 otherwise the values are not up to date

If you want to use the other library, install it in the arduino IDE first :slight_smile:

where would i be checking readData from?

You would have to modify a bit your code, add the include for the right library

#include "DHT.h"
#define DHTPIN 8     // what digital pin we're connected to
#define DHTTYPE DHT22

Create your object to handle your sensors as a global

Create the DHT sensor.

DHT dht(DHTPIN, DHTTYPE);

And initialize your object in the setup()

void setup() {
  Serial.begin(115200);
  // ... Your other initialization
  dht.begin(); // get it going
}

Then in the loop you keep,globally the same structure but when it's time to read you do something like

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a  slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

  // Check if any reads failed
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
  } else {
    // all ok. Put your code here to write the SD etc

  }

J-M-L:
In the current code Move

float t = DHT.temperature; // Gets the values of the temperature
float h = DHT.humidity; // Gets the values of the humidity

After the read22 otherwise the values are not up to date

If you want to use the other library, install it in the arduino IDE first :slight_smile:

You would have to modify a bit your code, add the include for the right library

#include "DHT.h"

#define DHTPIN 8    // what digital pin we're connected to
#define DHTTYPE DHT22



Create your object to handle your sensors as a global 

Create the DHT sensor.


DHT dht(DHTPIN, DHTTYPE);




And initialize your object in the setup()



void setup() {
  Serial.begin(115200);
  // ... Your other initialization
  dht.begin(); // get it going
}




Then in the loop you keep,globally the same structure but when it's time to read you do something like



// Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a  slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

// Check if any reads failed
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
  } else {
    // all ok. Put your code here to write the SD etc

}

Thank you for explaining the above - things are pretty busy with work so i will give this a go when i get chance.

Thanks again!

J-M-L:
In the current code Move

float t = DHT.temperature; // Gets the values of the temperature
float h = DHT.humidity; // Gets the values of the humidity

After the read22 otherwise the values are not up to date

If you want to use the other library, install it in the arduino IDE first :slight_smile:

You would have to modify a bit your code, add the include for the right library

#include "DHT.h"

#define DHTPIN 8    // what digital pin we're connected to
#define DHTTYPE DHT22



Create your object to handle your sensors as a global 

Create the DHT sensor.


DHT dht(DHTPIN, DHTTYPE);




And initialize your object in the setup()



void setup() {
  Serial.begin(115200);
  // ... Your other initialization
  dht.begin(); // get it going
}




Then in the loop you keep,globally the same structure but when it's time to read you do something like



// Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a  slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

// Check if any reads failed
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
  } else {
    // all ok. Put your code here to write the SD etc

}

Apologies for the delayed response, ive not had much time to get back into this.

So i have moved to the Library you recommended and made a few tweaks to my code wit your help!

My next question is do i still need this snippet of code with the new library;

  if (millis() > (lastUpdate + refresh_rate))

by the looks of things though it seems to be logging perfectly since the new library..

(i will follow this up with my finished code once I've tidied things a little)

Thank again! :slight_smile:

J-M-L:
In the current code Move

float t = DHT.temperature; // Gets the values of the temperature
float h = DHT.humidity; // Gets the values of the humidity

After the read22 otherwise the values are not up to date

If you want to use the other library, install it in the arduino IDE first :slight_smile:

You would have to modify a bit your code, add the include for the right library

#include "DHT.h"

#define DHTPIN 8    // what digital pin we're connected to
#define DHTTYPE DHT22



Create your object to handle your sensors as a global 

Create the DHT sensor.


DHT dht(DHTPIN, DHTTYPE);




And initialize your object in the setup()



void setup() {
  Serial.begin(115200);
  // ... Your other initialization
  dht.begin(); // get it going
}




Then in the loop you keep,globally the same structure but when it's time to read you do something like



// Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a  slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

// Check if any reads failed
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
  } else {
    // all ok. Put your code here to write the SD etc

}

As mentioned;
my code so far:

 //Humidity Sensor-Arduino:  (Left to Right) 1-5v, 2-Data (see below), 4-GND (10k Pullup resistor between 1 and 3)
//RTC-Arduino:              GND-GND, VCC-3.3v, SDA-A4, SCL-A5
//SD Reader-Arduino:        CS-4, SCK(Clock)-13, MISO-12, MOSI-11, VCC-3.3/5v, GND-GND
//DHT22=Humidity/Tempurature Sensor, DS3231=RTC

#include "DHT.h"
#include <DS3231.h>
#include <SD.h>

#define DHTPIN 8    // what digital pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

  DHT dht(DHTPIN, DHTTYPE);   // Initialize DHT sensor.

  DS3231  rtc(A4, A5);    // Init the DS3231 using the hardware interface
  Time  t;    // Init a Time-data structure

  const int chipSelect = 4;
  
  float refresh_rate = 5000;    //change this to increase or decrease the time between samples
  unsigned long lastUpdate = 0;   // Keep track of last update time

void setup() {
  Serial.begin(115200);

  dht.begin();    //Begin Humidity Sensor
  rtc.begin();    //Begin RTC


  Serial.println("DHTxx test!");
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);    // make sure that the default chip select pin is set to output, even if you don't use it:
  if (!SD.begin(chipSelect)) {    // see if the card is present and can be initialized:
  Serial.println("Card failed, or not present");    // don't do anything more:
  return;
  }
  Serial.println("card initialized.");
    

  //The following lines can be uncommented to set the date and time
  //rtc.setDOW(WEDNESDAY);       // Set Day-of-Week
  //rtc.setTime(20, 58, 00);     // Set the time (00:00:00 24hr format)
  //rtc.setDate(07, 22, 2016);   // Set the date (January 1st, 2014)
 }


void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();   //Read Humidity
  float t = dht.readTemperature();    // Read temperature as Celsius (the default)

  char *strTime;    // pointer to a time string
  strTime = rtc.getTimeStr(FORMAT_LONG);    // Get formatted Time 
  

  // Check if any reads failed
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
  } else {
    // all ok. Put your code here to write the SD etc
  if (millis() > (lastUpdate + refresh_rate))
  {
  Serial.print("    Time = ");
  Serial.print(strTime);
  Serial.print("    Temperature = ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print("    Humidity = ");
  Serial.print(h);
  Serial.println(" % ");
  
 File dataFile = SD.open("datalog.txt", FILE_WRITE);
 if (dataFile) {
 dataFile.print(strTime);
 dataFile.print(" - ");
 dataFile.print(t);
 dataFile.print(" *C");
 dataFile.print(" - ");
 dataFile.print(h);
 dataFile.println(" %");
 dataFile.close();
 lastUpdate = millis();
 }
 else {
 Serial.println("error opening datalog.txt");
 }

}
 
  //float hif = dht.computeHeatIndex(f, h);   // Compute heat index in Fahrenheit (the default)
  //float hic = dht.computeHeatIndex(t, h, false);    // Compute heat index in Celsius (isFahreheit = false)
  }
}

I really appreciate your help! if you have any pointers to improve how ive done things that would be awesome! thanks again :slight_smile:

Quick question- i have the time set to 5000milliseconds (5 seconds)

float refresh_rate = 5000;    //change this to increase or decrease the time between samples

however it appears to loge more like every 7 seconds,

    Time = 20:11:47    Temperature = 25.40 *C     Humidity = 48.90 % 
    Time = 20:11:54    Temperature = 25.40 *C     Humidity = 48.90 % 
    Time = 20:12:01    Temperature = 25.40 *C     Humidity = 48.90 % 
    Time = 20:12:08    Temperature = 25.40 *C     Humidity = 48.80 % 
    Time = 20:12:15    Temperature = 25.50 *C     Humidity = 48.90 % 
    Time = 20:12:21    Temperature = 25.50 *C     Humidity = 48.90 % 
    Time = 20:12:28    Temperature = 25.50 *C     Humidity = 48.80 % 
    Time = 20:12:35    Temperature = 25.50 *C     Humidity = 48.80 % 
    Time = 20:12:42    Temperature = 25.50 *C     Humidity = 48.80 % 
    Time = 20:12:49    Temperature = 25.50 *C     Humidity = 48.70 %

any idea whats going on?

Thanks

You could try this, Has I used a text file to read from the SD card so that I could adjust the sample time without keep re-programing it

 unsigned long refresh_rate = 5000;    //change this to increase or decrease the time between samples

Steve

Steveiboy:
You could try this, Has I used a text file to read from the SD card so that I could adjust the sample time without keep re-programing it

 unsigned long refresh_rate = 5000;    //change this to increase or decrease the time between samples

Steve

Thank you, i have changed this but it is still doing the same thing? maybe this is using up too much cpu time?

Serial.print("    Time = ");
  Serial.print(strTime);
  Serial.print("    Temperature = ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print("    Humidity = ");
  Serial.print(h);
  Serial.println(" % ");
  
 File dataFile = SD.open("datalog.txt", FILE_WRITE);
 if (dataFile) {
 dataFile.print(strTime);
 dataFile.print(" - ");
 dataFile.print(t);
 dataFile.print(" *C");
 dataFile.print(" - ");
 dataFile.print(h);
 dataFile.println(" %");
 dataFile.close();
 lastUpdate = millis();
 }
 else {
 Serial.println("error opening datalog.txt");
 }

Thanks

Deanc8159:
Thank you, i have changed this but it is still doing the same thing? maybe this is using up too much cpu time?

Serial.print("    Time = ");

Serial.print(strTime);
  Serial.print("    Temperature = ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print("    Humidity = ");
  Serial.print(h);
  Serial.println(" % ");
 
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.print(strTime);
dataFile.print(" - ");
dataFile.print(t);
dataFile.print(" *C");
dataFile.print(" - ");
dataFile.print(h);
dataFile.println(" %");
dataFile.close();
lastUpdate = millis();
}
else {
Serial.println("error opening datalog.txt");
}




Thanks

If you are using your code above you've posted I noticed that that you have delay(2000); you need to remove this has you are causing your program to hang for 2 seconds each time it goes through the loop remove it and try it again and see if that helps

Steveiboy:
If you are using your code above you've posted I noticed that that you have delay(2000); you need to remove this has you are causing your program to hang for 2 seconds each time it goes through the loop remove it and try it again and see if that helps

I completely forgot about that! you have honestly saved me hours of tearing my hair out! thanks a lot!

i will give this a go and see how things run!

Thanks again

Glad it helped you, I'm fairly new to Arduino myself and have learnt a hell of a lot with reading the post and peoples code the people here are great