CuriousScientist

4 channel Arduino based temperature logger with DS18B20

Jun 24th, 2020
208
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/q2tXh3CPbZQ
  3.  
  4. #include <OneWire.h>
  5. #include <DallasTemperature.h>
  6.  
  7. #include <SD.h> // SD library
  8.  
  9. #include "SSD1306Ascii.h"
  10. #include "SSD1306AsciiAvrI2c.h"
  11. #define I2C_ADDRESS 0x3C //Address
  12. #define RST_PIN -1 //For OLED with no reset pin
  13.  
  14. // Data wire is plugged into digital pin 5 on the Arduino
  15. #define ONE_WIRE_BUS 5 //The parallel wires are connected to D5
  16.  
  17. // Setup a oneWire instance to communicate with any OneWire device
  18. OneWire oneWire(ONE_WIRE_BUS);  
  19.  
  20. // Pass oneWire reference to DallasTemperature library
  21. DallasTemperature sensors(&oneWire);
  22.  
  23. int deviceCount = 0;
  24.  
  25. float T1, T2, T3, T4; //Variables for the 4 thermometers
  26.  
  27. const byte FileOnOffButton = 10; //toggle switch (ON-ON) for starting and stopping the logging
  28.  
  29. bool WritingEnabled = false; //we switch the status of this with the buttons
  30. bool SwitchStatus = false; //by default, this is false that means that the switch is OFF (no logging)
  31.  
  32. unsigned long startTime;
  33. unsigned long elapsedTime;
  34.  
  35. SSD1306AsciiAvrI2c display;
  36.  
  37. int CS = 4; //chip select pin for the MicroSD Card Adapter, This is the CS Pin
  38. File file; // file object that is used to read and write data
  39.  
  40. void setup(void)
  41. {
  42.   sensors.begin(); // Start up the library
  43.   Serial.begin(9600);
  44.  
  45.   sensors.setResolution(10); //10 bit resolution (0.25°C step)
  46.    
  47.   // locate devices on the bus
  48.   Serial.print("Locating devices...");
  49.   Serial.print("Found ");
  50.   deviceCount = sensors.getDeviceCount();
  51.   Serial.print(deviceCount, DEC);
  52.   Serial.println(" devices.");
  53.   Serial.println("");
  54.  
  55.   //Pin for the switch pins  
  56.   pinMode(FileOnOffButton, INPUT);
  57.  
  58.   //OLED part-----------------------------------------------------------------------------------
  59.   #if RST_PIN >= 0
  60.   display.begin(&Adafruit128x32, I2C_ADDRESS, RST_PIN);
  61.   #else // RST_PIN >= 0
  62.   display.begin(&Adafruit128x32, I2C_ADDRESS);
  63.   #endif // RST_PIN >= 0
  64.   //Call oled.setI2cClock(frequency) to change from the default frequency.
  65.  
  66.   display.setFont(System5x7);
  67.   display.set1X(); //set2x() is too large
  68.   display.clear();
  69.   //--endofOLED----
  70.  
  71.   //SD card module-----------------------------------------------------------------------------------
  72.   pinMode(CS, OUTPUT); // chip select pin is set as OUTPUT
  73.  
  74.   if (!SD.begin(CS))
  75.   { // Initialize SD card
  76.     Serial.println("No SD card found. Reset the device after inserting an SD card."); // if return value is false, something went wrong.
  77.   }
  78.  
  79.   /* //We skip this part
  80.   if (SD.exists("Temp.txt"))
  81.   { // if "Temp.txt" exists, fill will be deleted
  82.     Serial.println("File exists.");
  83.   if (SD.remove("Temp.txt") == true)
  84.   {
  85.       Serial.println("Successfully removed file.");
  86.   }
  87.   else
  88.   {
  89.       Serial.println("Could not removed file.");
  90.   }
  91.   }
  92.   */
  93.  
  94.   //Starting timer for the elapsed time
  95.   startTime = millis();
  96.  
  97. }
  98.  
  99. void loop()
  100. {
  101.   //If you found my video helpful, please SUBSCRIBE:  https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
  102.   ReadSwitchState();
  103.   ReadSensors();
  104.   PrintSerial();
  105.   PrintOLED();
  106.   WriteSD();
  107.  
  108.   delay(300);
  109. }
  110.  
  111. void ReadSensors()
  112. {  
  113.   elapsedTime = millis() - startTime;
  114.  
  115.   //Collect the values for each sensors    
  116.   sensors.requestTemperatures(); //request the temperature
  117.  
  118.   //Filling up the variables
  119.   T1 = sensors.getTempCByIndex(0);  
  120.   T2 = sensors.getTempCByIndex(1);  
  121.   T3 = sensors.getTempCByIndex(2);  
  122.   T4 = sensors.getTempCByIndex(3);        
  123. }
  124.  
  125.  
  126.  
  127. void PrintOLED()
  128. {
  129.  
  130.   //128x32 OLED
  131.   //1st line of the OLED
  132.   display.clear();  
  133.   display.setCursor(0, 0); //The cursor's unit is in pixels and not in blocks as in the case of the 16x2 LCD
  134.   display.print("1: ");
  135.   display.print(T1,1);
  136.   display.print("  2: ");
  137.   display.println(T2,1);
  138.  
  139.   if(WritingEnabled == true)
  140.   {
  141.     display.setCursor(120, 0); //this display has to be positioned by pixel value and not block value!
  142.     display.print("W"); // W = writing is in progress
  143.   }
  144.   else
  145.   {
  146.     display.setCursor(120, 0);
  147.     display.print("0"); // 0 = no writing at the moment    
  148.   }  
  149.  
  150.   //2nd line
  151.   display.setCursor(0, 1);
  152.   display.print("3: ");
  153.   display.print(T3,1);
  154.   display.print("  4: ");
  155.   display.println(T4,1);
  156.  
  157.   //3rd line
  158.   display.setCursor(0, 2);
  159.   display.print("t: ");
  160.   display.print(elapsedTime/1000);  
  161.   display.print(" s");
  162.  
  163. }
  164.  
  165. void PrintSerial()
  166. {
  167.   Serial.print(elapsedTime); //time in ms
  168.   Serial.print(" ");  //space
  169.   Serial.print(T1,2); //temperature, 2 digits (i.e. 28.12)
  170.   Serial.print(" ");  
  171.   Serial.print(T2,2);
  172.   Serial.print(" ");  
  173.   Serial.print(T3,2);
  174.   Serial.print(" ");  
  175.   Serial.println(T4,2);
  176. }
  177.  
  178. void WriteSD()
  179. {
  180.   if(WritingEnabled == true)
  181.   {
  182.     file = SD.open("Temp.txt", FILE_WRITE); // open "Temp.txt" to write data; make sure that you want to write in the same file that you created in the setup()
  183.     file.seek(EOF); //This goes to the End Of (the) File and _continues_ writing from there
  184.     if (file)
  185.     {
  186.       file.print(elapsedTime);
  187.       file.print(" ");
  188.       file.print(T1);
  189.       file.print(" ");
  190.       file.print(T2);
  191.       file.print(" ");
  192.       file.print(T3);
  193.       file.print(" ");
  194.       file.print(T4);
  195.       file.println(" ");      
  196.       file.close(); // close file      
  197.       Serial.println("Success"); //we print on the serial port, so we see that the writing to SD was OK. You can remove this
  198.     } else {
  199.       Serial.println("Could not open file (writing).");
  200.     }  
  201.   }
  202.   else
  203.   {
  204.   //  
  205.   }  
  206. }
  207.  
  208. void ReadSwitchState()
  209. {
  210. if(digitalRead(FileOnOffButton) == HIGH) //if the button is high
  211. {
  212.   if(SwitchStatus == false) //if the previous status was false, we restart the timer to 0 by resetting the startTime.
  213.   {
  214.     SwitchStatus = true; //flip the state
  215.     startTime = millis(); //reset the timer to zero
  216.   }
  217.   WritingEnabled = true; //enable the writing
  218.   Serial.println("ON"); //message for checking the things out
  219. }
  220. else //in this case, the FileOnOffButton is LOW, which means, we don't write the SD card
  221. {
  222.   SwitchStatus = false; //Switch status is off
  223.   WritingEnabled = false; //we do not write on the SD card
  224.   Serial.println("OFF"); //message on the serial, to see what is happening
  225. }  
  226. }
  227. //If you found my video helpful, please SUBSCRIBE:  https://www.youtube.com/c/CuriousScientist?sub_confirmation=1
Add Comment
Please, Sign In to add comment