Advertisement
Double_G

OLEDLOFASZ

Apr 12th, 2022
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. // Internal Temperature Sensor
  2. // Example sketch for ATmega328 types.
  3. //
  4. // https://playground.arduino.cc/Main/InternalTemperatureSensor/
  5. //
  6. // April 2012, Arduino 1.0
  7.  
  8. #include <U8g2lib.h>
  9. U8G2_SSD1306_128X32_UNIVISION_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
  10.  
  11. void setup()
  12. {
  13. Serial.begin(9600);
  14.  
  15. Serial.println(F("Internal Temperature Sensor"));
  16. u8g2.begin();
  17. u8g2.clearBuffer();
  18. u8g2.setDrawColor(1);
  19. u8g2.setFontPosTop();
  20. u8g2.setFontDirection(2);
  21. u8g2.setFont(u8g2_font_timB12_tr);
  22. u8g2.drawStr(127,32, "NAGYLOFASZ");
  23. u8g2.sendBuffer();
  24. delay(2000);
  25. }
  26.  
  27. void loop()
  28. {
  29. // Show the temperature in degrees Celsius.
  30. Serial.println(GetTemp(),1);
  31. u8g2.clearBuffer();
  32. u8g2.setDrawColor(1);
  33. u8g2.setFontPosTop();
  34. u8g2.setFontDirection(2);
  35. u8g2.setFont(u8g2_font_timB12_tr);
  36. u8g2.setCursor (127,32);
  37. u8g2.print(GetTemp(),1);
  38. u8g2.sendBuffer();
  39. delay(1000);
  40. }
  41.  
  42. double GetTemp(void)
  43. {
  44. unsigned int wADC;
  45. double t;
  46.  
  47. // The internal temperature has to be used
  48. // with the internal reference of 1.1V.
  49. // Channel 8 can not be selected with
  50. // the analogRead function yet.
  51.  
  52. // Set the internal reference and mux.
  53. ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));
  54. ADCSRA |= _BV(ADEN); // enable the ADC
  55.  
  56. delay(20); // wait for voltages to become stable.
  57.  
  58. ADCSRA |= _BV(ADSC); // Start the ADC
  59.  
  60. // Detect end-of-conversion
  61. while (bit_is_set(ADCSRA,ADSC));
  62.  
  63. // Reading register "ADCW" takes care of how to read ADCL and ADCH.
  64. wADC = ADCW;
  65.  
  66. // The offset of 324.31 could be wrong. It is just an indication.
  67. t = (wADC - 324.31 ) / 1.22;
  68.  
  69. // The returned temperature is in degrees Celsius.
  70. return (t);
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement