Here is the upgraded version of the data logger sketch that attempts to create an csv compatible file for importing into a Jupyter notebook.
Step 1:
Grab the code and give it a try. (8/1/20 -- I updated the code to include a time stamp on the data written to the SD card.)
/* Data Logger: Temperature This is a general framework for writing data to an SD card connected to the Arduino. This has been updated to include a timestamp in the data written to the SD card -- 8/1/20 Bruce 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 (Bruce.csv) can be changed but we are working here // to create a file that is formatted to match the .csv standard #define fileName "Bruce.csv" // 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; // Timing variables unsigned long initialTime; unsigned long currentMilli; float timeStart; float timeNow; // If I want to remove the data file initially I can change this to true bool removeFile = false; // if you wish to erase file after reading set to true // 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 = 40; // sets max number of data points // To write the data to the SD card I must first put it into a character string. // The header (first line) of a csv file are the labels for the columns of data String headerString = "data,time(s),temperature(C)"; // dataString is a string variable that will be filled with the information for each // line of data as I create it. 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 if (removeFile){ Serial.print("Removing data file..."); Serial.println(fileName); SD.remove(fileName); Serial.println("File removed"); delay(1000); } // open the data file to write the header. dataFile = SD.open(fileName, 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 for header"); } } 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); // get time currentMilli = millis(); timeNow = currentMilli/1000.; // 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(dataCount); dataString += ","; dataString += String(timeNow); dataString += ","; 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(fileName, 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); dataString = ""; // clear the data string for next line of data // Serial.print("Cleared data string: "); // Serial.println(dataString); } // if the file isn't open, pop up an error: else { Serial.println("error opening datalog.txt for data"); Serial.print("dataFile: "); Serial.println(dataFile); } // set delay between data points delay(60000); dataCount = dataCount + 1; }
Step 2: Things to try!
If you watch in the serial monitor you should be able to see the data that is being written to the file on the SD card. I assume that you will change the name to something more appropriate to you!
When you go to read the file on the card so you can copy it and paste it where your Jupyter notebook can find it you will have to explore whether you can plug the SD card into your computer to read it or whether you will need to use our ReadSD sketch and then copy and paste the csv file into a text editor. I will post instructions for this here.