Advertisement
CuriousScientist

Arduino ADS1115, LM335, 4 channel temperature logger

Jun 19th, 2020
2,196
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/c/CuriousScientist?sub_confirmation=1
  2. //The code belongs to the following tutorial video: https://youtu.be/Weu8omaW4j8
  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.  *                                          [ADJ] //NOT CONNECTED  
  24.  * -------------------------------------------------------
  25.  * PIN LAYOUT FOR LCD SCREEN
  26.  * VIN = 5V
  27.  * GND = GND
  28.  * SCL/SCK = A5
  29.  * SDA = A4
  30.  *
  31.  * PIN LAYOUT FOR ADS1115
  32.  * V = 5V
  33.  * G = GND
  34.  * SCL/SCK = A5
  35.  * SDA = A4  
  36.  * ADDR = GND
  37.  */
  38.  
  39. #include <SD.h> // SD library
  40. #include <Wire.h> //I2C
  41. #include <Adafruit_ADS1015.h>
  42. #include <LiquidCrystal_I2C.h>
  43.  
  44. //ADS and LCD
  45. Adafruit_ADS1115 ads;
  46. LiquidCrystal_I2C lcd(0x27, 16, 2);
  47.  
  48. int CS = 4; //chip select pin for the MicroSD Card Adapter, This is the CS Pin
  49. File file; // file object that is used to read and write data
  50.  
  51. int analogBit; //raw analog data from the ads1115
  52. float tempKelvin; //Temperature in Kelvin
  53. float tempCelsius; //Temperature in Celsius
  54.  
  55. float T1, T2, T3, T4; // these are the 4 channels
  56.  
  57. const byte FileStartButton = 2; //attachinterrupt button for starting the measurement (red)
  58. const byte FileStopButton = 3; //attachinterrupt button for stopping the measurement (black)
  59.  
  60. const byte GreenLED = 5; //Status LEDs
  61. const byte RedLED = 6;
  62.  
  63. bool WritingEnabled = false; //we switch the status of this with the buttons
  64.  
  65. unsigned long startTime;
  66. unsigned long elapsedTime;
  67.  
  68.  
  69. void setup() {
  70.  
  71.   //Defining the LEDs' functions
  72.   pinMode(GreenLED, OUTPUT);
  73.   pinMode(RedLED, OUTPUT);
  74.   digitalWrite(GreenLED, LOW);  
  75.   digitalWrite(RedLED, HIGH);  
  76.  
  77.   Serial.begin(9600); // start serial
  78.  
  79.   //Attachinterrupt pins  
  80.   pinMode(FileStartButton, INPUT_PULLUP);
  81.   pinMode(FileStopButton, INPUT_PULLUP);
  82.   attachInterrupt(digitalPinToInterrupt(FileStartButton), FileStart, CHANGE);
  83.   attachInterrupt(digitalPinToInterrupt(FileStopButton), FileStop, CHANGE);
  84.  
  85.   //ADC setup
  86.   ads.setGain(GAIN_TWOTHIRDS); // Gain - multiplier for the bit = 0.1875 mV
  87.   //ads.setGain(GAIN_ONE); //0.125 mV
  88.   //ads.setGain(GAIN_SIXTEEN); // 0.0078125 mV
  89.  
  90.   ads.begin();
  91.   //-------------------------------------
  92.  
  93.   //-----------------Taking care of LCD-------------------
  94.   //NOTE: if you cannot see the text on the LCD, try to change the potmeter on the back of it.
  95.   //Sometimes you just have wrong contrast settings and nothing shows up on the screen because of it.  
  96.   lcd.begin();
  97.   lcd.backlight(); //initialize backlight
  98.   //------------------------------------------------------
  99.   lcd.clear(); //clear the LCD
  100.   lcd.setCursor(0, 0); //Defining positon to write from first row,first column .
  101.   lcd.print("LM335Z Sensor"); //some message
  102.   lcd.setCursor(0, 1); //Cursor is moved to the 2nd line of the LCD
  103.   lcd.print("Temperature"); //You can write 16 Characters per line .
  104.   delay(1000); //wait 3 sec
  105.   //--------------------------------
  106.  
  107.  
  108.   //SD card module
  109.   pinMode(CS, OUTPUT); // chip select pin is set as OUTPUT
  110.  
  111.   if (!SD.begin(CS)) { // Initialize SD card
  112.     Serial.println("Could not initialize SD card."); // if return value is false, something went wrong.
  113.   }
  114.  
  115.   if (SD.exists("Temp.txt")) { // if "Temp.txt" exists, fill will be deleted
  116.     Serial.println("File exists.");
  117.     if (SD.remove("Temp.txt") == true) {
  118.       Serial.println("Successfully removed file.");
  119.     } else {
  120.       Serial.println("Could not removed file.");
  121.     }
  122.   }
  123.  
  124.   //Starting timer for the elapsed time
  125.   startTime = millis();
  126.  
  127. }
  128. void loop()
  129. {
  130.   //Calculate elapsed time
  131.   elapsedTime = millis() - startTime;
  132.   readADS();
  133.   delay(1000); //update and writing intervall basically. Change this for slower/faster acquisition
  134.   printLCD();
  135.   LEDControl();
  136.   writeFile();
  137. //please SUBSCRIBE:  https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  138.  
  139. }
  140.  
  141. void writeFile() //writing something to the SD card
  142. {
  143.   if(WritingEnabled == true)
  144.   {
  145.     file = SD.open("Temp.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()
  146.     if (file)
  147.     {
  148.       file.print(elapsedTime);
  149.       file.print(" ");
  150.       file.print(T1);
  151.       file.print(" ");
  152.       file.print(T2);
  153.       file.print(" ");
  154.       file.print(T3);
  155.       file.print(" ");
  156.       file.print(T4);
  157.       file.println(" ");      
  158.       file.close(); // close file
  159.       //Serial.print(tempCelsius, 2); // debug output: show written number in serial monitor
  160.       //you can write as much as you want, just make sure that you have a consistent formatting!
  161.       Serial.println("Success");
  162.     } else {
  163.       Serial.println("Could not open file (writing).");
  164.     }  
  165.   }
  166.   else
  167.   {
  168.   //  
  169.   }
  170. }
  171.  
  172. void LEDControl()
  173. {
  174.   if(WritingEnabled == true)
  175.   {
  176.     digitalWrite(GreenLED, HIGH);  
  177.     digitalWrite(RedLED, LOW);
  178.     lcd.setCursor(15, 1); //Defining positon to write from second row,first column .
  179.     lcd.print("!"); //You can write 16 Characters per line
  180.   }
  181.   else
  182.   {
  183.     digitalWrite(GreenLED, LOW);  
  184.     digitalWrite(RedLED, HIGH);    
  185.     lcd.setCursor(15, 1); //Defining positon to write from second row,first column .
  186.     lcd.print("."); //You can write 16 Characters per line
  187.   }  
  188. }
  189.  
  190. void FileStart()
  191. {    
  192.   //Serial.println("red"); //do not use serial.print() or digitalwrite() in the attachinterrupt!
  193.   WritingEnabled = true;  
  194. }
  195.  
  196. void FileStop()
  197. {
  198.   //Serial.println("black"); //do not use serial.print() or digitalwrite() in the attachinterrupt!
  199.   WritingEnabled = false;  
  200. }
  201.  
  202.  
  203. void readFile()
  204. {
  205.   //Reading
  206.   //You can read the content of the file and print it on the serial.
  207.   //This is not explained in the tutorial video because I was only focusing on the writing
  208.   file = SD.open("Temp.txt", FILE_READ); // open "file.txt" to read data
  209.   if (file) {
  210.     Serial.println("--- Reading start ---");
  211.     char character;
  212.     while ((character = file.read()) != -1) { // this while loop reads data stored in "file.txt" and prints it to serial monitor
  213.       Serial.print(character);
  214.     }
  215.     file.close();
  216.     Serial.println("--- Reading end ---");
  217.   } else {
  218.     Serial.println("Could not open file (reading).");
  219.   }
  220. }
  221.  
  222. void readADS()
  223. {
  224. //please SUBSCRIBE:  https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  225.   Serial.println(elapsedTime);
  226.  
  227.   analogBit = ads.readADC_SingleEnded(0); //read A0, store it in the analogBit variable
  228.   tempKelvin = (analogBit*0.1875)/10;
  229.   T1 = tempKelvin - 273.15;
  230.   Serial.print("Celsius1: ");
  231.   Serial.println(T1);
  232.  
  233.   analogBit = ads.readADC_SingleEnded(1); //read A1, store it in the analogBit variable
  234.   tempKelvin = (analogBit*0.1875)/10;
  235.   T2 = tempKelvin - 273.15;
  236.   Serial.print("Celsius2: ");
  237.   Serial.println(T2);
  238.  
  239.   analogBit = ads.readADC_SingleEnded(2); //read A2, store it in the analogBit variable
  240.   tempKelvin = (analogBit*0.1875)/10;
  241.   T3 = tempKelvin - 273.15;
  242.   Serial.print("Celsius3: ");
  243.   Serial.println(T3);
  244.  
  245.   analogBit = ads.readADC_SingleEnded(3); //read A3, store it in the analogBit variable
  246.   tempKelvin = (analogBit*0.1875)/10;
  247.   T4= tempKelvin - 273.15;  
  248.   Serial.print("Volt4: ");
  249.   Serial.println(tempKelvin);  
  250.   Serial.print("Celsius4: ");
  251.   Serial.println(T4);
  252.  
  253. }
  254.  
  255.  
  256. //-------------------------------------------------------------------------------------------------------------
  257.  
  258. //Small explanation
  259. //1024 comes from the resolution of the ADC. 2^10 = 1024. If you use different ADC, change the value.
  260. //4876 (mV) comes from the measured voltage on the 5V rail.
  261. //Example: (analogBit/1024.0) * 4876 /10;
  262. //To have precise data, measure the 5V rail when everything is connected to the arduino (screen, modules, etc) and they are running
  263. //use the measured voltage in mV (millivolts) in the formula
  264. // division by 10 comes from the gain of the LM335Z chip which is G=10 mV/K.
  265.  
  266.  
  267. void printLCD()
  268. {
  269.           //-------------LCD Printout------------------
  270.           lcd.clear(); //clear LCD
  271.           lcd.setCursor(0, 0); //Defining position to write from first row,first column .
  272.           lcd.print("1: ");
  273.           lcd.setCursor(2, 0);
  274.           lcd.print(T1, 1);
  275.           lcd.setCursor(7, 0);
  276.           lcd.print("2: ");
  277.           lcd.setCursor(9, 0);
  278.           lcd.print(T2, 1);  
  279.           //-------------------------------------------
  280.           lcd.setCursor(0, 1);
  281.           lcd.print("3: ");
  282.           lcd.setCursor(2, 1);
  283.           lcd.print(T3, 1);
  284.           lcd.setCursor(7, 1);
  285.           lcd.print("4: ");
  286.           lcd.setCursor(9, 1);
  287.           lcd.print(T4, 1);
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement