Saving sensor data to SD card using Arduino
In this video I show you a simple Arduino-based project which consist of a LM335Z temperature sensor, a 16x2 LCD and a micro SD card reader. This project is a good starting point to learn how to write the output data of a sensor on a SD card. This is useful if you want to make a logging device that you want to put somewhere in a remote location or want to log data without having the Arduino connected to a computer. The possibilities are endless. You can add more sensors, format the data written on the card, change the logging interval, etc.
Wiring diagram
Arduino source code
#include <SD.h> // SD library //#include <SPI.h> //OLED --this is not needed here, but I mentioned in the video that this is necessary for OLED screen #include <Wire.h> //I2C #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); int CS = 4; //chip select pin for the MicroSD Card Adapter, This is the CS Pin File file; // file object that is used to read and write data int outputPin= A0; //A0 as the output pin for the LM355Z //variables for the temperature values float tempKelvin; float tempCelsius; void setup() { Serial.begin(9600); // start serial //-----------------Taking care of LCD------------------- //NOTE: if you cannot see the text on the LCD, try to change the potmeter on the back of it. //Sometimes you just have wrong contrast settings and nothing shows up on the screen because of it. lcd.init(); // initialize the lcd lcd.init(); lcd.backlight(); //initialize backlight //------------------------------------------------------ lcd.clear(); //clear the LCD lcd.setCursor(0, 0); //Defining position to write from first row,first column . lcd.print("LM335Z Sensor"); //some message lcd.setCursor(0, 1); //Cursor is moved to the 2nd line of the LCD lcd.print("Temperature"); //You can write 16 Characters per line . delay(3000); //wait 3 sec //-------------------------------- pinMode(CS, OUTPUT); // chip select pin is set as OUTPUT if (!SD.begin(CS)) { // Initialize SD card Serial.println("Could not initialize SD card."); // if return value is false, something went wrong. } if (SD.exists("file.txt")) { // if "file.txt" exists, fill will be deleted Serial.println("File exists."); if (SD.remove("file.txt") == true) { //If file is removed, we print a message Serial.println("Successfully removed file."); } else { Serial.println("Could not removed file."); } } } void loop() { readTemp(); delay(200); //update and writing interval basically. printLCD(); writeFile(); } void writeFile() //writing something to the SD card { file = SD.open("file.txt", FILE_WRITE); // open "file.txt" to write data; make sure that you want to write in the same file that you created in the setup() if (file) { file.println(tempCelsius, 2); // write number to file; in this case, the temperature with 2 decimals precision file.close(); // close file Serial.print(tempCelsius, 2); // debug output: show written number in serial monitor //you can write as much as you want, just make sure that you have a consistent formatting! } else { Serial.println("Could not open file (writing)."); } } void readFile() { //Reading //You can read the content of the file and print it on the serial. //This is not explained in the tutorial video because I was only focusing on the writing file = SD.open("file.txt", FILE_READ); // open "file.txt" to read data if (file) { Serial.println("--- Reading start ---"); char character; while ((character = file.read()) != -1) { // this while loop reads data stored in "file.txt" and prints it to serial monitor Serial.print(character); } file.close(); Serial.println("--- Reading end ---"); } else { Serial.println("Could not open file (reading)."); } } void readTemp() //reading the LM335Z { int analogBit = analogRead(outputPin); //read A0, store it in the analogBit variable tempKelvin = (analogBit/1024.0) * 4876 /10; //Small explanation //1024 comes from the resolution of the ADC. 2^10 = 1024. If you use different ADC, change the value. //4876 (mV) comes from the measured voltage on the 5V rail. //To have precise data, measure the 5V rail when everything is connected to the arduino (screen, modules, etc) and they are running //use the measured voltage in mV (millivolts) in the formula // division by 10 comes from the gain of the LM335Z chip which is G=10 mV/K. tempCelsius = tempKelvin - 273.15; //converting kelvin to celsius Serial.print("Kelvin: "); Serial.println(tempKelvin); //Output: "Kelvin: xxxxx" Serial.print("Celsius: "); Serial.println(tempCelsius); //Output: "Celsius: xxxxx" } void printLCD() { //-------------LCD Printout------------------ lcd.clear(); //clear LCD lcd.setCursor(0, 0); //Defining position to write from first row,first column . lcd.print("Temperature"); lcd.setCursor(0, 1); //Defining position to write from second row,first column . lcd.print(tempCelsius, 2); //You can write 16 Characters per line; here we print celsius degrees with 2 digit precision //------------------------------------------- }