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

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

Hacking a USB-C to slim tip adapter cable to charge the Thinkpad T450s

This hack is inspired by this post . A year ago, I bought an adapter cable for my wife's Thinkpad X1 Carbon (2nd Gen) that allows her to power her laptop with a 60W-capable portable battery (20V x 3A). A USB-C cable goes from the battery into the adapter, which converts it to the slim tip output required by the laptop. Everything works out of the box, so I didn't give much thought about it. Recently, I decided to buy a similar cable for my Thinkpad T450s. I know technically it should work because the T450s can go as low as 45W (20V x 2.25A) in terms of charging (though I have the 65W charger - 20V x 3.25A).  I went with another adapter cable because it was cheaper and also I prefer the single cable design. So imagine my surprise when the cable came and I plugged it into my laptop and it didn't work! The power manager just cycle in and out of charging mode before giving up with an error message saying there is not enough power. After much research and reading the Thinkwiki

Using Google Dashboard to manage your Android device backup

I used to use AppBrain/Fast Web Install to keep track of which apps I have installed on my phone, and to make it easier to reinstall those apps when the phone gets wiped or replaced. But AppBrain had been going down the tubes, and Fast Web Install had always been a hit-and-miss affair. Android's own "backup to the cloud" system had previously been even more unusable. There isn't a place where you can see what has been backed up. And when you setup a new phone with your Google account, you just have to wait and pray that your favorite apps will be restored to the phone. Typically all the stars have to be aligned just right for this to happen. More often than not, after waiting for an hour or so and nothing happens, you just curse under your breath and proceed to install your favorites apps manually via the Play Store. But I just looked again recently and was pleasantly surprised that things are much more civilized now. Firstly there is a place now where you can loo