Advertisement
cisco404

C++ | Thermometer using Thermocouple Type-K with Arduino Uno

Dec 21st, 2020 (edited)
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | Source Code | 0 0
  1. #include "U8glib.h"
  2. #include <SPI.h>
  3. #include <Wire.h>
  4. #include "max6675.h"
  5.  
  6. // -------------------------------------------
  7. // Thermometer using Thermocouple Type-K with Arduino Uno
  8. // www.ardukode.blogspot.com
  9. // -------------------------------------------
  10.  
  11. boolean centigrade = true; //Uncomment for Centigrade and Comment the  line below
  12. //  boolean centigrade = false; // Uncomment for Fahrenheit and Comment the line above
  13.  
  14. // setup u8g object
  15. U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C
  16.  
  17. double max = 215; // Maximum temperature
  18. double min = -215; // Minimum temperature
  19. float currentTemp = 0.00;
  20. String thisTemp = "";
  21. int maxTemp = 0; // Maximum temperature reached
  22. int minTemp = 0; // Minimum temperature reached
  23. int pad = 0;
  24.  
  25. // Thermocouple MAX 6675 Module
  26. int thermoSO = 11;
  27. int thermoCS = 12;
  28. int thermoSCK = 13;
  29. MAX6675 thermocouple(thermoSCK, thermoCS, thermoSO);
  30. int vccPin = 20; // Vcc pin for MAX6675 module = D3
  31. int gndPin = 22; // Gnd pin for MAX6675 = D2
  32.  
  33. void draw(void) {
  34.   u8g.setFont(u8g_font_profont12);
  35.   u8g.drawStr(31, 10, "Temperature");
  36.   u8g.setFont(u8g_font_profont12);
  37.   // Show maximum temperature reached
  38.   u8g.drawStr(10, 25, "max");
  39.   if (maxTemp <= int(currentTemp)) {
  40.     maxTemp = int(currentTemp);
  41.   }
  42.   thisTemp = String(maxTemp);
  43.   if (centigrade) {
  44.     thisTemp = thisTemp + "\260C";
  45.   }
  46.   else {
  47.     thisTemp = thisTemp + "\260F";
  48.   }
  49.   const char* maxTempC = (const char*) thisTemp.c_str();
  50.   u8g.drawStr(30, 25, maxTempC);
  51.   // Show the minimum temperature reached
  52.   u8g.drawStr(80, 25, "min");
  53.   if (minTemp >= int(currentTemp)) {
  54.     minTemp = int(currentTemp);
  55.   }
  56.   thisTemp = String(minTemp);
  57.   if (centigrade) {
  58.     thisTemp = thisTemp + "\260C";
  59.   }
  60.   else {
  61.     thisTemp = thisTemp + "\260F";
  62.   }
  63.   const char* minTempC = (const char*) thisTemp.c_str();
  64.   u8g.drawStr(100, 25, minTempC);
  65.   u8g.setFont(u8g_font_profont29);
  66.   if (currentTemp > 99) {
  67.     pad = 2;
  68.   }
  69.   if (currentTemp > 9 && currentTemp < 100) {
  70.     pad = 10;
  71.   }
  72.   if (currentTemp < 10) {
  73.     pad = 18;
  74.   }
  75.   thisTemp = String(currentTemp);
  76.   if (centigrade) {
  77.     thisTemp = thisTemp + "\260C";
  78.   }
  79.   else {
  80.   }
  81.   const char* newDispC = (const char*) thisTemp.c_str();
  82.   u8g.drawStr(pad, 50, newDispC);
  83. }
  84.  
  85. void setup(void) {
  86.   pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH); // D3 @5V/Vcc
  87.   pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW); // D2@0V/GND
  88.   Serial.begin(9600); // Serial Begin
  89.   Wire.begin();
  90.   delay(500); // Stabilization Time
  91.   if (centigrade) {
  92.     currentTemp = thermocouple.readCelsius();
  93.     minTemp = int(thermocouple.readCelsius());
  94.     maxTemp = int(thermocouple.readCelsius());
  95.   }
  96.   else {
  97.     currentTemp = thermocouple.readFahrenheit();
  98.     minTemp = int(thermocouple.readFahrenheit());
  99.     maxTemp = int(thermocouple.readFahrenheit());
  100.   }
  101. }
  102. void loop(void) {
  103.   currentTemp = 0;
  104.   for (int f = 0; f < 25; f++) {
  105.     if (centigrade) {
  106.       currentTemp = thermocouple.readCelsius() + currentTemp;
  107.     }
  108.     else {
  109.       currentTemp = thermocouple.readFahrenheit() + currentTemp;
  110.     }
  111.   }
  112.   currentTemp = currentTemp / 25; //Average 25 readings
  113.   // Draw
  114.   u8g.firstPage();
  115.   do {
  116.     draw();
  117.   } while ( u8g.nextPage() );
  118.   delay(50); // Refresh Time
  119. }
  120. // avrdude -DV -patmega328p -Pnet:192.168.4.1:23 -carduino -b115200 -U flash:w:firmware.hex:i
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement