Arduino project - Saving DHT22 data to sd card

Thread Starter

Ally773

Joined Jul 6, 2017
3
Hi there. I am currently working on an agricultural drone concept for my university RM project. Briefly, in my project, I will be capturing relative humidity and temperature data via the dht22 sensor data and saving it onto my sd card as a .txt file and the file data will be imported to excel spreadsheet into table/chart when I connect the sdcard reader to the laptop. The serial monitor is displaying correct form of data. Upon connecting my sd card to my laptop, the file is present. But I am getting an unreadable format of data such € ¤¤¨ÓdU5 instead of "73.84 , 22.50, ..." for Relative humidity and temperature respectively. I have gone through many many similar posts, tutorials, and videos but to no avail. The sd card is not corrupted and I have formatted with the windows and non-windows utility just to be sure. Could you assist me in getting the expected results? Thank you

Code:
#include <SPI.h>
#include <SD.h>

// Example sketch for DHT22 humidity - temperature sensor
// Written by cactus.io, with thanks to Adafruit for bits of their library. public domain

#include "cactus_io_DHT22.h"
#define DHTTYPE DHT22
#define DHT22_PIN 8     // what pin on the arduino is the DHT22 data line connected to

// For details on how to hookup the DHT22 sensor to the Arduino then checkout this page
// http://cactus.io/hookups/sensors/temperature-humidity/dht22/hookup-arduino-to-dht22-temp-humidity-sensor

// Initialize DHT sensor for normal 16mhz Arduino.
DHT22 dht(DHT22_PIN);
// Note: If you are using a board with a faster processor than 16MHz then you need
// to declare an instance of the DHT22 using
// DHT22 dht(DHT22_DATA_PIN, 30);
// The additional parameter, in this case here is 30 is used to increase the number of
// cycles transitioning between bits on the data and clock lines. For the
// Arduino boards that run at 84MHz the value of 30 should be about right.



const int chipSelect=4;
File myFile;


  void setup() {
   
  Serial.begin(9600);
  pinMode(chipSelect, OUTPUT);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
      Serial.println("initialization failed!");
      return;
  }
  {
     Serial.println("initialization done.");
  }
/*  {
   if (SD.exists("data.txt")) {
    Serial.println("data.txt exists.");
  } else {
    Serial.println("data.txt doesn't exist.");
  }
  }*/
 
    myFile = SD.open("data.txt", FILE_WRITE);
    Serial.println("DHT22 Humidity - Temperature Sensor");
    Serial.println("RH\t\tTemp (C)\tTemp (F)\tHeat Index (C)\tHeat Index (F)");
  if (myFile) {
 
    myFile.print(dht.humidity); //Serial.print(" %\t\t");
    myFile.print(",");
    myFile.print(dht.temperature_C);// Serial.print(" *C\t");
    myFile.print(",");
    myFile.print(dht.temperature_F); //Serial.print(" *F\t");
    myFile.print(",");
    myFile.print(dht.computeHeatIndex_C()); //Serial.print(" *C\t");
    myFile.print(",");
    myFile.print(dht.computeHeatIndex_F()); //Serial.println(" *F");
    myFile.close();
  }
    else {
    Serial.println("Error opening file in setup.");
  }
  dht.begin();

  delay(1000);
}


void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  dht.readHumidity();
  dht.readTemperature();
 
  // Check if any reads failed and exit early (to try again).
  if (isnan(dht.humidity) || isnan(dht.temperature_C)) {
    Serial.println("DHT sensor read failure!");
    return;
  }
  Serial.print(dht.humidity); Serial.print(" %\t\t");
  Serial.print(dht.temperature_C); Serial.print(" *C\t");
  Serial.print(dht.temperature_F); Serial.print(" *F\t");
  Serial.print(dht.computeHeatIndex_C()); Serial.print(" *C\t");
  Serial.print(dht.computeHeatIndex_F()); Serial.println(" *F");
 
  // Wait a few seconds between measurements. The DHT22 should not be read at a higher frequency of
  // about once every 2 seconds. So we add a 3 second delay to cover this.
//  saveData();//save to sd card
// delay(3000);
   // make a string for assembling the data to log:


   myFile = SD.open("data.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    myFile.print(dht.humidity); //Serial.print(" %\t\t");
    myFile.print(",");
    myFile.print(dht.temperature_C);// Serial.print(" *C\t");
    myFile.print(",");
    myFile.print(dht.temperature_F); //Serial.print(" *F\t");
    myFile.print(",");
    myFile.print(dht.computeHeatIndex_C()); //Serial.print(" *C\t");
    myFile.print(",");
    myFile.print(dht.computeHeatIndex_F()); //Serial.println(" *F");
    myFile.close();
  }
  else {
   // if the file didn't open, print an error:
    Serial.println("error opening data.txt");
  }
   delay(3000);

}
 

AlbertHall

Joined Jun 4, 2014
12,345
Maybe this is because the laptop program is expecting 16 bit unicode characters but you are writing 8 bit ansi.
Try using a hex file reader to see just what is in the file.
 

ericgibbs

Joined Jan 29, 2010
18,766
hi ally,
Which pin do have for the Data line, pin 2 or 8.???
#define DHT22_PIN 8 // pin on the arduino is the DHT22 data line connect.

The circuit diagram on your link says pin2.?

E
 

ericgibbs

Joined Jan 29, 2010
18,766
hi ally,
I built a quick test Arduino UNO, simplified the code by removing the SD coding.
This is the result I get, ref image, try this dht22aac1.txt with the native serial window.
Note the Data pin numbers.

Change the .txt to .ino OK

Eric
 

Attachments

AlbertHall

Joined Jun 4, 2014
12,345
hi ally,
I built a quick test Arduino UNO, simplified the code by removing the SD coding.
This is the result I get, ref image, try this dht22aac1.txt with the native serial window.
Note the Data pin numbers.

Change the .txt to .ino OK

Eric
TS says "The serial monitor is displaying correct form of data." so the problem would seem to be with saving to, or reading from the SD card.
 

ericgibbs

Joined Jan 29, 2010
18,766
hi AH,
Thanks for the heads up, missed that point.
I have a Arduino and SD card project running so I will add the DHT22 to that and retry his full program.
BTW: reason for the Humidity on my sample showing close to zero, it has a fault, its an old sensor.
E
 

ericgibbs

Joined Jan 29, 2010
18,766
hi Ally,
The original program works OK, only change I have made is to move CS to pin #10, to suit my quick test rig.
I am using a Arduino UNO, 8Gb uSD card in a standard SD adaptor.
Note: the DHT22 is an old one, with a defective humidity sensor.

I have read back the SD card into my PC using a USB SD card reader, the images are for the native serial [A005] and the SD read out data [A006].
I would suggest when you get it working, add a CRLF to each data line.

myFile.println(dht.computeHeatIndex_F()); //Serial.println(" *F");
myFile.close();

Eric

BTW:
Download this free HxD program.
The 'Extras' option is useful for checking data on SD cards.

http://download.cnet.com/HxD-Hex-Editor/3000-2352_4-10891068.html

This SD card formatter is free.
https://www.sdcard.org/downloads/formatter_4/
 

Attachments

Last edited:

Thread Starter

Ally773

Joined Jul 6, 2017
3
Thank you all for your assistance. I tried some your advices beforehand and some after this post. Apparently, when it is connected to my PC, I either capture only one set of data with some unreadable data or none at all or completely unreadable values. When I connected it to a powerbank (which has proved to be a better option than my 9V duracell battery), I got accurate values without any problem. The issue I think is somewhere with the hardware. Your suggestions made me trust my work and to solve the issue. I thank you all once again.
 

ericgibbs

Joined Jan 29, 2010
18,766
hi Ally,
The SD card module when writing does draw a high current during the Write cycle.
This can pull down the USB power voltage, the USB sourced voltage is usually less than 5v, some times its approx 4.5V.
This additional pull down can cause SD write problems, I guess you have discovered this already.

One method used is to add a 10uF or 22uF capacitor across the 5V and 0V lines , close to the SD card.

E
 

Thread Starter

Ally773

Joined Jul 6, 2017
3
hi Ally,
The SD card module when writing does draw a high current during the Write cycle.
This can pull down the USB power voltage, the USB sourced voltage is usually less than 5v, some times its approx 4.5V.
This additional pull down can cause SD write problems, I guess you have discovered this already.

One method used is to add a 10uF or 22uF capacitor across the 5V and 0V lines , close to the SD card.

E
Yes you indeed have made a good point here. I will be using the information for my next arduino project. Thank you Sir.
 

djsfantasi

Joined Apr 11, 2010
9,156
Note that I believe writing a float variable will output the binary. To get readable text, you will have to find/write a function to display the text representation of a binary/float value. Not close to my IDE, so can't supply specifics.
 

ericgibbs

Joined Jan 29, 2010
18,766
hi dj,
That Arduino sketch produces a serial data text output and SD card read data , that is ASCII text, as shown in post #7.
His original problem was due to a 'poor' 5V for the SD card module, which he has now fixed.

Eric
 
Last edited:
Top