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

Operation

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.

Hardware

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.

Software

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.

Comments

Popular posts from this blog

Cooling mod for the X96 Air #2

Previously, I added a USB cooling fan to the X96 Air TV box . The problem with this mod is that the fan is always running, and it runs at full speed. Ideally, the fan should kick in only when the CPU temperature is above a certain threshold. It would be even better if there is a way to control the fan speed. Dan McDonald left me a comment pointing to his project on Github . He basically connected the fan to a USB relay that can be controlled by Python script. His project inspired me to make a similar mod that would make use of the spare D1 Mini boards I have lying around. The plan is to hook up the fan to a MOSFET (2N7000) and control it via PWM. Here's the very simple circuit: The code simply reads a single character from the serial port (0 - 9). 0 will turn the fan off, while 1 - 9 will generate a proportional PWM to drive the fan, with 1 being the lowest and 9 being the highest. Here's the Arduino code: #include <Arduino.h> void setup () { Serial . begin ( 9600 ...

Installing and customizing CoreELEC in X96 Air

I previously installed CoreELEC on another TV Box ( Ugoos X3 Pro ), which unfortunately died after only 9 months during the summer (due to the unit overheating, which I learned is a common problem for cheap Android TV boxes). So this time I purchased a X96 Air  (4GB/32Gb) and had to do the whole thing again. So this is a note-to-self in case I ever have to install CoreELEC again on some other device. Installation of CoreELEC is simple enough by following this guide . Basically, it involves downloading and writing the firmware to a microSD card using usbimager . Then insert the microSD card, reset the unit and hold the reset until the logo appears. The unit will then proceed to boot into CoreELEC. First thing is to connect to WiFi, then enable SSH. This allows me to login via ssh and execute: ceemmc -x from the terminal. This writes CoreELEC to the built-in eMMC storage, after which I am able to remove the microSD card and reboot the unit into CoreELEC via the built-in sto...

DC-DC Buck Stepdown Converter for ESP8266

I am working on a project that requires a step-down converter from 12V to 5V, that will then power a WeMOS D1 Mini. I saw this new mini buck converter based on the usual LM2596 MP2307 , so I thought I'd give it a try. Unfortunately, it didn't work. Although it is supposed to be able to supply up to 1.8A, the D1 Mini was not able to boot up. The 5V pin was being properly supplied, but the 3.3V pin measures at only ~1.3V. So I had to go back to my usual LM2596 module, which is much larger, but works to power the D1 Mini with a 12V source. Here's a great review of the mini buck converter I found while trying to figure out how to make it work. The fact that it has high quiescent current (~60mA) is also mentioned in a few other sources.