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

Update: Line adapter for Ozito Blade Trimmer

Update (Dec 2021): If you access to a 3D printer, I would now recommend this solution , which makes it super easy to replace the trimmer line. I have been using it for a few months now with zero issue.

3D Printer Filament Joiner

I have been looking at various ways of joining 3D printing filaments. One method involves running one end of a filament through a short PTFE tubing, melting it with a lighter or candle, retracting it back into the tubing and immediately plunging the filament to be fused into the tubing: One problem with this method is that you can't really control the temperature at which you melt the filament, so you frequently end up with a brittle joint that breaks upon the slightest bend. Aliexpress even sells a contraption that works along the same line. As it uses a lighter or candle as well, it suffers from the same weakness. I am not even sure why you need a special contraption when a short PTFE tubing will work just as well. Another method involves using shrink tubing/aluminium foil, and a heat gun: But a heat gun is rather expensive, so I wanted to explore other alternatives. The candle + PTFE tubing method actually works quite well when you happen to melt it at the rig

Attiny85 timer programming using Timer1

This Arduino sketch uses Timer1 to drive the LED blinker: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 /* * Program ATTiny85 to blink LED connected to PB1 at 1s interval. * Assumes ATTiny85 is running at 1MHz internal clock speed. */ #include <avr/io.h> #include <avr/wdt.h> #include <avr/sleep.h> #include <avr/interrupt.h> bool timer1 = false , led = true ; // Interrupt service routine for timer1 ISR(TIMER1_COMPA_vect) { timer1 = true ; } void setup() { // Setup output pins pinMode( 1 , OUTPUT); digitalWrite( 1 , led); set_sleep_mode(SLEEP_MODE_IDLE); // Setup timer1 to interrupt every second TCCR1 = 0 ; // Stop timer TCNT1 = 0 ; // Zero timer GTCCR = _BV(PSR1); // Reset prescaler OCR1A = 243 ; // T = prescaler / 1MHz = 0.004096s; OCR1A = (1s/T) - 1 = 243 OCR1C = 243 ; // Set to same value to reset timer1 to