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

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

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

Hacking an analog clock to sync with NTP - Part 5

This is how it looks after I have put everything together. The Arduino sketch is available here . The 2 jumper wires soldered to the clock mechanism are connected to pins D0 and D1 on the ESP-12 (in any order). When the device first boots up, it presents an access point which can be connected to via the PC or smartphone. Once connected, the captive portal redirects the web browser to the configuration page:     A custom field has been added to the WiFi configuration page to enter the current clock time in HHMMSS format. Try to set the clock time to as close to the current time as possible using the radial dial at the back of the clock so the clock will have less work to do catching up. In the config page, the HTML5 Geolocation API is also used to obtain your current location (so if your web browser asks if you would like to share your location, answer "yes"). This is then passed to the Google Time Zone API to obtain the time and DST offset of your time z