Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //If you found my video helpful, please SUBSCRIBE: https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
- //The code belongs to this tutorial video: https://youtu.be/eWZP44vS7cU
- /* PIN LAYOUT FOR SD CARD READER
- * CS = 4
- * SCK = 13
- * MOSI = 11
- * MISO = 12
- * VCC = 5V (Make sure that your module is 5V and not 3.3V)
- * GND = GND
- * -------------------------------------------------------
- * PIN LAYOUT FOR LM335Z
- * Flat part is facing you!
- * 1 2 3
- * ADJ OUT GND
- * -------------------------------------------------------
- * WIRING FOR LM335Z
- *
- * Arduino pins resistor LM335Z Chip
- * (+5V)-------------[ 2k ]---/-------------[OUT]
- * (A0)----------------------/
- * (GND)------------------------------------[GND]
- * x---[ADJ] //NOT CONNECTED
- * -------------------------------------------------------
- * PIN LAYOUT FOR 16x2 LCD SCREEN
- * VIN = 5V
- * GND = GND
- * SCL/SCK = A5
- * SDA = A4
- */
- /*
- If you want to buy the things shown in the video and support me at the same time, please use the links below:
- Arduino UNO: https://www.banggood.com/custlink/33KKF85c3i
- Micro SD Card: https://www.banggood.com/custlink/3KvKestC6r
- Micro SD card module for Arduino (3PCS): https://www.banggood.com/custlink/K3GD4bhPkC
- LM335Z temperature sensor chip: https://www.banggood.com/custlink/DD3v0nYF2S
- 16x2 LCD (I2C): https://www.banggood.com/custlink/KmGGgtP2Lv
- 2k resistor 1W, 50 PCS: https://www.banggood.com/custlink/D3mveAEP26
- Large breadboard: https://www.banggood.com/custlink/DvDGeg8gw5
- Multimeter: https://www.banggood.com/custlink/vDKGFeuJtt
- Thermometer with thermocouple: https://www.banggood.com/custlink/DvDvtBTW8H
- Heating Gun for heat shrink tube https://www.banggood.com/custlink/m3D34AFZG7
- Silicone Soldering mat: https://www.banggood.com/custlink/D3Kv0j8t6O
- */
- #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
- //-------------------------------------------
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement