ESP32-C3 Super Mini Real-Time Clock with OLED Display
Learn how to create an accurate real-time clock using the ESP32-C3 Super Mini and OLED display with NTP synchronization. Step-by-step guide included. Are you looking to create an accurate IoT clock for your next project? In this comprehensive guide, we’ll walk you through building a real-time clock using the ESP32-C3 Super Mini and an OLED display. This project leverages Network Time Protocol (NTP) servers for precise timekeeping while providing a clear visual output.
What You’ll Need: Materials and Setup
Hardware Requirements:
- ESP32-C3 Super Mini (Affiliate): Buy on AliExpress or Buy on AliExpress
- 0.96″ OLED Display (I2C interface) (Affiliate) – Buy on AliExpress
- Breadboard and jumper wires (Affiliate): Buy on AliExpress
- USB-C cable for programming and power
Software Requirements:
- Arduino IDE (latest version recommended)
- ESP32 board support package
- Libraries to control de hardware
Step-by-Step Guide: How to Build Your ESP32-C3 Real-Time Clock
Install Required Libraries
Go to Sketch > Include Library > Manage Libraries and install the following:
- NTPClient by Fabrice Weinberg
- U8g2 by oliver
Connect the OLED Display
Now, let’s connect the OLED display to the ESP32-C3 Super Mini:
- VCC to 3.3V
- GND to GND
- SDA to GPIO8
- SCL to GPIO9
We are using the I2C to communicate with the Oled Display:
Circuit
Adjusting for Your Timezone
When working with NTP (Network Time Protocol), the time retrieved from NTP servers is typically in Coordinated Universal Time (UTC). To display the correct local time on your ESP32-C3 real-time clock, you need to adjust this time for your specific timezone. Hereโs how to do it:
Step-by-Step Guide to Adjusting Timezone
Useful Link for Timezone Information (WIKIPEDIA)
For more information about timezones and daylight saving adjustments, consider visiting these resource:
- Time Zone Database – A comprehensive list of all time zones.
Steps to Determine the Correct Offset (Open previous Time Zone Database Link -> Wiki)
- Identify Your Location: Find your specific time zone in the list.
- Check the UTC Offset Column: Look at the “UTC offset” column, which corresponds to Standard Time (SDT).
- Modify the Time Offset in Your Code:
In the setup function of your Arduino sketch, you will set the time offset using thesetTimeOffset()
method of theNTPClient
object. Hereโs how to do it:
// Set timezone offset in seconds // Example: UTC-5 (Eastern Standard Time) = -5 * 3600 = -18000 seconds timeClient.setTimeOffset(-18000); // Adjust this value based on your timezone
Common Timezones Table
To help you easily set your timezone offset in the timeClient.setTimeOffset()
function, refer to the following table of common timezones:
Region/City | UTC Offset | Example Code |
---|---|---|
Eastern Standard Time | UTC-5 | timeClient.setTimeOffset(-18000); |
Central Standard Time | UTC-6 | timeClient.setTimeOffset(-21600); |
Mountain Standard Time | UTC-7 | timeClient.setTimeOffset(-25200); |
Pacific Standard Time | UTC-8 | timeClient.setTimeOffset(-28800); |
Greenwich Mean Time | UTC+0 | timeClient.setTimeOffset(0); |
Central European Time | UTC+1 | timeClient.setTimeOffset(3600); |
Eastern European Time | UTC+2 | timeClient.setTimeOffset(7200); |
India Standard Time | UTC+5:30 | timeClient.setTimeOffset(19800); |
Singapore Standard Time | UTC+8 | timeClient.setTimeOffset(28800); |
Japan Standard Time | UTC+9 | timeClient.setTimeOffset(32400); |
The NTPClient library, which is in the code, automatically handle Daylight Saving Time (DST) adjustments when properly configured. Let’s revisit the relevant part of the original code:
timeClient.begin(); // Adjust for your timezone here timeClient.setTimeOffset(0); // UTC+0
This setup is correct for Portugal and will automatically adjust for both summer and winter time. Here’s why:
- The NTPClient library fetches the current UTC time from NTP servers.
- By setting the time offset to 0 (
setTimeOffset(0)
), you’re telling the client that your local time is UTC+0, which is correct for Portugal’s standard time. - The NTP servers provide information about DST, which the NTPClient library uses to automatically adjust the time.
So, you don’t need to make any changes to compensate for summer time. The code as it stands will automatically handle the switch between winter time (WET, UTC+0) and summer time (WEST, UTC+1) for Portugal.
This approach is valid for any timezone, which is one of the great advantages of using the NTPClient library.
Complete Code (Change In The Code Your Timezone and Wifi credentials)
#include <WiFi.h> #include <NTPClient.h> #include <WiFiUdp.h> #include <U8g2lib.h> const char* ssid = "YOUR SSID"; const char* password = "YOUR WIFI NETWORK PASSWORD"; WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "pool.ntp.org"); // Initialize U8g2 display U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE); void setup() { Serial.begin(115200); // Initialize U8g2 display u8g2.begin(); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } timeClient.begin(); // Adjust for your timezone here timeClient.setTimeOffset(0); // Adjust this value based on your timezone (UTC offset in seconds) } void loop() { timeClient.update(); u8g2.clearBuffer(); // Set font and draw time u8g2.setFont(u8g2_font_logisoso24_tr); u8g2.drawStr(0, 30, timeClient.getFormattedTime().c_str()); // Set font and draw day u8g2.setFont(u8g2_font_ncenB14_tr); u8g2.drawStr(0, 60, String(timeClient.getDay()).c_str()); u8g2.sendBuffer(); delay(1000); }
Time Example
The number returned by timeClient.getDay()
represents the day of the week, where:
- 0 represents Sunday
- 1 represents Monday
- 2 represents Tuesday
- 3 represents Wednesday
- 4 represents Thursday
- 5 represents Friday
- 6 represents Saturday
This is a standard convention used in many programming languages and libraries for representing days of the week. The numbering starts at 0 for Sunday and goes up to 6 for Saturday.
Troubleshooting Common Issues
Even with careful setup, you may encounter some issues while building your ESP32-C3 real-time clock. Here are common problems and their solutions:
Common Issues:
- Wi-Fi Connection Issues:
- Ensure that your SSID and password are correctly entered in your code.
- Check if your Wi-Fi network is operational and within range.
- OLED Display Not Working:
- Verify that all connections are secure.
- Ensure that you have installed the correct library for your OLED display.
- NTP Synchronization Failures:
- Make sure your ESP32 is connected to the internet.
- Check if the NTP server address is correct; you can try using different NTP servers.
Solutions:
- If you experience Wi-Fi connection issues, try resetting your router or moving closer to it.
- For display problems, double-check wiring connections against your schematic.
- If synchronization fails, consider increasing the delay between attempts or using a different NTP server pool.
How Accurate is the ESP32-C3 for Timekeeping?
The accuracy of your real-time clock will depend on several factors:
- Internal Oscillator Drift: The ESP32-C3’s internal oscillator can drift by up to ยฑ20 seconds per hour due to temperature variations. This means that without regular synchronization with an NTP server, your clock may become inaccurate over time.
- External RTC Modules: For projects requiring higher precision, consider integrating an external RTC module like the DS3231. This module offers superior accuracy (ยฑ2ppm) compared to the internal oscillator.
- Regular Synchronization: To maintain accuracy, ensure that your device regularly connects to Wi-Fi and synchronizes with NTP servers.
- Long-Term Performance: Users have reported that over longer periods (e.g., weeks), the ESP32 clock can drift significantly if not synchronized frequently.
By following these guidelines, you can enhance the accuracy of your ESP32-C3 real-time clock project.
Enhancing Your ESP32-C3 Clock: Additional Features
Display Date Information (Change In The Code Your Timezone and Wifi credentials)
Want to expand functionality? Here’s how to show the current date:
#include <WiFi.h> #include <NTPClient.h> #include <WiFiUdp.h> #include <U8g2lib.h> const char* ssid = "YOUR SSID"; const char* password = "YOUR WIFI NETWORK PASSWORD"; WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "pool.ntp.org"); // Initialize U8g2 display U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE); String weekDays[7] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; String months[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; void setup() { Serial.begin(115200); // Initialize U8g2 display u8g2.begin(); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } timeClient.begin(); // Adjust for your timezone here timeClient.setTimeOffset(0); // Adjust this value based on your timezone (UTC offset in seconds) } void loop() { timeClient.update(); time_t epochTime = timeClient.getEpochTime(); struct tm *ptm = gmtime((time_t *)&epochTime); int monthDay = ptm->tm_mday; int currentMonth = ptm->tm_mon; int currentYear = ptm->tm_year + 1900; u8g2.clearBuffer(); // Set font and draw time u8g2.setFont(u8g2_font_logisoso24_tr); // Larger font for time u8g2.drawStr(0, 30, timeClient.getFormattedTime().c_str()); // Set a smaller font for date u8g2.setFont(u8g2_font_ncenB08_tr); // Smaller font for date String dateStr = String(weekDays[timeClient.getDay()]) + " " + String(monthDay) + " " + String(months[currentMonth]) + " " + String(currentYear); // Draw the date string u8g2.drawStr(0, 50, dateStr.c_str()); // Adjust Y position as needed u8g2.sendBuffer(); delay(1000); }
Date Example
Links to Related Resources
We’d love to hear about your experience building this ESP32-C3 real-time clock! Have you encountered any challenges or made any interesting modifications? Share your thoughts in the comments below.For more IoT projects and tutorials, check out our related articles:
- ESP32-C3 Super Mini: Arduino IDE Quick Start Guide
- ESP32-C3 Mini Relay Timer: DIY Countdown Solution
- Temperature Sensor – ESP32-C3 Documentation โ Official documentation detailing how to use the built-in temperature sensor on the ESP32-C3.
Frequently Asked Questions (FAQ)
- How do I ensure my clock stays accurate over time?
Regularly synchronize with NTP servers by connecting to Wi-Fi frequently. - Can I use other displays instead of an OLED?
Yes, you can use other types of displays as long as they are compatible with the ESP32-C3. - What should I do if my device fails to connect to Wi-Fi?
Double-check your network credentials in the code and ensure that your router is functioning properly. - How often should I synchronize with NTP?
Itโs advisable to synchronize at least once every hour or more frequently if possible for better accuracy.
Future Enhancements to Consider:
- Adding a battery backup for timekeeping during power outages.
- Integrating with other sensors for time-based data logging.
- Creating a web interface for remote time monitoring and adjustment.
Conclusion: Your ESP32-C3 Real-Time Clock Project
By following this guide, you’ve created a reliable and visually appealing timekeeping system using the ESP32-C3 Super Mini and OLED display. This project demonstrates the versatility of the ESP32-C3 for IoT applications, particularly in accurate timekeeping and display.