Disconnect your power to the Arduino and wire up your temperature sensor AND your SD card. This sketch will take a specific number of data points and write them to a .txt file on the SD card. When you return to the main lab the next step will be to read that data file for later use.
Step 1:
Wire it up -- temperature sensor and SD card reader. My sketch is built around the TMP36 sensor so if you are using a different sensor you will need to modify the data taking portion to match your sensor.
Step 2:
Here's the code for this basic data logger.
/* Data Logger: Temperature This is a general framework for writing data to an SD card connected to the Arduino. The libraries in question aredocumented here... https://www.arduino.cc/en/Reference/SD documented here... https://www.arduino.cc/en/Reference/SPI SD card attached to SPI bus as follows: ** MOSI - pin 11 ** MISO - pin 12 ** CLK - pin 13 ** CS - pin 10 It is an important skill for an STEM worker to be able to understand and use a tool that they don't fully understand. This is personallly difficult for me because I want to know how it happens but sometimes you just gotta go with the flow and this is one of those times. I got the very lightly commented code below from https://www.arduino.cc/en/Tutorial/Datalogger and I am thankful to be able to do so. There are many almost identical versions of this code so it is clearly in the public domain. I will add my own thoughts or perhaps insights to the comments with my initals so that you know to be cautious about them. */ // these #include statements make the functions defined in these libraries // accessible to the writer of this sketch. See the documentation links // above for detailed descriptions of all functions. Notice that any function // used from these libraries starts with SD.'function' or SPI.'function'. You // can tell the IDE recognizes them because of the color change. #include <SD.h> #include <SPI.h> // Creates an object that is of a type File and gives it a name much // like defining an int or float (needs more clarity) -- BE File dataFile; // NOTE: data file name (datalog.txt) must be manually edited on // lines 83, 87, 126 // the CS pin manages the back and forth flow of information to the card as // part of the Serial Protocol Interface (SPI) method of communication. - BE int CS_pin = 10; // set up temperature sensor -- this is base on the TMP36 sensor. DHT11 users need changes int sensorPin = A0; //the analog pin the TMP36's Vout (sense) pin is connected to int dataCount = 0; // sets counter for data points int dataMax = 10; // sets max number of data points // To write the data to the SD card I must first put it into a character string. This is // the string I will use. String headerString = "Temperature (C)"; String dataString = ""; 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 } Serial.print("Initializing SD card..."); // SD.begin initiallizes the library and the card and returns TRUE if // successful. Argument articulates the control pin. Default pin depends // on board. if (SD.begin(10)) { Serial.println("SD card is initialized and it is ready to use"); } else { Serial.println("SD card is not initialized"); // don't do anything more: while (1); } // remove old data file otherwise data is appended Serial.println("Removing data file..."); SD.remove("datalog.txt"); delay(1000); // open the data file to write the header. dataFile = SD.open("datalog.txt", FILE_WRITE); // if the file is available, write to it: if (dataFile) { dataFile.println(headerString); dataFile.close(); // print to the serial port too: Serial.print("Header written: "); Serial.println(headerString); } // if the file isn't open, pop up an error: else { Serial.println("Error opening datalog.txt"); } } void loop() { // check to see if we've reached max data points if (dataCount >= dataMax) { Serial.println("Reached max number of data points"); Serial.println("Spinning wheels now......."); while(1); } Serial.print("Data point # "); Serial.println(dataCount); // Refer to the Temperature lab for details about reading the sensor and converting int sensorOut = analogRead(sensorPin); float voltage = sensorOut * (5.0/1024); // Serial.print(voltage); // Serial.println(" volts"); float temperatureC = (voltage - 0.5) * 100 ; Serial.print(temperatureC); Serial.println(" degrees C"); // convert temp to data string for writing to .txt file -- is this needed? - BE dataString = String(temperatureC); // open the file. note that only one file can be open at a time, // so you have to close this one before opening another. dataFile = SD.open("datalog.txt", FILE_WRITE); // if the file is available, write to it, close file after writing! if (dataFile) { dataFile.println(dataString); dataFile.close(); // print to the serial port too: Serial.print("Data written to file: "); Serial.println(dataString); } // if the file isn't open, pop up an error: else { Serial.println("error opening datalog.txt"); } // set delay between data points delay(5000); dataCount = dataCount + 1; }
Step 3: Explore!
Change the number of data points or the time delay between them. Now you can imagine loading this data logger onto the Arduino and powering the Arduino with the battery so you can place it anywhere you want. To restart the program just press the reset button on the Arduino.