Advertisement
CuriousScientist

Writing sensor data to SD card using Arduino

Nov 8th, 2019
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found my video helpful, please SUBSCRIBE:  https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
  2. //The code belongs to this tutorial video: https://youtu.be/eWZP44vS7cU
  3.  
  4. /* PIN LAYOUT FOR SD CARD READER
  5.  * CS = 4
  6.  * SCK = 13
  7.  * MOSI = 11
  8.  * MISO = 12
  9.  * VCC = 5V (Make sure that your module is 5V and not 3.3V)
  10.  * GND = GND
  11.  * -------------------------------------------------------
  12.  * PIN LAYOUT FOR LM335Z
  13.  * Flat part is facing you!
  14.  *  1    2     3
  15.  * ADJ  OUT   GND
  16.  * -------------------------------------------------------
  17.  * WIRING FOR LM335Z
  18.  *
  19.  * Arduino pins      resistor            LM335Z Chip
  20.  * (+5V)-------------[ 2k ]---/-------------[OUT]
  21.  * (A0)----------------------/
  22.  * (GND)------------------------------------[GND]
  23.  *                                      x---[ADJ] //NOT CONNECTED  
  24.  * -------------------------------------------------------
  25.  * PIN LAYOUT FOR 16x2 LCD SCREEN
  26.  * VIN = 5V
  27.  * GND = GND
  28.  * SCL/SCK = A5
  29.  * SDA = A4  
  30.  */
  31.  
  32. /*
  33. If you want to buy the things shown in the video and support me at the same time, please use the links below:
  34.  
  35. Arduino UNO: https://www.banggood.com/custlink/33KKF85c3i
  36. Micro SD Card: https://www.banggood.com/custlink/3KvKestC6r
  37. Micro SD card module for Arduino (3PCS): https://www.banggood.com/custlink/K3GD4bhPkC
  38. LM335Z temperature sensor chip: https://www.banggood.com/custlink/DD3v0nYF2S
  39. 16x2 LCD (I2C): https://www.banggood.com/custlink/KmGGgtP2Lv
  40. 2k resistor 1W, 50 PCS: https://www.banggood.com/custlink/D3mveAEP26
  41. Large breadboard: https://www.banggood.com/custlink/DvDGeg8gw5
  42. Multimeter: https://www.banggood.com/custlink/vDKGFeuJtt
  43. Thermometer with thermocouple: https://www.banggood.com/custlink/DvDvtBTW8H
  44. Heating Gun for heat shrink tube https://www.banggood.com/custlink/m3D34AFZG7
  45. Silicone Soldering mat: https://www.banggood.com/custlink/D3Kv0j8t6O
  46. */
  47.  
  48.  
  49. #include <SD.h> // SD library
  50. //#include <SPI.h> //OLED --this is not needed here, but I mentioned in the video that this is necessary for OLED screen
  51. #include <Wire.h> //I2C
  52. #include <LiquidCrystal_I2C.h>
  53. LiquidCrystal_I2C lcd(0x27, 16, 2);
  54.  
  55. int CS = 4; //chip select pin for the MicroSD Card Adapter, This is the CS Pin
  56. File file; // file object that is used to read and write data
  57. int outputPin= A0; //A0 as the output pin for the LM355Z
  58.  
  59. //variables for the temperature values
  60. float tempKelvin;
  61. float tempCelsius;
  62.  
  63. void setup() {
  64.  
  65.   Serial.begin(9600); // start serial
  66.  
  67.   //-----------------Taking care of LCD-------------------
  68.   //NOTE: if you cannot see the text on the LCD, try to change the potmeter on the back of it.
  69.   //Sometimes you just have wrong contrast settings and nothing shows up on the screen because of it.
  70.   lcd.init();                      // initialize the lcd
  71.   lcd.init();
  72.   lcd.backlight(); //initialize backlight
  73.   //------------------------------------------------------
  74.   lcd.clear(); //clear the LCD
  75.   lcd.setCursor(0, 0); //Defining position to write from first row,first column .
  76.   lcd.print("LM335Z Sensor"); //some message
  77.   lcd.setCursor(0, 1); //Cursor is moved to the 2nd line of the LCD
  78.   lcd.print("Temperature"); //You can write 16 Characters per line .
  79.   delay(3000); //wait 3 sec
  80.   //--------------------------------
  81.  
  82.   pinMode(CS, OUTPUT); // chip select pin is set as OUTPUT
  83.  
  84.   if (!SD.begin(CS)) { // Initialize SD card
  85.     Serial.println("Could not initialize SD card."); // if return value is false, something went wrong.
  86.   }
  87.  
  88.   if (SD.exists("file.txt")) { // if "file.txt" exists, fill will be deleted
  89.     Serial.println("File exists.");
  90.     if (SD.remove("file.txt") == true) { //If file is removed, we print a message
  91.       Serial.println("Successfully removed file.");
  92.     } else {
  93.       Serial.println("Could not removed file.");
  94.     }
  95.   }
  96.  
  97. }
  98. void loop()
  99. {
  100.   readTemp();
  101.   delay(200); //update and writing interval basically.
  102.   printLCD();
  103.   writeFile();
  104.  
  105. }
  106.  
  107. void writeFile() //writing something to the SD card
  108. {
  109.   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()
  110.   if (file) {  
  111.     file.println(tempCelsius, 2); // write number to file; in this case, the temperature with 2 decimals precision
  112.     file.close(); // close file
  113.     Serial.print(tempCelsius, 2); // debug output: show written number in serial monitor
  114.     //you can write as much as you want, just make sure that you have a consistent formatting!
  115.    
  116.   } else {
  117.     Serial.println("Could not open file (writing).");
  118.   }
  119.  
  120.  
  121. }
  122.  
  123. void readFile()
  124. {
  125.   //Reading
  126.   //You can read the content of the file and print it on the serial.
  127.   //This is not explained in the tutorial video because I was only focusing on the writing
  128.   file = SD.open("file.txt", FILE_READ); // open "file.txt" to read data
  129.   if (file) {
  130.     Serial.println("--- Reading start ---");
  131.     char character;
  132.     while ((character = file.read()) != -1) { // this while loop reads data stored in "file.txt" and prints it to serial monitor
  133.       Serial.print(character);
  134.     }
  135.     file.close();
  136.     Serial.println("--- Reading end ---");
  137.   } else {
  138.     Serial.println("Could not open file (reading).");
  139.   }
  140. }
  141.  
  142. void readTemp() //reading the LM335Z
  143. {
  144. int analogBit = analogRead(outputPin); //read A0, store it in the analogBit variable
  145.  
  146. tempKelvin = (analogBit/1024.0) * 4876 /10;
  147. //Small explanation
  148. //1024 comes from the resolution of the ADC. 2^10 = 1024. If you use different ADC, change the value.
  149. //4876 (mV) comes from the measured voltage on the 5V rail.
  150. //To have precise data, measure the 5V rail when everything is connected to the arduino (screen, modules, etc) and they are running
  151. //use the measured voltage in mV (millivolts) in the formula
  152. // division by 10 comes from the gain of the LM335Z chip which is G=10 mV/K.
  153.  
  154. tempCelsius = tempKelvin - 273.15; //converting kelvin to celsius
  155.  
  156. Serial.print("Kelvin: ");
  157. Serial.println(tempKelvin); //Output: "Kelvin: xxxxx"
  158. Serial.print("Celsius: ");
  159. Serial.println(tempCelsius); //Output: "Celsius: xxxxx"
  160. }
  161.  
  162. void printLCD()
  163. {
  164.           //-------------LCD Printout------------------
  165.           lcd.clear(); //clear LCD
  166.           lcd.setCursor(0, 0); //Defining position to write from first row,first column .
  167.           lcd.print("Temperature");
  168.           lcd.setCursor(0, 1); //Defining position to write from second row,first column .
  169.           lcd.print(tempCelsius, 2); //You can write 16 Characters per line; here we print celsius degrees with 2 digit precision
  170.           //-------------------------------------------
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement