Advertise here

Teensy RTC and adafruit 3.5 inch TFT Display

Teensy RTC and adafruit 3.5 inch TFT Display







in this quick tutorial , you will find a quick guide to hookup a 3.5 HX8357 TFT LCD display from adafruit and Teensy 3.1/3.2 board (and will work with 3.5/3.6 as well).

I want to display a time and date on the LCD and update the value every 1 second , the biggest challenge for me was a flickering, if you update the display by re-write the entire data every 1 second  the display will flicker the time and the date and it will not be comfort for your eyes.



so I will share with you my code and you can re-use it and re-write it to match your application 

what you need :

1-TFT LCD 3.5 Inch from adafruit , you can buy it from Here " and need to read the article about it from here since you need to solder and jumper some pads to enable SPI mode".

source : adafruit.com

teensy Board , I used 3.1 And 3.2 to test the code.

source : adafruit.com

schematic and wiring 









Code 

you can download the code from Here 

you will need to download adafruitGFX library and and HX8357 Library




/*
 * display Time and Date on 3.5 inch adafruit TFT display   
 * I used Teensy 3.1 / 3.2 board to test this code , you
 * can make it working for any other board easily
 * you can find the component for this example :
 * ***** TFT 3.5 Inch display
 * https://www.adafruit.com/product/2050
 * 
 * ** teensy 3.2 board
 * https://www.adafruit.com/product/2756
 * 
 * written By: Mohannad Rawashdeh
 * https://mb-raw.blogspot.com/2018/05/teensy-rtc-and-adafruit-35-inch-tft.html
 */

#include <TimeLib.h>
#include <Time.h> 
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_HX8357.h"

// These are 'flexible' lines that can be changed
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8 // RST can be set to -1 if you tie it to Arduino's reset
#define FontSize 7
#define DateFontSize 5
#define HOUR 3
#define MINUTE 2
#define SECOND 1
#define DAY 4
#define MONTH 5
#define YEAR 6

unsigned int Hour_X_POS=80;
unsigned int Hour_Y_POS=80;
unsigned int DOT1_X_POS=Hour_X_POS+(FontSize*11)+6;
unsigned int DOT1_Y_POS=Hour_Y_POS;
unsigned int Min_X_POS =DOT1_X_POS+(FontSize*7);
unsigned int Min_Y_POS=Hour_Y_POS;
unsigned int DOT2_X_POS=Min_X_POS+(FontSize*11)+8;
unsigned int DOT2_Y_POS=Hour_Y_POS;
unsigned int Sec_X_POS=DOT2_X_POS+(FontSize*7);
unsigned int Sec_Y_POS=Hour_Y_POS;

unsigned int Day_X_POS=65;
unsigned int Day_Y_POS=200;
unsigned int SLASH1_X_POS=Day_X_POS+(DateFontSize*15);
unsigned int SLASH1_Y_POS=Day_Y_POS;
unsigned int Mon_X_POS=SLASH1_X_POS+(DateFontSize*11);
unsigned int Mon_Y_POS=Day_Y_POS;
unsigned int SLASH2_X_POS=Mon_X_POS+(DateFontSize*16);
unsigned int SLASH2_Y_POS=Day_Y_POS;
unsigned int Year_X_POS=SLASH2_X_POS+(DateFontSize*11);
unsigned int Year_Y_POS=Day_Y_POS;

 
// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);
bool Blinky=false;
 int UpdateHour=-1; int UpdateMin=-1; int UpdateSec=-1;
 int UpdateDay=-1; int UpdateMonth=-1; int UpdateYear=-1;
 int Teensy_Hour,Teensy_minute,Teensy_Second,Teensy_Day,Teensy_Month,Teensy_Year;
void setup()  {
  setSyncProvider(getTeensy3Time);
  Serial.begin(115200);
  delay(3000);
  delay(100);
    if (timeStatus()!= timeSet) {
    Serial.println("Unable to sync with the RTC");
    setTime(4,30,0,1,1,18);
  } else {
    Serial.println("RTC has set the system time");
  }
   tft.begin(HX8357D);
   tft.setRotation(3);
   tft.fillScreen(HX8357_BLACK);
   tft.setCursor(50, 20);
   tft.setTextSize(3);
   tft.setTextColor(HX8357_RED);
   tft.println("Time Now (+3GMT)");
   tft.setTextColor(HX8357_WHITE);
   tft.setTextSize(FontSize);
   UpdateDot();
}

void loop() 
{
  //digitalClockDisplay();
  upd_time();
  if(UpdateHour != Teensy_Hour)
  {
    UpdateHour=Teensy_Hour;
    UpdateDisplay(HOUR);
  }
  if(UpdateMin != Teensy_minute)
  {
    UpdateMin=Teensy_minute;
    UpdateDisplay(MINUTE);
  }  
    if(UpdateSec != Teensy_Second)
  {
    UpdateSec=Teensy_Second;
    UpdateDisplay(SECOND);
  }
   if(UpdateDay != Teensy_Day)
  {
    UpdateDisplay(DAY);
    UpdateDay=Teensy_Day;
    if(UpdateMonth != Teensy_Month)
    {
      UpdateMonth = Teensy_Month;
    }    
    if(UpdateYear != Teensy_Year)
    {
      UpdateYear = Teensy_Year;
    }
  }     
  delay(50);
}

void UpdateDot()
{
         tft.setTextColor(HX8357_WHITE);
         tft.setCursor(DOT1_X_POS, DOT1_Y_POS);
         tft.print(":");
         tft.setCursor(DOT2_X_POS, DOT2_Y_POS);
         tft.print(":");
}
void UpdateDisplay(unsigned int Element)
{
     tft.setTextColor(HX8357_WHITE);
     tft.setTextSize(FontSize);
     if(Element==SECOND)
    {
         tft.setTextColor(HX8357_BLACK);
         tft.setCursor(Sec_X_POS, Sec_Y_POS);
         if(Teensy_Second==0){tft.print("59");}
         if(Teensy_Second<=10){tft.print("0");tft.print(Teensy_Second-1);}
         else{tft.print(Teensy_Second-1);}       
         tft.setTextColor(HX8357_WHITE);
         tft.setCursor(Sec_X_POS, Sec_Y_POS);
         if(Teensy_Second<10){tft.print("0");tft.print(Teensy_Second);}
         else{tft.print(Teensy_Second);}
    }
    if(Element==MINUTE)
    {
         tft.setTextColor(HX8357_BLACK);
         tft.setCursor(Min_X_POS, Min_Y_POS);
         if(Teensy_minute==0){tft.print("59");}
         if(Teensy_minute<=10){tft.print("0");tft.print(Teensy_minute-1);}
         else{tft.print(Teensy_minute-1);} 
         tft.setTextColor(HX8357_WHITE);
         tft.setCursor(Min_X_POS, Min_Y_POS);
         if(Teensy_minute<10){tft.print("0"); tft.print(Teensy_minute);}
         else{tft.print(Teensy_minute);}
         UpdateDot(); // optional , you can remove it
    }
    if(Element==HOUR)
    {
         tft.setTextColor(HX8357_BLACK);
         tft.setCursor(Hour_X_POS, Hour_Y_POS);
         if(Teensy_Hour==0){tft.print("23");}
         if(Teensy_Hour<=10){tft.print("0");tft.print(Teensy_Hour-1);}
         else{tft.print(Teensy_Hour-1);}
         tft.setTextColor(HX8357_WHITE);
         tft.setCursor(Hour_X_POS, Hour_Y_POS);
         if(Teensy_Hour<10){tft.print("0"); tft.print(Teensy_Hour);}
         else{tft.print(Teensy_Hour);}
    }

    if(Element==DAY)
    {
        tft.setTextSize(DateFontSize);
         tft.setTextColor(HX8357_BLACK);
         tft.setCursor(Day_X_POS, Day_Y_POS);
         if(Teensy_Day==0){tft.print(UpdateDay);}
         if(Teensy_Day<=10){tft.print("0");tft.print(Teensy_Day-1);}
         else{tft.print(Teensy_Day-1);}
         tft.setTextColor(HX8357_WHITE);
         tft.setCursor(Day_X_POS, Day_Y_POS);
         if(Teensy_Day<10){tft.print("0"); tft.print(Teensy_Day);}
         else{tft.print(Teensy_Day);}
         
         tft.setTextColor(HX8357_BLACK);
         tft.setCursor(Mon_X_POS, Mon_Y_POS);
         if(Teensy_Month==0){tft.print(UpdateMonth);}
         if(Teensy_Month<=10){tft.print("0");tft.print(Teensy_Month-1);}
         else{tft.print(Teensy_Month-1);}
         tft.setTextColor(HX8357_WHITE);
         tft.setCursor(Mon_X_POS, Mon_Y_POS);
         if(Teensy_Month<10){tft.print("0"); tft.print(Teensy_Month);}
         else{tft.print(Teensy_Month);}

         tft.setTextColor(HX8357_BLACK);
         tft.setCursor(Year_X_POS, Year_Y_POS);
         tft.print(UpdateYear);
         tft.setTextColor(HX8357_WHITE);
         tft.setCursor(Year_X_POS, Year_Y_POS);
         tft.print(Teensy_Year);
         
         tft.setCursor(SLASH1_X_POS, SLASH1_Y_POS);
         tft.setTextColor(HX8357_WHITE);
         tft.print("/");
         tft.setCursor(SLASH2_X_POS, SLASH2_Y_POS);
         tft.setTextColor(HX8357_WHITE);
         tft.print("/");
         
         }
    }    

time_t getTeensy3Time()
{
  return Teensy3Clock.get();
}


 void upd_time()
 {
  Teensy_Hour=hour();
  Teensy_minute=minute();
  Teensy_Second=second();
  Teensy_Day=day();
  Teensy_Month=month();
  Teensy_Year=year();
 }



and this video Will show you the results



Support me and Follow me on:



No comments

Powered by Blogger.