Mike86
Published

DIY a Clock that can Detect Temperature and Humidity

This is a tutorial about using a TFT LCD Touch Screen to make a clock that can show temperature and humidity at the same time.

BeginnerFull instructions provided30 minutes1,745
DIY a Clock that can Detect Temperature and Humidity

Things used in this project

Hardware components

Iteaduino Derivative Arduino MEGA2560 ATMega2560 Pins Board
×1
Itead IIC EEPROM AND RTC Module
×1
Itead Electronic Brick - DHT11 Humidity Temperature Sensor Brick
×1
Itead UNO Mega 2.8 TFT LCD Touch Display Shield With SD Card Socket For Arduino Board Mudle
×1
CR2032 button cell
×1
9V power adapter
×1
Dupont lines
×1

Story

Read more

Schematics

Schematic for Iteaduino MEGA 2560

Schematic for IIC EEPROM AND RTC Module

Schematic for DHT11 Humidity Temperature Sensor Brick

Schematic for 2.8 TFT LCD Touch Display Shield

Code

Display Demo

C/C++
// Connetion:
// DS1307:  SDA pin   -> Arduino Digital 22
//          SCL pin   -> Arduino Digital 23
//          VCC pin   -> 5V
//          GND pin   -> GND
// DHT11    S pin     -> D25
//          V pin     -> 5V
//          G pin     -> GND


#include <DS1307.h>
#include <EEPROM.h>

#include <UTFT.h>
#include <avr/pgmspace.h>

UTFT myGLCD(ITDB28,A5,A4,A3,A2);
extern unsigned int itead[0x28A0];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];

// Init the DS1307
DS1307 rtc(22, 23);

#define eeprom_address 	0x00
#define eeprom_value 	0x01

int DHT11PIN=25;

int humidity;
int temperature;

char str_hum[10];
char str_temp[10];

unsigned long start_millis;

void setup()
{
  // Set the clock to run-mode
  rtc.halt(false);
  
  byte value;
  value = EEPROM.read(eeprom_address);
  if(value != eeprom_value)
  {
    EEPROM.write(eeprom_address, eeprom_value);
  // The following lines can be commented out to use the values already stored in the DS1307
  rtc.setDOW(THURSDAY);        // Set Day-of-Week to THURSDAY
  rtc.setTime(15, 17, 0);     // Set the time to 15:17:00 (24hr format)
  rtc.setDate(25, 9, 2014);   // Set the date to September 9th, 2014
  }
  myGLCD.InitLCD();
  myGLCD.clrScr();
  //myGLCD.fillScr(255, 255, 255);  
  myGLCD.setColor(0, 0, 0);
  myGLCD.setFont(BigFont);
  myGLCD.drawBitmap (319, 68, 100, 104, itead,90,0,0);
  myGLCD.setColor(0, 0, 255);
  myGLCD.print("Temperature:", 200, 0,90);
  myGLCD.print("^C", 175, 150,90);
  
  
  myGLCD.print("Humidity:", 150, 0,90);
  myGLCD.print("%", 125, 150,90);
  
  myGLCD.setColor(255, 0, 0);
  start_millis = millis();
}

void loop()
{
  if(millis()-start_millis>1000)
  {
    start_millis = millis();
  // Send Day-of-Week

  myGLCD.print(rtc.getDOWStr(FORMAT_SHORT), 20, 0,90);
  
  // Send date

  myGLCD.print(rtc.getDateStr(), 20, 80,90);
  // Send time


  myGLCD.print(rtc.getTimeStr(), 50, 50,90);

  //read dht11
  int chk = dht11_read(DHT11PIN);

  if(chk==0)
  {
    itoa(humidity,str_hum,10);
    itoa(temperature,str_temp,10);
    //myGLCD.setColor(255, 255, 255);
    myGLCD.print("  ", 175, 80,90);
    //myGLCD.setColor(255, 255, 255);
    myGLCD.print("  ", 125, 80,90);
    //myGLCD.setColor(255, 0, 0);
    myGLCD.print(str_temp, 175, 80,90);
    myGLCD.print(str_hum, 125, 80,90);
    
  }
  

  }
  
  
}

int dht11_read(int pin)
{
	// BUFFER TO RECEIVE
	uint8_t bits[5];
	uint8_t cnt = 7;
	uint8_t idx = 0;

	// EMPTY BUFFER
	for (int i=0; i< 5; i++) bits[i] = 0;

	// REQUEST SAMPLE
	pinMode(pin, OUTPUT);
	digitalWrite(pin, LOW);
	delay(18);
	digitalWrite(pin, HIGH);
	delayMicroseconds(40);
	pinMode(pin, INPUT);

	// ACKNOWLEDGE or TIMEOUT
	unsigned int loopCnt = 10000;
	while(digitalRead(pin) == LOW)
		if (loopCnt-- == 0) return -2;

	loopCnt = 10000;
	while(digitalRead(pin) == HIGH)
		if (loopCnt-- == 0) return -2;

	// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
	for (int i=0; i<40; i++)
	{
		loopCnt = 10000;
		while(digitalRead(pin) == LOW)
			if (loopCnt-- == 0) return -2;

		unsigned long t = micros();

		loopCnt = 10000;
		while(digitalRead(pin) == HIGH)
			if (loopCnt-- == 0) return -2;

		if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
		if (cnt == 0)   // next byte?
		{
			cnt = 7;    // restart at MSB
			idx++;      // next byte!
		}
		else cnt--;
	}

	// WRITE TO RIGHT VARS
        // as bits[1] and bits[3] are allways zero they are omitted in formulas.
	humidity    = bits[0]; 
	temperature = bits[2]; 

	uint8_t sum = bits[0] + bits[2];  

	if (bits[4] != sum) return -1;
	return 0;
}

Ticking clock demo

C/C++
// UTFT_Bitmap (C)2013 Henning Karlsen
// web: http://www.henningkarlsen.com/electronics
//
// This program is a demo of the drawBitmap()-function.
//
// This demo was made to work on the 320x240 modules.
// Any other size displays may cause strange behaviour.
//
// This program requires the UTFT library.
//
#include <DS1307.h>
#include <UTFT.h>
#include <EEPROM.h>

#define eeprom_address 	0x00
#define eeprom_value 	0x01

// Init the DS1307
DS1307 rtc(22, 23);

// Declare which fonts we will be using
extern uint8_t BigFont[];

UTFT myGLCD(TFT01_24_8,A5,A4,A3,A2);   // Remember to change the model parameter to suit your display module!

unsigned long starttime;

int sxi;
int syi;
int mxi;
int myi;
int hxi;
int hyi;

int DHT11PIN=25;

int humidity;
int temperature;
int temp_humidity;
int temp_temperature;
int sectime=1;

char str_hum[10];
char str_temp[10];
unsigned long start_millis;

Time ds1307_time;

uint8_t	temp_hour=0xFF;
uint8_t	temp_min=0xFF;
uint8_t	temp_sec=0xFF;
uint8_t	temp_date=0xFF;
uint8_t	temp_mon=0xFF;
uint16_t temp_year=0xFF;
uint8_t	temp_dow=0xFF;

uint8_t	format_hour=0xFF;
uint8_t	temp_format_hour=0x00;




void setup()
{
  byte value;
  value = EEPROM.read(eeprom_address);
  if(value != eeprom_value)
  {
    EEPROM.write(eeprom_address, eeprom_value);
  // The following lines can be commented out to use the values already stored in the DS1307
  rtc.setDOW(THURSDAY);        // Set Day-of-Week to THURSDAY
  rtc.setTime(15, 17, 0);     // Set the time to 15:17:00 (24hr format)
  rtc.setDate(25, 9, 2014);   // Set the date to September 9th, 2014
  }
  myGLCD.InitLCD(LANDSCAPE);
  myGLCD.fillScr(0, 0, 0);
  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(BigFont);
  
  myGLCD.fillCircle(200,120,119);
  myGLCD.setColor(0, 0, 0);
  myGLCD.fillCircle(200,120,116);

  
  myGLCD.setColor(255, 255, 255);
  for(int a=0;a<12;a++)
  {
    int x1,x2,y1,y2;
    x1 = 200+cos(PI*0-PI*a/6)*105;
    x2 = 200+cos(PI*0-PI*a/6)*117;
    y1 = 120-sin(PI*0-PI*a/6)*105;
    y2 = 120-sin(PI*0-PI*a/6)*117;
    
    myGLCD.drawLine(x1,y1,x2,y2);
  }
  for(int a=0;a<60;a++)
  {
    int x1,x2,y1,y2;
    x1 = 200+cos(PI*0-PI*a/30)*112;
    x2 = 200+cos(PI*0-PI*a/30)*117;
    y1 = 120-sin(PI*0-PI*a/30)*112;
    y2 = 120-sin(PI*0-PI*a/30)*117;
    
    myGLCD.drawLine(x1,y1,x2,y2);
  }  
  myGLCD.setColor(0, 255, 0);
  myGLCD.print("^C", 50, 40,90);
  myGLCD.print("%", 50, 225,90);
  myGLCD.setColor(255, 0, 127);
  myGLCD.print("ITEAD Clock", 75, 35,90);
  start_millis = millis();
  rtc.halt(false);
}

void loop()
{

      if(millis()-start_millis>1000)
  {
    start_millis = millis();
  // Send Day-of-Week
  ds1307_time =rtc.getTime();

      myGLCD.setColor(0, 0, 0);
      
      sxi = 200+cos(PI*0-PI*temp_sec/30)*90;
      syi = 120-sin(PI*0-PI*temp_sec/30)*90;
      myGLCD.drawLine(200, 120, sxi, syi);
      temp_sec = ds1307_time.sec;
      myGLCD.setColor(0, 0, 255);
      sxi = 200+cos(PI*0-PI*temp_sec/30)*90;
      syi = 120-sin(PI*0-PI*temp_sec/30)*90;
      myGLCD.drawLine(200, 120, sxi, syi);
      if(temp_min != ds1307_time.min)
      {      
      myGLCD.setColor(0, 0, 0);      
      mxi = 200+cos(PI*0-PI*temp_min/30)*70;
      myi = 120-sin(PI*0-PI*temp_min/30)*70;
      myGLCD.drawLine(200, 120, mxi, myi);
      temp_min = ds1307_time.min;
      myGLCD.setColor(0, 255, 0);
      mxi = 200+cos(PI*0-PI*temp_min/30)*70;
      myi = 120-sin(PI*0-PI*temp_min/30)*70;
      myGLCD.drawLine(200, 120, mxi, myi);        
        
      }
      else
      {
      myGLCD.setColor(0, 255, 0);
      mxi = 200+cos(PI*0-PI*temp_min/30)*70;
      myi = 120-sin(PI*0-PI*temp_min/30)*70;
      myGLCD.drawLine(200, 120, mxi, myi);           
      }
      if(temp_hour != ds1307_time.hour)
      {
      if(temp_hour>12)
      {
        format_hour =1;
      }
      else
      {
        format_hour =0;
      }
      myGLCD.setColor(0, 0, 0);      
      hxi = 200+cos(PI*0-PI*temp_hour/6)*50;
      hyi = 120-sin(PI*0-PI*temp_hour/6)*50;
      myGLCD.drawLine(200, 120, hxi, hyi);
      temp_hour = ds1307_time.hour;
      myGLCD.setColor(255, 0, 0);
      hxi = 200+cos(PI*0-PI*temp_hour/6)*50;
      hyi = 120-sin(PI*0-PI*temp_hour/6)*50;
      myGLCD.drawLine(200, 120, hxi, hyi);        
        
      }
      else
      {
      myGLCD.setColor(255, 0, 0);
      hxi = 200+cos(PI*0-PI*temp_hour/6)*50;
      hyi = 120-sin(PI*0-PI*temp_hour/6)*50;
      myGLCD.drawLine(200, 120, hxi, hyi);            
      }
      
    myGLCD.setColor(0, 255, 0);      
      if(temp_dow != ds1307_time.dow)  
    {    
        temp_dow = ds1307_time.dow;
        myGLCD.print(rtc.getDOWStr(FORMAT_SHORT), 20, 0,90);
        
    }

  // Send date
      if(temp_date != ds1307_time.date)  
    {    
        temp_date = ds1307_time.date;
        myGLCD.print(rtc.getDateStr(), 20, 80,90);
    }
  // Send time

  if(temp_format_hour!=format_hour)
  {
   temp_format_hour=format_hour;
   if(temp_format_hour==0)
    myGLCD.print("AM", 50, 108,90);
    else if(temp_format_hour==1)
    myGLCD.print("PM", 50, 108,90);

  }

  //read dht11
  int chk = dht11_read(DHT11PIN);

  if(chk==0)
  {
    if(temp_humidity !=humidity)
    {
    temp_humidity = humidity;
    itoa(humidity,str_hum,10);
    //myGLCD.print("  ", 50, 0,90);
    myGLCD.print(str_temp, 50, 0,90);
    }
    if(temp_temperature !=temperature)
    {
    temp_humidity = temperature;
    itoa(temperature,str_temp,10);
    //myGLCD.print("  ", 50, 180,90);
    myGLCD.print(str_hum, 50, 180,90);
    }
    
    
  }
  

  } 

  
}


int dht11_read(int pin)
{
	// BUFFER TO RECEIVE
	uint8_t bits[5];
	uint8_t cnt = 7;
	uint8_t idx = 0;

	// EMPTY BUFFER
	for (int i=0; i< 5; i++) bits[i] = 0;

	// REQUEST SAMPLE
	pinMode(pin, OUTPUT);
	digitalWrite(pin, LOW);
	delay(18);
	digitalWrite(pin, HIGH);
	delayMicroseconds(40);
	pinMode(pin, INPUT);

	// ACKNOWLEDGE or TIMEOUT
	unsigned int loopCnt = 10000;
	while(digitalRead(pin) == LOW)
		if (loopCnt-- == 0) return -2;

	loopCnt = 10000;
	while(digitalRead(pin) == HIGH)
		if (loopCnt-- == 0) return -2;

	// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
	for (int i=0; i<40; i++)
	{
		loopCnt = 10000;
		while(digitalRead(pin) == LOW)
			if (loopCnt-- == 0) return -2;

		unsigned long t = micros();

		loopCnt = 10000;
		while(digitalRead(pin) == HIGH)
			if (loopCnt-- == 0) return -2;

		if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
		if (cnt == 0)   // next byte?
		{
			cnt = 7;    // restart at MSB
			idx++;      // next byte!
		}
		else cnt--;
	}

	// WRITE TO RIGHT VARS
        // as bits[1] and bits[3] are allways zero they are omitted in formulas.
	humidity    = bits[0]; 
	temperature = bits[2]; 

	uint8_t sum = bits[0] + bits[2];  

	if (bits[4] != sum) return -1;
	return 0;
}

Credits

Mike86

Mike86

15 projects • 11 followers

Comments