celem icon

SDReadWrite.ino

celem | PRO | 02/09/13 11:08:07 PM UTC | 0 ⭐ | 1757 👁️ | Never ⏰ | []
C |

3.78 KB

|

None

|

0 👍

/

0 👎

/*
  Reads temperature from HT11 sensor and writes to SD card
  SD card read/write via LC Technology SD Module (www.lctech-inc.com)
 
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** SCK  - pin 13
 ** CS   - pin 4
 ** GND  - GND
 ** +3.3 - 3.3V
 
 HT11 Sensor is wired per below. Note: with checked openings facing you,
 pin 1 is to the left.
 ** PIN 1 - 3.3V or 5V
 ** PIN 2 - Data (to pins attached below (probably 2)
 ** PIN 3 - NC
 ** PIN 4 - Ground
 
 This example code is in the public domain.
 
 */
#define SOFTWARE_SPI 1
#define ITERATIONS 25
 
#include <SD.h>     // SD Card library
#include <dht11.h>  // DHT111 temperature probe Library
 
dht11 DHT11;
 
 
File myFile;
 
void shutdown()
{
    // add you emergency shutdown code here
    while (1); // endless loop
}
 
void setup()
{
// Open serial communications and wait for port to open:
    Serial.begin(9600);
    DHT11.attach(2);
    Serial.println("DHT11 TEST PROGRAM ");
    Serial.print("LIBRARY VERSION: ");
    Serial.println(DHT11LIB_VERSION);
 
 
    Serial.print("Initializing SD card...");
    // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
    // Note that even if it's not used as the CS pin, the hardware SS pin
    // (10 on most Arduino boards, 53 on the Mega) must be left as an output
    // or the SD library functions will not work.
    pinMode(10, OUTPUT);
 
    if (!SD.begin(4)) {
        Serial.println("initialization failed!");
        return;
    }
 
    Serial.println("initialization done.");
 
    // open the file. note that only one file can be open at a time,
    // so you have to close this one before opening another.
    myFile = SD.open("temps.txt", FILE_WRITE);
 
    // if the file opened okay, write to it:
    if (!myFile) {
        // if the file didn't open, print an error:
        Serial.println("error opening file - Halting system");
        shutdown();
    }
 
        Serial.print("Test limited to ");
        Serial.print(ITERATIONS, DEC);
        Serial.println(" temperature readings");
        Serial.println("File on SD created, temperature tests will now start");
}
 
void loop()
{
    int n, chk;
 
        myFile.println("Test of LC Technology SD Module and DFRobot DHT11 Temperature & Humidity Sensor");
        myFile.print("Test limited to ");
        myFile.print(ITERATIONS, DEC);
        myFile.println(" temperature readings");
    for (n = 0; n < ITERATIONS; n++) {
        chk = DHT11.read();
        Serial.print("Sensor read: ");
        myFile.print("\nSensor read: ");
 
        switch (chk) {
        case 0:
            Serial.println("OK");
            myFile.println("OK");
            myFile.print("Humidity (%): ");
            myFile.println((float)DHT11.humidity, DEC);
 
            myFile.print("Temperature (");
            myFile.print((char)176);        // Print degree symbol
            myFile.print("C): ");
            myFile.println((float)DHT11.temperature, DEC);
 
            myFile.print("Temperature (");
            myFile.print((char)176);
            myFile.print("F): ");
            myFile.println(DHT11.fahrenheit(), DEC);
 
            myFile.print("Temperature (");
            myFile.print((char)176);
            myFile.print("K): ");
            myFile.println(DHT11.kelvin(), DEC);
 
            myFile.print("Dew Point (");
            myFile.print((char)176);
            myFile.print("C): ");
            myFile.println(DHT11.dewPoint(), DEC);
 
            myFile.print("Dew PointFast (");
            myFile.print((char)176);
            myFile.print("C): ");
            myFile.println(DHT11.dewPointFast(), DEC);
            break;
 
        case -1:
            Serial.println("Checksum error");
            Serial.println("Checksum error");
            break;
 
        case -2:
            Serial.println("Time out error");
            myFile.println("Time out error");
            break;
 
        default:
            Serial.println("Unknown error");
            myFile.println("Unknown error");
            break;
        }
        delay(2000);    // 2-second pause
    }
        Serial.println("Test Complete - Halting system");
        myFile.close();
    shutdown();
}

Comments