Arduino Real Time Clock and temperature Monitor using the DS3231 RTC and 3.2″ Color TFT display
- Nick Koumaris
- http://educ8s.tv
- info@educ8s.tv
- 11.666 Views
- moderate
- Tested
Time is a critical element of our existence that will never get old, and with technology, we can find better and more intuitive ways to measure it. In one of our past tutorials, we looked at how the DS3231 real time clock module can be used with Arduino to display time on a 16×2 LCD display. Today, we will build an upgrade to that project using an Arduino Due, the DS3231 RTC module and a 3.2″colour TFT display in place of the 16×2 LCD display used in the previous project.
At the heart of today’s project is the DS3231 real time clock module which we will use to obtain the current time, date and temperature of the environment. The DS3231 real time clock module is one of the most popular real-time clock chips among makers and DIY enthusiasts. It is a low-cost, highly accurate, I2C based real-time clock (RTC) with a temperature-compensated crystal oscillator (TCXO) and crystal integrated into it. The module integrates a coin cell battery input which helps it retain date and time even when the main power to the device is interrupted. It maintains seconds, minutes, hours, day, date, month, and year information, automatically adjusting the date for months with fewer than 31 days, including corrections for leap year. It can be set to operate either in the 24-hour or 12-hour format with an active-low AM/PM indicator. It has been used in several projects on this website mostly, due to its accuracy, and its low power requirements which help it keep time accurately, for a longer period of time compared to other real-time clocks (RTC) modules.
Another key part/component that we will use in today’s tutorial is the Arduino Due. One of the important things, when designing electronic systems that have displays is ensuring, that there is no flicker or lag when updating information on the screen and one of the best ways to ensure that, is to use a fast enough micro-controller. Putting this into consideration, for this project, we will use the very fast Arduino Due board. The Arduino Due has one of the fastest CPU in the Arduino family. The Due runs on an 84MHz CPU compared to the 16MHz CPU speed of the Arduino UNO, and as such, it is able to update the screen without any visible flickering.
The most important update to the previous project, however, is the 3.2″color LCD display being used. The display gives us the ability to create a better, bigger and colourful user interface for our clock at a cheap price as it costs about 7$ on banggood.
The goal for this project is to build a real-time clock with a user-friendly interface capable of displaying (without lag or flickering) the current time, date, temperature including the minimum and maximum temperature recorded in a particular environment over time.
Required Components
The following components are required to build this project;
As usual, all of these components can be bought via the links attached to them.
Schematics
The 3.2″ TFT, like most other TFT displays, comes as a shield which can be easily mounted on the Arduino Due. This, however, makes it difficult to access the IOs of the Arduino after the display has been mounted, as it tends to cover the front face of the board. To solve this, so that the DS3231 module can be connected, male headers are used (after bending them as shown in the picture below) to connect the RTC module to the Arduino.
Connect the DS3231 to the Arduino as shown below.
The DS3231 module is an I2C based device, and it’s thus connected to the Arduino as shown above. For clarity a pin map is made available below;
DS3231 - Arduino Due VCC - 3.3V GND - GND SDA - SDA SCL - SCL
With the connections all done, we can move to the code for the project.
Code
To easily write the code for this project, we will use two libraries: the Bodmer TFT HX8537 library for the TFT display and the Sodaq DS3231 library to easily interface with the DS3231 module. Both libraries can be downloaded via the links attached to their names above. The Bodmer library is a version of the UTFT library specially modified for the Arduino Due as this particular display is incompatible with the UTFT library.
With the libraries installed, relaunch the IDE to begin writing the code. The code for this project is quite simple but bulky, due to the functions used to create the user interface.
As usual, we start the code by including the libraries needed for the project.
// Written by Nick koumaris // info@educ8s.tv #include <TFT_HX8357_Due.h> // https://github.com/Bodmer/TFT_HX8357_Due #include <Sodaq_DS3231.h> //RTC Library https://github.com/SodaqMoja/Sodaq_DS3231
Next, we create variables that will be used to store information in our code. With this done, we create an instance of the HX8357 library.
float minTemperature = 100; float maxTemperature = -40; String dateString; String hours; int minuteNow=0; int minutePrevious=0; TFT_HX8357_Due tft = TFT_HX8357_Due(); // Invoke custom library
Next is the void setup function. We initiate communication with the RTC module and Initialize the display, setting our preferred orientation for the display and print the UI to the display.
void setup() { rtc.begin(); tft.init(); tft.setRotation(1); tft.fillScreen(0xC618); delay(100); printUI(); }
With this done, we move to the void loop function. Under this function, we write the code to update all the parameters (after specific intervals) on the display including the min temperature, the max temperature, time and the date.
void loop() { float temperature = rtc.getTemperature(); getAndPrintTime(); printTemperature(temperature); if(temperature>maxTemperature) { maxTemperature = temperature; updateMaxTemperature(); } if(temperature<minTemperature) { minTemperature = temperature; updateMinTemperature(); } delay(1000); }
The other part of the code are functions used for creating the User Interface for the screen.
The complete code for the project is attached under the download section below.
Demo
Upload the code to Arduino, and you should see the screen come up with the time and temperature as shown in the image below.
That’s it for this tutorial guys, there are several useful projects that can be built using this tutorial as a foundation. You could decide to add a buzzer to the project to create an alarm clock or make a to-do list based project, all out of this.
Till next time!
The video version of the tutorial is available on youtube here.