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

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

Attiny85 timer programming using Timer1

Hacking an analog clock to sync with NTP - Part 5