Arduino project - Saving DHT22 data to sd card

Discussion in 'Homework Help' started by Ally773, Jul 6, 2017.

  1. Ally773

    Thread Starter New Member

    Jul 6, 2017
    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 (Text):
    1. #include <SPI.h>
    2. #include <SD.h>
    4. // Example sketch for DHT22 humidity - temperature sensor
    5. // Written by, with thanks to Adafruit for bits of their library. public domain
    7. #include "cactus_io_DHT22.h"
    8. #define DHTTYPE DHT22
    9. #define DHT22_PIN 8     // what pin on the arduino is the DHT22 data line connected to
    11. // For details on how to hookup the DHT22 sensor to the Arduino then checkout this page
    12. //
    14. // Initialize DHT sensor for normal 16mhz Arduino.
    15. DHT22 dht(DHT22_PIN);
    16. // Note: If you are using a board with a faster processor than 16MHz then you need
    17. // to declare an instance of the DHT22 using
    18. // DHT22 dht(DHT22_DATA_PIN, 30);
    19. // The additional parameter, in this case here is 30 is used to increase the number of
    20. // cycles transitioning between bits on the data and clock lines. For the
    21. // Arduino boards that run at 84MHz the value of 30 should be about right.
    25. const int chipSelect=4;
    26. File myFile;
    29.   void setup() {
    31.   Serial.begin(9600);
    32.   pinMode(chipSelect, OUTPUT);
    34.   while (!Serial) {
    35.     ; // wait for serial port to connect. Needed for native USB port only
    36.   }
    38.   Serial.print("Initializing SD card...");
    40.   if (!SD.begin(chipSelect)) {
    41.       Serial.println("initialization failed!");
    42.       return;
    43.   }
    44.   {
    45.      Serial.println("initialization done.");
    46.   }
    47. /*  {
    48.    if (SD.exists("data.txt")) {
    49.     Serial.println("data.txt exists.");
    50.   } else {
    51.     Serial.println("data.txt doesn't exist.");
    52.   }
    53.   }*/
    55.     myFile ="data.txt", FILE_WRITE);
    56.     Serial.println("DHT22 Humidity - Temperature Sensor");
    57.     Serial.println("RH\t\tTemp (C)\tTemp (F)\tHeat Index (C)\tHeat Index (F)");
    58.   if (myFile) {
    60.     myFile.print(dht.humidity); //Serial.print(" %\t\t");
    61.     myFile.print(",");
    62.     myFile.print(dht.temperature_C);// Serial.print(" *C\t");
    63.     myFile.print(",");
    64.     myFile.print(dht.temperature_F); //Serial.print(" *F\t");
    65.     myFile.print(",");
    66.     myFile.print(dht.computeHeatIndex_C()); //Serial.print(" *C\t");
    67.     myFile.print(",");
    68.     myFile.print(dht.computeHeatIndex_F()); //Serial.println(" *F");
    69.     myFile.close();
    70.   }
    71.     else {
    72.     Serial.println("Error opening file in setup.");
    73.   }
    74.   dht.begin();
    76.   delay(1000);
    77. }
    80. void loop() {
    81.   // Reading temperature or humidity takes about 250 milliseconds!
    82.   // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
    83.   dht.readHumidity();
    84.   dht.readTemperature();
    86.   // Check if any reads failed and exit early (to try again).
    87.   if (isnan(dht.humidity) || isnan(dht.temperature_C)) {
    88.     Serial.println("DHT sensor read failure!");
    89.     return;
    90.   }
    91.   Serial.print(dht.humidity); Serial.print(" %\t\t");
    92.   Serial.print(dht.temperature_C); Serial.print(" *C\t");
    93.   Serial.print(dht.temperature_F); Serial.print(" *F\t");
    94.   Serial.print(dht.computeHeatIndex_C()); Serial.print(" *C\t");
    95.   Serial.print(dht.computeHeatIndex_F()); Serial.println(" *F");
    97.   // Wait a few seconds between measurements. The DHT22 should not be read at a higher frequency of
    98.   // about once every 2 seconds. So we add a 3 second delay to cover this.
    99. //  saveData();//save to sd card
    100. // delay(3000);
    101.    // make a string for assembling the data to log:
    104.    myFile ="data.txt", FILE_WRITE);
    106.   // if the file opened okay, write to it:
    107.   if (myFile) {
    108.     myFile.print(dht.humidity); //Serial.print(" %\t\t");
    109.     myFile.print(",");
    110.     myFile.print(dht.temperature_C);// Serial.print(" *C\t");
    111.     myFile.print(",");
    112.     myFile.print(dht.temperature_F); //Serial.print(" *F\t");
    113.     myFile.print(",");
    114.     myFile.print(dht.computeHeatIndex_C()); //Serial.print(" *C\t");
    115.     myFile.print(",");
    116.     myFile.print(dht.computeHeatIndex_F()); //Serial.println(" *F");
    117.     myFile.close();
    118.   }
    119.   else {
    120.    // if the file didn't open, print an error:
    121.     Serial.println("error opening data.txt");
    122.   }
    123.    delay(3000);
    125. }
  2. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    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.
  3. ericgibbs


    Jan 29, 2010
    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.?

  4. ericgibbs


    Jan 29, 2010
    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

  5. AlbertHall

    AAC Fanatic!

    Jun 4, 2014
    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.
  6. ericgibbs


    Jan 29, 2010
    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.
  7. ericgibbs


    Jan 29, 2010
    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");


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

    This SD card formatter is free.
    Last edited: Jul 7, 2017
  8. Ally773

    Thread Starter New Member

    Jul 6, 2017
    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.
  9. ericgibbs


    Jan 29, 2010
    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.

  10. Ally773

    Thread Starter New Member

    Jul 6, 2017
    Yes you indeed have made a good point here. I will be using the information for my next arduino project. Thank you Sir.
  11. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    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.
  12. ericgibbs


    Jan 29, 2010
    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.

    Last edited: Jul 9, 2017
    djsfantasi likes this.