Hey everyone,
Sorry I have not posted in a while. Today, I will be presenting you my Analog and Digital Clock on Adafruit TFT Display using Arduino. You can find the schematic and code explanation within this post. Read on further to learn how I did this project.
Hardware components used in this project
- Arduino Mega 2560
- Adafruit 2.8" TFT Touch Shield for Arduino w/ Capacitive Touch - You could also use any other Arduino compatible TFT display
- DS1307 RTC Module
- Solderless Breadboard - Half+ (This is optional if you are connecting the RTC module to the development board directly)
- Jumper wires (x4) - Male/Male jumper wires if using solderless breadboard. Female/Male if connecting RTC module directly to Arduino Mega.
Software apps
- Arduino IDE - latest version is recommended
Setup
Connections
DS1307 RTC Module
- SCL - A5
- SDA - A4
- VCC - 5V
- GND - Ground
Adafruit TFT Touch shield
You can simply place the Adafruit TFT Touch shield onto your Arduino Mega 2560 development board.
Make sure to include these in your code:
- TFT Data Command pin - D9
- TFT Chip Select pin - D10
Coding
For this sketch, you will need the following libraries:
- SPI - Needed for display
- Adafruit_GFX - Core graphics library (Adafruit's guide to using this library)
- Adafruit_ILI9341 - Library for ILI9341 displays
- Adafruit_SPIFlash - For FAT filesystems on SPI flash chips
- Adafruit_ImageReader - Loading .BMP images from an SD card (Adafruit's guide to using this library)
- Adafruit_FT6206 - This is a library for the Adafruit FT6206-Based capacitive touch screens and displays.
- RTClib - Needed for RTC module
- Wire - This library allows you to communicate with I2C / TWI devices.
The SPI and Wire libraries are inbuilt so you do not have to download them.
Refer to each library's documentation to learn about the functions.
Create tft instance using the Adafruit_ILI9341 class. Use this object to draw two rounded rectangles. Please refer to this guide about drawing pixels using the Adafruit GFX library.
One rounded rectangle should be labelled as 'Analog' while the other should be labelled as 'Digital'.
This project uses the same technique followed in the CapTouch_onoffbutton example sketch from the Adafruit FT6206 library. Make sure to write down the coordinates located within the rounded rectangles.
Analog Clock
You will need some basic trigonometry knowledge to calculate the coordinates of your 'clock hands'.
You will be needing the sin and cos functions for this sketch and remember that the angles should be in radians.
To convert degrees to radians, multiply the angle (in degrees) by pi and divide it by 180.
There are 60 minutes in an hour and 60 seconds in a minute so the angle for each minute and second are: 360/60 = 6 degrees.
The hour hand travels 360 degrees around the clock face and the angle between each hour is : 360/12 = 30 degrees.
Draw a white circle to denote the clock face.
The radius of the circle should be used with sine and cosine functions and the result should be added to the coordinates denoting the centre of the circle to calculate the coordinates where the clock hands should point to.
angle = a
height = h
width = w
centre of circle = (x,y)
hands = (x1,y1)
sin (a) = w/ radius
cos (a) = h/ radius
w = sin(a) x radius
h = cos (a) x radius
x1 = x + w
y1 = y - h
You should decide the radius for each clock hand. The minute hand is the shortest and the second hand is the longest.
Digital Clock
Displaying the digital clock is quite easy. The display should be refreshed every 1 second.
Draw a black rectangle within which the time, date and day of the week should be displayed.
The day of the week function gives the result as a number so you should create an array which stores the days of the week. The result of the function will be used as the index of the array to display the day.
The time and date can be obtained by using the hour( ), minute( ), second( ), day( ), month( ) and year( ) functions.
Watch the YouTube video in the last section of this post to see how this project works.
If anyone has any questions or suggestions about this project, please feel free to comment below.
Do not send me any email requesting the codes.
Final Look
The YouTube video will be published in the next few hours so please stay tuned.
If anyone has any questions or suggestions about this project, please feel free to comment below.
Comments
Post a Comment