Skip to main content

ESPCLOCK 3.1

Here are the latest changes to the ESPCLOCK3 project.

Migration to PlatformIO

I have moved to PlatformIO for new projects, and migrated some of my older projects to PlatformIO as well. PlatformIO offers a lot of advantages over the Arduino IDE, including:

  • Intellisense quick info and code completion
  • Easy to target specific library versions in platform.ini
  • Easy to have multiple targets in the same project. For ESPCLOCK, this means target for ESP8266 and ATtiny85 in the same project.

There are minor pitfalls though. For example, to set fuse settings for ATtiny85 (eg. 1MHz clock, retain EEPROM, disable BOD), they have to be manually computed and added to platform.ini. This was not very intuitive and took me some time to figure out.

Dealing with low battery

In the original design, the ATtiny85 checks the VCC voltage level and writes vital info to EEPROM when voltage falls below 2.8V. 

The problem is, the discharge characteristics of NiMH battery is such that it operates at ~1.2V for most of its effective life, followed by a sharp fall. Common advice is not to let the voltage fall below 1V, at which point the battery is almost completely spent.

So I made a change by adding a voltage bridge to the ADC pin of the ESP8266 to read the voltage of the battery pack:

The input voltage range of the ADC pin is 0 - 1V. By using the voltage bridge, and assuming worst case of using 4 x AA akaline (1.55 nominal voltage), the maximum voltage to the ADC pin is (1.5 x 4 x (10 / 57)) = 1.05V. To compute VCC, simply reverse the equation i.e. VCC = ADC * 57 / 10.

In my code, I try to get a more accurate result by discarding the first ADC reading, then averaging the next 8 readings.

float getSupplyVoltage() {
  int sum = 0;
  analogRead(A0);
  for (int i=0; i<8; i++) sum += analogRead(A0);
  sum = sum >> 3; // divide by 8
  float ratio = sum / 1024.0;
  return ratio * 57.0 / 10.0;
}

So the ESP8266 will compute the supply voltage every time it wakes up. If the supply voltage falls to 4.2V (~1.05V on each AA battery), it turns off the RTC clock signal and sleeps indefinitely.

The ATtiny85 no longer uses the ADC to detect low power condition. Instead, it checks that the incoming clock signal is not longer present and stops the clock. This means we can turn off the ATtiny's ADC, which should help save 0.2mA.

Stuck SDA on the I2C bus

During tinkering, I found that occasionally the ESP8266 will wake up and find the SDA line on the I2C bus stuck low. I suspect the problem is nicely summarized on this page, but I am not 100% sure. Getting to the root of the problem is, I think, beyond my current expertise.

I ran an experiment whereby the ESP8266 wakes up every  5 minutes and scans the I2C bus. Out of 1240 wakes, there were 125 times where the SDA line was stuck low. The code then attempts to clear the bus using the I2C_ClearBus() function found in the web page above. I tried 2 approaches:

1. Repeatedly call I2C_ClearBus(), wait 5 secs, initialize I2C again by calling Wire.begin(), then scan the I2C bus.

2. Call I2C_ClearBus(), deep sleep for 1 minute, then wake up and run the setup routine again.

Method (1) did not produce the desired result. Maybe the wait time was not long enough. Almost always, the I2C bus did not recover.

Method (2) did succeed with bus recovery. Most times, the I2C bus recovered within the minute. However, there were times when this went on 10 to 20 times before the bus recovered. In the worst case, 62 attempts (62 minutes) were required before the bus recovered. However, the good news is the bus always eventually recovered. Never once did the bus stayed permanently stuck, unlike (1).

Strangely, during actual runs which default to 2-hour interval between wakes, I was unable to obtain the same result. Mathematically, the occurrence rate should be about 10%, yet after a few hundred intervals, I did not observe a single stuck SDA. Not sure why, but I will continue to monitor the outputs. Also, just to be safe, I have incorporated the same I2C_ClearBus() routine anyway into the production code. If the bus is stuck, the ESP8266 calls I2C_ClearBus(), deep sleep for 1 hour, then wake up and try again.

Update: Spoke too soon. I have now started to observed occurrences of stuck SDA, with the worst case of 7 consecutive observations before resolution. 

Update #2: Switch back to calling I2C_ClearBus() at 5-minute intervals after stuck SDA detected.

Phantom power

Apparently, this is a common problem with I2C bus where multiple power sources are present. In this case, the ATtiny85 is powered by a supercapacitor. so when the battery power is removed, the SCL and SDA lines on the bus are still powered by the ATtiny85 aka. "phantom power"

These are my observations for the ESPCLOCK3 circuit with respect to phantom power.

For the ESP8266, due to phantom power, VCC is maintained at some residual voltage when battery power goes down. This may result in the ESP8266 not booting up at all when battery power is re-applied. The workaround to this is to press the reset button briefly to reboot the ESP8266.

For the RTC, what happens is the clock signal does not stop immediately when battery power is removed. Instead, it continues to supply the clock signal for a further 8~10s before dying down The result is the physical clock may continue to run for up to 15~18s when battery power goes down (10s for clock signal to stop, and 8s before WDT on the ATtiny85 triggers to check).

Power consumption

With the new setup (add voltage divider hardware, remove VCC voltage check from ATtiny85, add battery pack voltage check to ESP8266), the power consumption is between 1.20mA to 1.24mA, pretty much on par with before.

Note: The latest code and schematic has been updated on Github.

ESPCLOCK1 / ESPCLOCK2 / ESPCLOCK3 / ESPCLOCK4





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.