#include #include #include #include #define ONE_WIRE_BUS 7 // Data wire is plugged into pin 7 of the arduino // Setup a OneWire instance to communicate with any OneWire devices OneWire oneWire(ONE_WIRE_BUS); // Pass OneWire reference to Dallas Temperature DallasTemperature sensors(&oneWire); const int chipSelect = 10; long id = 0; void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } sensors.begin(); // Start up the library Serial.print("Initializing SD card..."); // see if the card is present and can be initialized: if (!SD.begin(chipSelect)) { Serial.println("Card failed, or not present"); // don't do anything more: while (1); } Serial.println("card initialized."); } void loop() { // call sensors.requestTemperatures() to issue a global temperature // request to all devices on the bus // Send the command to get temperature readings sensors.requestTemperatures(); // make a string for assembling the data to log: String dataString = ""; // read the sensor data and append to string: dataString = String(id) + "," + String(sensors.getTempCByIndex(0)); // You can have more than one DS18B20 on the same bus (0 refers to the first IC on the wire). // open the file. note that only one file can be open at a time, // so you have to close this one before opening another. File dataFile = SD.open("datalog.csv", FILE_WRITE); // if the file is available, write to it: if (dataFile) { dataFile.println(dataString); dataFile.close(); // print to the serial port too: Serial.println(dataString); } // if the file isn't open, pop up an error: else { Serial.println("error opening datalog.csv"); } id++; // increase id delay(5000); // wait for 5s before starting new measurement }