logo elektroda
logo elektroda
X
logo elektroda

Clock synchronizer with I2C bus with time downloaded from the Internet

Tytus Kosiarski  23 4128 Cool? (+8)
📢 Listen (AI):
Electronic circuit with ESP8266 module and ATtiny4313 microcontroller.

Hello everyone.

This time I would like to present the I2C clock synchronizer with time downloaded from the Internet. Using the previously presented clock on the Z8, I thought it would be worth extending it with this possibility, hence the presented circuit was created.

The system itself is based on the ESP8266-01 module, which obtains the time from the NTP server after logging into the WiFi network. The module cooperates with the ATtiny4313 microcontroller, whose task is generally to be seen as an I2C slave with address 0x0A by the clock cooperating with this system, which is an I2C master. In addition to serving as an I2C slave device, the microcontroller also operates the ESP8266 module via UART and supports an optional LCD display as a monitor of data exchanged via I2C with the master. You may ask why I didn't implement the I2C slave function in the ESP itself? Well, I didn't succeed. Multiple attempts (also using external interrupts to which ESP can respond) did not bring the expected effect. Here: https://github.com/esp8266/Arduino/issues/5762
there is even a discussion about it. So, in order to continue with this project, I decided to use an additional microcontroller. The microcontroller periodically sends data received from the I2C master to the WiFi module to log in to the WiFi network. Cyclic, because after turning on the power, the I2C master sends the SSID and password (both ending with CR and LF) needed to log in to the described synchronizer system once, and at that moment the WiFi network may not be available. After the ESP8266 module successfully establishes a connection to WiFi, the module periodically sends the received RSSI, time and date data to the microcontroller in the form of an 8-element int16 array. The ATtiny4313 microcontroller receives this data from the module, makes sure that the data is in the right order, and then transfers the ordered data to a buffer intended for reading by the I2C master. The optional display presents the SSID on the first line and the password received from the I2C master on the second line, and after establishing an Internet connection, displays the contents of the buffer for reading by the I2C master.
The representation of data placed in the correct order in the buffer and ready to be read by the I2C master is as follows:

I2C clock synchronizer circuit with LCD display and other components.

Where:
first element of this buffer: RSSI; a value less than 0 (0xFFC7 from LCD) means establishing an Internet connection; the lack of this connection is signaled by the value 0x001F
second element of this buffer: seconds in BCD (0057 from LCD)
third element of this buffer: minutes in BCD (0049 from LCD)
fourth element of this buffer: hours in BCD (0018 from LCD)
the fifth element of this buffer: day of the week; 1 - Monday, ..., 7 - Sunday (0001 from LCD)
sixth element of this buffer: day of the month in BCD (0023 from LCD)
seventh element of this buffer: month in BCD (0010 from LCD)
eighth element of this buffer: year in BCD (2023 from LCD).

The video on YT shows the synchronization of the clock, deliberately set to January 1, 2001, with the time downloaded from the Internet by the described module.

Now a little about the preparations for this project. When I was thinking about this project, I first came across the website: https://randomnerdtutorials.com/esp8266-nodemcu-date-time-ntp-client-server-arduino/
This is almost the finished product on which I based my sketch in the Arduino environment. Even this worked, but the inconvenience was the lack of automatic change of summer/winter time (despite the presence of the tm_isdst component in the tm structure, however, the value of this component did not change when switching from winter to summer time in spring). Moreover, the author asked us to add/subtract the number of hours expressed in seconds to the UTC time received by the module programmed with this example sketch, depending on the expected time zone. While looking for how to solve the above problems, I finally found the website: https://werner.rothschopf.net/202011_arduino_esp8266_ntp_en.htm
and after matching this sketch to our time zone using #define MY_TZ, the program began to correctly display the time in Poland on the terminal. After that, it remained only to adapt the sketch to the needs of cooperation with the described system.

A few more words about preparing the ESP8266 module itself to work with this system. Apart from adjusting the voltage levels on UART (module powered by 3.3V, ATTiny powered by 5V), which I implemented as described at http://mirekk36.blogspot.com/2015/05/konwerter-levelow-33v-ttl.html, the module itself must be programmed with the NTP_time_date.ino sketch. To do this you need:
1. Disconnect power from the module.
2. Short-circuit pins 2-1 with jumper JP1
3. Connect e.g. the ZL4USB adapter to the J2 socket. The pin arrangement in the J2 socket is adapted to the adapter I have.
4. Short pin 5 (GPIO0/Flash) of the ESP8266-01 module to ground using a microswitch.
5. With pin 5 shorted to ground, supply power to the module
6. You can now disconnect the connection from point 4. The module is now in firmware update mode.
7. In the Arduino 1.8.5 environment (this is what I use), compile and upload the NTP_time_date.ino sketch to the module
8. After uploading the sketch, disconnect and connect the power supply to the module again
9. Open e.g. RealTerm and connect to the module, connection parameters 19200.8N1.
10. Send the module the SSID name and password (both strings ending with CR LF, max. 14 characters in the string) for the WiFi network to which we want to connect. The characters of the sent SSID and password should be displayed in the appropriate lines on the connected LCD.
11. After successful connection, the module should periodically send data to RealTerm, as in the screenshot. This data will also be presented on the connected LCD.

Screenshot of Arduino IDE with NTP sketch code and RealTerm window displaying data.

If you are unable to complete the above points 7...11 (the firmware in the module may be inappropriate), you should update the firmware (which works for it). For this purpose:
1. Download the firmware with flasher from https://www.elektroda.pl/rtvforum/topic3844441.html.
2. Follow steps 1...6 above to enter firmware update mode.
3. Run the ESP8266Flasher.exe file
4. In this program, in the Config tab, indicate the files eagle.app.v6.flash.bin and eagle.app.v6.irom0text.bin as in the screenshot:

NodeMCU Firmware Programmer interface with two binary files to load.

5. Then, in the Operation tab of this program, select the appropriate COM Port and press the Flash button.
6. After successfully uploading the firmware, try again to upload the NTP_time_date.ino sketch according to the above points 1...11

I wrote the program for ATtiny4313 in the Rowley Crossworks for AVR ver.1.4 IDE.

To sum up: the clock with this synchronizer works well, at least I have a reference time according to which I can set other clocks in the house. I also had to equip the described system with:a 220uF capacitor connected to the 5V side (visible in one of the photos), because during startup it turned out that when the WiFi network was lost (e.g. when the router was turned off), the Z8681 clock microcontroller could hang. The reason was a drop in the 5V voltage supplying the clock. An example of the cooperation of the described system with the Z8681 clock is here:
https://www.elektroda.pl/rtvforum/topic3795375.html

Regards, KT

Printed circuit board with a 220uF capacitor and mounting holes.
I2C time synchronizer module with ESP8266-01 and LCD



Attachments:
  • dok.zip (114.49 KB) You must be logged in to download this attachment.

About Author
Tytus Kosiarski wrote 291 posts with rating 526 , helped 10 times. Been with us since 2007 year.

Comments

tos18 19 Nov 2023 00:26

I think it would be possible to get rid of the attina. ESP01 can support I2C. [Read more]

szymon122 19 Nov 2023 00:32

The title is a bit awkwardly written. This is not an I2C clock synchronizer because you do not synchronize the bus, but it is a clock synchronizer WITH an I2C INTERFACE :D At first I thought your clock... [Read more]

Tytus Kosiarski 19 Nov 2023 01:22

@tos18, I didn't manage to do it, as I already mentioned. If you could present a working sketch or at least a concept of how to do it, that would be great :) And it would definitely be used. @szymon122,... [Read more]

acctr 19 Nov 2023 09:52

Have you considered DCF-77? Then the Internet and ESP would be redundant. What is a dip? It reminds me of some military exercises abroad. [Read more]

tmf 19 Nov 2023 10:39

DCF77 requires a large antenna (the ferrite one is a few cm long) and range. WiFi is now practically everywhere, and certainly at home. That's why you also have BOD and WD in the MCU. It's worth... [Read more]

Anonymous 19 Nov 2023 10:52

Failed to run I2C in Slave mode on ESP8266? You should have asked on the forum. Examples from Arduino SDK ( Link ) are insufficient? This design looks as if a carriage was pulling a car. [Read more]

Tytus Kosiarski 19 Nov 2023 14:11

There is something like this: https://pl.wikipedia.org/wiki/Zapad_napi%C4%99cia I know it's worth using, but Z8 doesn't have such inventions ;) These are not the times yet ;) Perhaps... [Read more]

efi222 19 Nov 2023 14:27

I confirm problems with the operation of the I2C bus on the ESP 8266. I once tried to connect two ESP8266 via I2C. Only when setting Wire.setClock(20000); somehow it worked... [Read more]

tmf 19 Nov 2023 15:21

You wrote that you use ATtiny4313, which has these inventions.... BOD is especially important, because when the voltage increases slowly, the MCU may enter some undefined state from which it cannot recover... [Read more]

Anonymous 19 Nov 2023 15:33

There is also mentioned the alternative Brzo I2C library for ESP8266, which I used. It is more difficult to use, but sometimes it is worth using such solutions if the "popular" Arduin ones fail. Generally,... [Read more]

Tytus Kosiarski 19 Nov 2023 16:55

ABOUT! And that's great news. It is worth knowing about the existence of such a module. Then indeed, ATtiny would no longer be needed. There is something like this in Botland: https://botland.com.pl/plytki-zgodne-z-arduino-dfrobot/21227-beetle-esp32-c3-risc-vz-mikrokontrolerem-espressif-esp32-c3-risc-v-wifi-bluetooth-dfrobot-... [Read more]

Anonymous 19 Nov 2023 17:46

On Ali, similar ESP32-C3 MINI boards are priced at approximately PLN 15. [Read more]

krzbor 19 Nov 2023 18:04

Have you had problems with UART on ESP8266? Unlike I2C, UART is hardware-based and has quite a large I/O buffer. [Read more]

jacek24642 19 Nov 2023 19:27

They are available on Ali, but delivery is expensive. [Read more]

efi222 19 Nov 2023 19:42

My friend @krzbor made me realize that in fact, I don't remember ever having a problem with communication. Very often, ESP8266 in my projects is used to generate a wireless GUI with data to other MCUs. ... [Read more]

jacek24642 19 Nov 2023 19:54

Somehow I couldn't find one for PLN 4. [Read more]

efi222 19 Nov 2023 20:13

https://obrazki.elektroda.pl/6885509500_1700421147_thumb.jpg PLN 43 applies to purchases that will be shipped within 14 days. And this is the minimum purchase amount to deliver them within 14 days. [Read more]

tmf 19 Nov 2023 20:32

It's not a question of AVR, but of mathematics. Calculate the error for a given quartz and baudrate and the puzzle is solved.... [Read more]

efi222 19 Nov 2023 21:10

Of course. What I meant was that with e.g. AVR you need to match the quartz to the baudrate or vice versa. With ESP, we actually have no influence on the timing and this UART works as it should. [Read more]

FAQ

TL;DR: This design bridges 8 int16 fields from NTP time to an I2C clock, and its core lesson is: "it works" even when ESP8266 slave I2C proves unreliable. It is for retro-clock builders who need a practical Internet-to-I2C time source at address 0x0A, using ESP8266-01 for Wi‑Fi and ATtiny4313 for bus-facing reliability. [#20821650]

Why it matters: It shows a workable way to add accurate Internet time to an older 5 V clock that expects an I2C slave, while documenting the real failure modes that shaped the final architecture.

Time source / platform What the thread says Main trade-off
ESP8266-01 + ATtiny4313 + NTP Proven working build; ATtiny exposes I2C slave 0x0A Extra MCU and UART link
ESP8266 alone Author could not make slave I2C work reliably Simpler hardware, weaker I2C confidence
DCF77 Avoids Internet and ESP Needs antenna and usable reception
ESP32-C3 Suggested as having two I2C hardware controllers Requires changing platform
GPS / SIM800L Mentioned as alternate time sources More hardware; SIM800L needs a SIM

Key insight: The decisive design choice was not NTP, but bus reliability. The thread’s strongest takeaway is that ESP8266 can fetch time well, yet a separate microcontroller made the I2C-slave side dependable in this specific build. [#20821650]

Quick Facts

  • The working interface uses ESP8266-01 + ATtiny4313, with the AVR acting as an I2C slave at address 0x0A and also handling UART plus an optional LCD monitor. [#20821650]
  • The I2C read buffer has 8 elements of int16, carrying RSSI, BCD seconds, minutes, hours, weekday, BCD day, BCD month, and year; example values shown include 0xFFC7, 0x001F, and year 2023. [#20821650]
  • The ESP test link runs at 19200 8N1, and both SSID and password must end with CR LF; each string is limited to 14 characters max. [#20821650]
  • The author added a 220 uF capacitor on the 5 V rail because Wi‑Fi loss could cause a supply dip and freeze the Z8-based clock during startup or reconnect events. [#20821650]
  • One user reported ESP8266 I2C became usable only after lowering Wire.setClock to 20000, while another suggested ESP32-C3 as a cleaner replacement because it has two I2C hardware controllers. [#20822598]

How does this ESP8266-01 and ATtiny4313 clock synchronizer download NTP time from the Internet and expose it as an I2C slave at address 0x0A?

It uses the ESP8266-01 for Wi‑Fi and NTP, then hands the data to an ATtiny4313 over UART. The ATtiny4313 logs into Wi‑Fi using SSID and password received once from the I2C master, then stores the returned time data in an 8-element buffer. To the clock, the AVR appears as an I2C slave at address 0x0A. The ESP periodically sends RSSI, time, and date, and the AVR reorders that data before the clock reads it over I2C. [#20821650]

Why was an ATtiny4313 used between the ESP8266-01 and the clock instead of implementing the I2C slave directly on the ESP8266?

An ATtiny4313 was used because the author could not make the ESP8266 work reliably as an I2C slave. Multiple attempts failed, including tests with external interrupts, so the AVR became the stable bus-facing device. That let the ESP8266 focus on Wi‑Fi and NTP, while the ATtiny handled slave I2C, UART, and the optional LCD. The design is less elegant, but it solved the exact failure that blocked a one-chip implementation. [#20821650]

What is the structure of the 8-element int16 buffer sent to the I2C master, and how are RSSI, time, date, and weekday encoded in it?

The buffer has 8 signed 16-bit elements in a fixed order. Element 1 is RSSI; a value below 0 such as 0xFFC7 means Internet connection, while 0x001F signals no connection. Elements 2–4 are seconds, minutes, and hours in BCD. Element 5 is weekday, where 1 = Monday and 7 = Sunday. Elements 6–7 are day and month in BCD, and element 8 is the year, shown as 2023 in the example. [#20821650]

How do I program an ESP8266-01 with the NTP_time_date.ino sketch and put it into firmware update mode using GPIO0 and JP1?

You enter programming mode by forcing GPIO0 low during power-up, then upload the sketch. 1. Remove power, short JP1 pins 2–1, connect the adapter, and hold GPIO0/Flash to ground. 2. Power the module, release GPIO0, and upload NTP_time_date.ino in Arduino 1.8.5. 3. Power-cycle the ESP8266-01 after upload. Those steps place the module in firmware update mode and then return it to normal boot after flashing. [#20821650]

What steps are needed to test the ESP8266-01 with RealTerm at 19200 8N1 and send the SSID and password with CR LF endings?

Use a serial terminal after flashing to confirm Wi‑Fi login and time output. 1. Open RealTerm and connect at 19200 8N1. 2. Send the SSID and password as two strings, each terminated with CR LF and limited to 14 characters. 3. Check that the LCD echoes both strings and that the module later sends periodic time data. If this fails, the thread says the ESP firmware may need updating before retrying the sketch upload. [#20821650]

Why did the author switch from the Random Nerd Tutorials NTP example to the Werner Rothschopf time zone approach with #define MY_TZ?

The author switched because the first example did not handle summer/winter time automatically. The earlier approach required manually adding or subtracting hours from UTC, and tm_isdst did not change as expected during the spring DST transition. After changing to the Werner Rothschopf method with #define MY_TZ, the module started showing the correct time for Poland on the terminal. That made it suitable for the clock synchronizer without manual seasonal correction. [#20821650]

What is BCD time format, and why are seconds, minutes, hours, day, and month stored that way in this clock synchronizer?

"BCD is a numeric encoding that stores each decimal digit separately in binary, which makes values like 57 seconds appear as 0x57 rather than pure binary 57." This synchronizer stores seconds, minutes, hours, day, and month in BCD because the receiving clock already expects time fields in that form. The example display shows 0057, 0049, 0018, 0023, and 0010, matching human-readable decimal digits and simplifying transfer into a classic clock design. [#20821650]

What is a voltage dip, and how can a temporary drop on the 5V rail freeze a Z8-based clock during WiFi reconnect events?

A voltage dip is a short-term drop in supply voltage, and here it could freeze the Z8 clock MCU. "Voltage dip is a power-quality disturbance that temporarily lowers the supply voltage below its nominal level, often long enough to upset logic without causing a clean reset." The author found that losing Wi‑Fi, for example when the router was turned off, could pull down the 5 V rail during startup or reconnect. That caused the Z8-based clock to hang until a 220 uF capacitor was added on the 5 V side. [#20821650]

How do BOD and watchdog features help microcontrollers recover from brownouts or undefined startup states in a design like this?

BOD and a watchdog improve recovery by forcing a reset when voltage or firmware behavior becomes unsafe. One reply explicitly recommended both, and another explained that BOD is especially important when voltage rises slowly, because the MCU can enter an undefined state and fail to recover by itself. In this project, that advice applies more to the ATtiny4313 side than to the older Z8 clock MCU, because the author noted the Z8 lacks those protections. [#20822701]

Which is a better time source for a home clock synchronizer: NTP over WiFi, DCF77, GPS, or a SIM800L modem, and what trade-offs matter most?

NTP over Wi‑Fi was the best fit in this thread because it already worked at home and integrated with the ESP8266 build. DCF77 removes the Internet requirement, but a reply noted its antenna is several centimeters long and reception matters. GPS and SIM800L were also mentioned as options, but SIM800L was later said to require a SIM card. For this project, the deciding factors were existing Wi‑Fi coverage, simple home deployment, and compatibility with an I2C-facing clock. [#20840980]

What working sketch, library, or implementation approach can make an ESP8266 operate reliably as an I2C slave with an Arduino or AVR master?

The thread does not provide a confirmed working slave-I2C sketch for ESP8266 with an AVR master. One reply says Arduino Wire examples were not enough, and the original author cites repeated failures in slave mode. Another user points to the alternative Brzo I2C library and says it can be worth using when popular Arduino solutions fail. In practical terms, the only fully proven approach in the thread is to avoid ESP8266 slave I2C and use a separate AVR or move to ESP32-C3. [#20822709]

How much does lowering Wire.setClock on ESP8266 help I2C stability, and why do some users report it only works around 20 kHz?

Lowering the clock can help significantly, but the thread gives only one hard number: 20 kHz. A user reported that two ESP8266 devices communicated only after setting Wire.setClock(20000);. Another reply explains why results vary: ESP8266 lacks a hardware I2C controller, so its I2C is software-implemented, and communication quality depends on that implementation. That makes low bus speeds a workaround, not a guaranteed fix, especially when mixing ESP8266 with AVR masters. [#20822709]

What makes ESP32-C3 a better replacement than ESP8266 plus ATtiny4313 for an Internet time to I2C gateway, especially regarding hardware I2C controllers?

ESP32-C3 was presented as a cleaner replacement because it has two I2C hardware controllers. That matters because it can combine Internet access and robust I2C without the extra UART hop and second microcontroller. A reply also estimated the module cost at roughly a dozen or so złotys, making it attractive as a simpler gateway platform. In this thread, the strongest argument for ESP32-C3 is reduced complexity: fewer chips, fewer links, and less dependence on software-emulated I2C. [#20822709]

How should I handle 3.3V-to-5V level shifting for UART and I2C when connecting an ESP8266-01 to an ATtiny4313 or a 5V clock circuit?

You should level-shift the signals because the ESP8266-01 runs at 3.3 V while the ATtiny and clock side run at 5 V. The author says he adjusted the UART voltage levels using a dedicated converter approach before linking ESP8266 and ATtiny4313. Later, another reply adds an I2C-specific warning: if the bus has pull-ups to 5 V, you must remember the voltage converter there as well. In short, protect both UART and I2C whenever a 3.3 V ESP meets a 5 V bus. [#20823558]

Why can UART between ESP8266 and AVR be more reliable than I2C in this kind of project, and how do baud rate, crystal choice, and divider errors affect communication?

UART can be more reliable here because ESP8266 has hardware UART, while its I2C is software-based. One participant explicitly says they do not recall having UART problems on ESP8266 and often use it to exchange data with other MCUs. The discussion also notes that AVR UART quality depends on baud-rate math, crystal choice, and divider error. If the chosen baud rate is only approximate on one side, communication can degrade; picking a rate with friendly divisors on both sides improves margin. [#20823558]
Generated by the language model.
%}