Skip to main content

ESPCLOCK2, Part 4 - Implementation

Parts required:

  • WeMos D1 Mini
  • ATtiny85
  • 0.47F capacitor
  • BAT46 Schottky diode
  • Pushbutton 
  • 2 x 100 ohm resistors
  • 2 x 4.7K ohm resistors

Breadboard layout:

Source code


When the circuit is first powered up, the onboard LED on the D1 Mini will light up, indicating that it is in configuration mode. 

Selecting the captive wifi portal "ESPCLOCK2" will bring up the web browser. The configuration page looks like this:
Assuming that the timezone has been prefilled correctly, one only has to select the local wifi AP and supply the password, then key in the time on the physical clock in HHMMSS format. Hit the [Save] button, the onboard LED should turn off, and the clock will start to run.


When the pushbutton is held down while the circuit is powered up, the D1 mini clears out the wifi AP and password, allowing you to start over again from a clean slate.

The Schottky diode cuts off the link to the 3.3V pin when the battery is depleted or removed. The ATtiny85 will then be powered by the capacitor. The drop across the diode is 0.34V, so the voltage to the ATtiny85 VCC pin is 3.3V - 0.34V = 2.96V. The ADC on the ATtiny85 will measure this as anywhere from 27XXmV to 29XXmV. With the battery off, the VCC drops to 2.8V quite quickly. The ATtiny95 will measure this as between 26XXmV to 28XXmV. Hence in the code, once the ADC drops below 2700mV, we write the clock data to EEPROM, and put the micocontroller in deep sleep.


The D1 Mini does very little compared to the ATtiny85. When it starts up, it goes into config mode if there is no wifi config info saved, or the reset button is pressed.

Once it is configured, it will send out a START_CLOCK command to the ATtiny85 via I2C. Then it tries to get the local time from the network, and pass that along to the ATtiny85 through a SET_NETTIME command.

When it first starts, it will deep sleep for 5 mins before passing the local time to ATtiny85 again. This is to allow the ATtiny85 to calibrate the OCR1A counter for Timer1 to make it get as close as possible to 1 second interval. After that, it will only wake up every 60 minutes.

On my setup, the OCR1A counter initially starts at the calculated value of 243. It eventually gets calibrated to 247 based on the feedback from the D1 Mini.

The ATtiny85, on the other hand, is the tiny microcontroller that does all the heavy lifting. These are the tasks it needs to juggle simultanesouly:
  • Listen and act on I2C commands from the D1 Mini.
  • Respond to Timer1 interrupt every second, and use Timer0 interrupt to pulse and tick the clock second hand.
  • Measure the voltage on the VCC pin via ADC every 5 secs, and take action when voltage drops below a certain level.

Power requirement

The entire purpose for ESPCLOCK2 is try to reduce the current draw of the system. Previously, ESPCLOCK1 could only last slightly over a day on a 2600mAh USB battery pack.

Using the LTC4150 Coulomb counter, we can measure the current draw of the system quite accurately. In steady state (D1 Mini in deep sleep, and ATtiny85 ticking the clock every second), the current draw is about 2.5mA.When it is playing catch up (by ticking the clock as fast as possible), the current draw goes up to 8mA over the duration.

When the D1 Mini wakes up to check the network time, the average current draw increases slightly to 3.5mA over the duration. Put another way, instead of 0.6144C lasting 200 secs, the same capacity only lasts for 150 secs during the period when the D1 Mini wakes up, a whole 50 secs less.

The problem with the steady state current draw of 2.5mA is that it is so tiny that most USB battery packs will automatically shut off after a short period of time. This seems to be a widely documented problem. There are battery packs that do not have this limitation, but they are typically priced at a premium.

Hence, for ESPCLOCK2, I have chosen to power the circuit with 4 x AA NiMH rechargeable batteries, or even 4 x AA alkaline batteries. The former should yield 5.1 ~ 5.2V, while the latter will give ~6V. This is connected to the 5V pin on the D1 Mini, which regulates the voltage down to 3.3V.

At a steady state current draw of 2.5mA, using rechargeable batteries of 2300mAh will yield a uptime of 920 hours, which is about 38 days. Providing for other factors (D1 Mini wake up current, catch up current draw, lower actual battery capacity), I think 4 x AA batteries should comfortably provide for at least a month of uptime. 

This final figure is 30 times better than ESPCLOCK1, but it can still be improved. By using a more barebones ESP8266 board such as the ESP-07, we should be able to cut down the deep sleep current draw from 0.8mA to <0.1mA. This should bring the steady state current draw from 2.5mA to 1.8mA (2.5mA - 0.7mA).

By using some other way to check for low VCC (eg. a power regulator with a "power good" output), we might be able to turn off the ATtiny85's ADC and potentially save 0.3mA (experimentally arrived at by commenting out the code for ADC measurement). This will further bring the steady state current draw to 1.5mA (1.8mA - 0.3mA).

Finally, by using a RTC such as the DS3231 to replace the Timer1 interrupt on the ATtiny85 (another idea inspired by the SynchroClock project) , we should be able to reduce the current draw further by putting the ATtiny85 into POWER_DOWN sleep (instead of IDLE sleep). I will run an experiment later using the watchdog timer to see how much reduction in current draw that will produce.

If we can reduce the current draw to 1mA, this will increase the uptime to 2300 hours on the same set of batteries, which is about 95 days. This is a realistic uptime of about 3 months.


Popular posts from this blog

Adding "Stereo Mixer" to Windows 7 with Conexant sound card

This procedure worked for my laptop (Thinkpad E530) with a Conexant 20671 sound card, but I suspect it will work for other sound cards in the Conexant family. I was playing with CamStudio to do a video capture of a Flash-based cartoon so that I can put it on the WDTV media player and play it on the big screen in the living room for my kids. The video capture worked brilliantly, but to do a sound capture, I needed to do some hacking. Apparently, there was this recording device called "Stereo Mixer" that was pretty standard in the Windows XP days. This allowed you to capture whatever was played to the speaker in all its digital glory. Then under pressure from various organizations on the dark side of the force, Microsoft and soundcard makers starting disabling this wonderful feature from Windows Vista onwards. So after much Googling around, I found out that for most sound cards, the hardware feature is still there, just not enabled on the software side. Unfortunately, to

Hacking a USB-C to slim tip adapter cable to charge the Thinkpad T450s

This hack is inspired by this post . A year ago, I bought an adapter cable for my wife's Thinkpad X1 Carbon (2nd Gen) that allows her to power her laptop with a 60W-capable portable battery (20V x 3A). A USB-C cable goes from the battery into the adapter, which converts it to the slim tip output required by the laptop. Everything works out of the box, so I didn't give much thought about it. Recently, I decided to buy a similar cable for my Thinkpad T450s. I know technically it should work because the T450s can go as low as 45W (20V x 2.25A) in terms of charging (though I have the 65W charger - 20V x 3.25A).  I went with another adapter cable because it was cheaper and also I prefer the single cable design. So imagine my surprise when the cable came and I plugged it into my laptop and it didn't work! The power manager just cycle in and out of charging mode before giving up with an error message saying there is not enough power. After much research and reading the Thinkwiki

Using Google Dashboard to manage your Android device backup

I used to use AppBrain/Fast Web Install to keep track of which apps I have installed on my phone, and to make it easier to reinstall those apps when the phone gets wiped or replaced. But AppBrain had been going down the tubes, and Fast Web Install had always been a hit-and-miss affair. Android's own "backup to the cloud" system had previously been even more unusable. There isn't a place where you can see what has been backed up. And when you setup a new phone with your Google account, you just have to wait and pray that your favorite apps will be restored to the phone. Typically all the stars have to be aligned just right for this to happen. More often than not, after waiting for an hour or so and nothing happens, you just curse under your breath and proceed to install your favorites apps manually via the Play Store. But I just looked again recently and was pleasantly surprised that things are much more civilized now. Firstly there is a place now where you can loo