Advertisement
celem

SDReadWrite.ino

Feb 9th, 2013
1,395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.78 KB | None | 0 0
  1. /*
  2.   Reads temperature from HT11 sensor and writes to SD card
  3.   SD card read/write via LC Technology SD Module (www.lctech-inc.com)
  4.  
  5.  * SD card attached to SPI bus as follows:
  6.  ** MOSI - pin 11
  7.  ** MISO - pin 12
  8.  ** SCK  - pin 13
  9.  ** CS   - pin 4
  10.  ** GND  - GND
  11.  ** +3.3 - 3.3V
  12.  
  13.  HT11 Sensor is wired per below. Note: with checked openings facing you,
  14.  pin 1 is to the left.
  15.  ** PIN 1 - 3.3V or 5V
  16.  ** PIN 2 - Data (to pins attached below (probably 2)
  17.  ** PIN 3 - NC
  18.  ** PIN 4 - Ground
  19.  
  20.  This example code is in the public domain.
  21.  
  22.  */
  23. #define SOFTWARE_SPI 1
  24. #define ITERATIONS 25
  25.  
  26. #include <SD.h>     // SD Card library
  27. #include <dht11.h>  // DHT111 temperature probe Library
  28.  
  29. dht11 DHT11;
  30.  
  31.  
  32. File myFile;
  33.  
  34. void shutdown()
  35. {
  36.     // add you emergency shutdown code here
  37.     while (1); // endless loop
  38. }
  39.  
  40. void setup()
  41. {
  42. // Open serial communications and wait for port to open:
  43.     Serial.begin(9600);
  44.     DHT11.attach(2);
  45.     Serial.println("DHT11 TEST PROGRAM ");
  46.     Serial.print("LIBRARY VERSION: ");
  47.     Serial.println(DHT11LIB_VERSION);
  48.  
  49.  
  50.     Serial.print("Initializing SD card...");
  51.     // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  52.     // Note that even if it's not used as the CS pin, the hardware SS pin
  53.     // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  54.     // or the SD library functions will not work.
  55.     pinMode(10, OUTPUT);
  56.  
  57.     if (!SD.begin(4)) {
  58.         Serial.println("initialization failed!");
  59.         return;
  60.     }
  61.  
  62.     Serial.println("initialization done.");
  63.  
  64.     // open the file. note that only one file can be open at a time,
  65.     // so you have to close this one before opening another.
  66.     myFile = SD.open("temps.txt", FILE_WRITE);
  67.  
  68.     // if the file opened okay, write to it:
  69.     if (!myFile) {
  70.         // if the file didn't open, print an error:
  71.         Serial.println("error opening file - Halting system");
  72.         shutdown();
  73.     }
  74.  
  75.         Serial.print("Test limited to ");
  76.         Serial.print(ITERATIONS, DEC);
  77.         Serial.println(" temperature readings");
  78.         Serial.println("File on SD created, temperature tests will now start");
  79. }
  80.  
  81. void loop()
  82. {
  83.     int n, chk;
  84.  
  85.         myFile.println("Test of LC Technology SD Module and DFRobot DHT11 Temperature & Humidity Sensor");
  86.         myFile.print("Test limited to ");
  87.         myFile.print(ITERATIONS, DEC);
  88.         myFile.println(" temperature readings");
  89.     for (n = 0; n < ITERATIONS; n++) {
  90.         chk = DHT11.read();
  91.         Serial.print("Sensor read: ");
  92.         myFile.print("\nSensor read: ");
  93.  
  94.         switch (chk) {
  95.         case 0:
  96.             Serial.println("OK");
  97.             myFile.println("OK");
  98.             myFile.print("Humidity (%): ");
  99.             myFile.println((float)DHT11.humidity, DEC);
  100.  
  101.             myFile.print("Temperature (");
  102.             myFile.print((char)176);        // Print degree symbol
  103.             myFile.print("C): ");
  104.             myFile.println((float)DHT11.temperature, DEC);
  105.  
  106.             myFile.print("Temperature (");
  107.             myFile.print((char)176);
  108.             myFile.print("F): ");
  109.             myFile.println(DHT11.fahrenheit(), DEC);
  110.  
  111.             myFile.print("Temperature (");
  112.             myFile.print((char)176);
  113.             myFile.print("K): ");
  114.             myFile.println(DHT11.kelvin(), DEC);
  115.  
  116.             myFile.print("Dew Point (");
  117.             myFile.print((char)176);
  118.             myFile.print("C): ");
  119.             myFile.println(DHT11.dewPoint(), DEC);
  120.  
  121.             myFile.print("Dew PointFast (");
  122.             myFile.print((char)176);
  123.             myFile.print("C): ");
  124.             myFile.println(DHT11.dewPointFast(), DEC);
  125.             break;
  126.  
  127.         case -1:
  128.             Serial.println("Checksum error");
  129.             Serial.println("Checksum error");
  130.             break;
  131.  
  132.         case -2:
  133.             Serial.println("Time out error");
  134.             myFile.println("Time out error");
  135.             break;
  136.  
  137.         default:
  138.             Serial.println("Unknown error");
  139.             myFile.println("Unknown error");
  140.             break;
  141.         }
  142.         delay(2000);    // 2-second pause
  143.     }
  144.         Serial.println("Test Complete - Halting system");
  145.         myFile.close();
  146.     shutdown();
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement