Konstantin Dimitrov
Published © GPL3+

ThermoClock: An OpenSource Arduino UNO OLED Clock

Today I am going to show you how to make An OpenSource Arduino UNO OLED Clock that also measures temperature.

IntermediateFull instructions provided2 hours14,800

Things used in this project

Story

Read more

Code

Code snippet #1

Plain text
<br><p>/*************LIBRARIES**********************/<br>#include <U8glib.h>      // OLED#include <Wire.h>       // I2C
#include <Time.h>       // Time Manipulation
#include <DS1307RTC.h>  // DS1307 RTC
/*************VARIABLES**********************/
char timebuf[10];       // Time
char datebuf[10];       // Date
int year2digit;         // 2 digit year
int year4digit;         // 4 digit year
int tmp102Address = 0x48;</p><p>const int SwitchTime = 10000; //switching time in millis
/********************************************/
U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9, 8);  // CLK=13, DIN=11, CS=10, DC=9, Reset=8
//
//
//
/****************DRAW FUNCTION***************/
void draw(void) {</p><p>u8g.setFont(u8g_font_fub17r);  // select font
u8g.setPrintPos(30, 28);       // set position
u8g.print(timebuf);            // display time
u8g.drawHLine(0,33,128);       // draw line
u8g.setPrintPos(12, 55);       // set position
u8g.print(datebuf);            // display date
}</p><p>void draw1(void)
{
  float celsius = getTemperature();
  float fahrenheit = (1.8 * celsius) + 32;  
 
  u8g.setFont(u8g_font_fub17r);
  u8g.setPrintPos(15, 28);
  u8g.print(celsius);
  u8g.print(" C");
  u8g.setPrintPos(12, 55);
  u8g.print(fahrenheit);
  u8g.println(" F");
}</p><p>float getTemperature(){
  Wire.requestFrom(tmp102Address,2); </p><p>  byte MSB = Wire.read();
  byte LSB = Wire.read();</p><p>  //it's a 12bit int, using two's compliment for negative
  int TemperatureSum = ((MSB << 8) | LSB) >> 4; </p><p>  float celsius = TemperatureSum*0.0625;
  
 if (celsius > 180)
 {
  celsius = (celsius - 256);  // For negative temperatures
 }
 else 
 {
  celsius;
 }
 return celsius;
 }</p><p>void setup(void) {
}
void loop(void) {</p><p>tmElements_t tm;
if (RTC.read(tm)) 
{
year2digit = tm.Year - 30;  // 2 digit year variable
//year4digit = tm.Year + 1970;  // 4 digit year variable</p><p>sprintf(timebuf, "%02d:%02d",tm.Hour, tm.Minute, tm.Second); // format time
sprintf(datebuf, "%02d/%02d/%02d",tm.Day, tm.Month, year2digit);  // format date
/********************************************/</p><p>u8g.firstPage();  // Put information on OLED
do {
draw(); 
} while( u8g.nextPage()  );
delay(SwitchTime);</p><p>u8g.firstPage();  // Put information on OLED
do {
draw1(); 
} while( u8g.nextPage()  );
}
delay(SwitchTime);  // Delay 
}</p>

Credits

Konstantin Dimitrov

Konstantin Dimitrov

11 projects • 183 followers

Comments