Reading voltage through A0 pin

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
oserror
Posts: 43
Joined: Tue Feb 12, 2019 12:26 am

Reading voltage through A0 pin

Post by oserror » Thu Jun 27, 2019 10:54 am

Just yesterday, I finished making this project:
https://www.instructables.com/id/Solar- ... ation-V20/

I rewrote the code in MicroPython and I'm fairly pleased with the results. What does not seem to work is the voltage detection.

There is a 220k resistor between the battery and the A0 pin. I used this code from the MicroPython docs to read the data. The comment is from the original code:

Code: Select all

# Battery Voltage
# Voltage Divider R1 = 220k+100k+220k = 540K and R2 = 100k
calib_factor = 5.28
adc = ADC(0)
raw = adc.read()
result['volt'] = raw * calib_factor / 1024
output.append('Voltage: %.2f V\n' % result['volt'])
This always results in 5.28. I looked at my soldering connections yesterday and I thought that there were no bridges, but I may have made a mistake there. The board is a Wemos D1 Mini Pro. I thought there might be a bridge between A0 and RST or D0. I'm going to check this -- I had a heck of a time mounting the board to the case with tiny M2 screws; I had to use tweezers, it took me 30 minutes for 4 screws!

Does this seem likely or do I have an issue with the code I posted above?

Thanks!

User avatar
oserror
Posts: 43
Joined: Tue Feb 12, 2019 12:26 am

Re: Reading voltage through A0 pin

Post by oserror » Thu Jun 27, 2019 11:48 am

Could it be that the brand new battery is supplying a high voltage, maybe higher than 5V? It is an 18650 going through a TP4056. The 220k resistor is connected to the output voltage of the TP4056 and goes to pin A0 (as well as going to VCC) of the Wemos.

Everything else is working -- I just always wonder if I messed something up after I solder a circuit together. It could be that everything is fine.

I have the data going to Thingspeak and I am not seeing a decrease in voltage overnight, but the thing only runs for about 10 seconds every ten minutes. During the day it has a solar panel to recharge.

I don't know why A0 would read 1024 all the time though. When there was no battery hooked up, I got various low values.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Reading voltage through A0 pin

Post by Roberthh » Thu Jun 27, 2019 3:29 pm

A fully charged 18650 Li-battery has a voltage of about 4.2V. What I do not understand from you post: did you add an additional 220k resistor between the TP4056 output and A0 (obsolete: ,or do you connect the output of the TP4056 directly to A0. In the latter case, it is no wonder that you always read 1024: The voltage divider in the Wemos D1 Mini expands the ADC range to 0-3.3V. Any value above that results in a 1024 reading. You should add another resistor between the TP4056 output and A0, at least 100k, but better 180k. Then you have a simple 5:1 division ratio. )
You should be aware that even then the voltage divider consumes about 7 µA current, which is about 600 mAh per day, or 20% of a typical 18650 Li battery charge.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Reading voltage through A0 pin

Post by Roberthh » Thu Jun 27, 2019 3:36 pm

Looking at the schematics https://wiki.wemos.cc/_media/products:d ... v2.0.0.pdf, the board has a solder jumper SJ1, which connects the TP405x output internally via a 130k resistor. If that one is closed, and you have you rexternal 220k resistor too, then the resulting resistor is 81.7 k, resulting in a 0-4V range. What do your read when you remove the external 220k?

User avatar
oserror
Posts: 43
Joined: Tue Feb 12, 2019 12:26 am

Re: Reading voltage through A0 pin

Post by oserror » Thu Jun 27, 2019 3:59 pm

Robert, thanks for your detailed and quick reply.

I made a dumb error. I looked again at the resistor pack and I had 220 Ohm instead of 220 KOhm. I went to Radio Shack since I posted, managed to get the old one off (no small feat for me!) and I'm back in business.

Case closed!

User avatar
oserror
Posts: 43
Joined: Tue Feb 12, 2019 12:26 am

Re: Reading voltage through A0 pin

Post by oserror » Thu Jun 27, 2019 7:49 pm

Hi Robert,

I'm using your BME280 library and it works great! By default, it uses Forced mode, correct? It takes a sample and then goes to 'sleep'? I looked at one of your calculations and that seems to be the case.

I've had just the basic Wemos and BME280 circuit on my breadboard for a couple of weeks while I worked on the Micropython code. I just froze the modules after a battle with the openSDK compilation and everything seems to be working.

About the A0 voltage: I have:
raw = adc.read()
voltage = raw * 5.28 / 1024

This calculation is giving me 3.91 volts. I measured outside and got 3.80 volts (not sure how accurate my meter is but at least 3.8 is probably accurate). So, if the scale remains proportionate (which I don't know), my factor would be 5.1315? I suppose I might need a few points, but maybe a regression is overkill. :)

One other thing. This is certainly not a high-priority project but the voltage seems to decrease over 30 minutes by 0.01 volt if the sky is cloudy. I have not had this run overnight yet but I suspect the battery might drain quickly. I know the Wemos is going to sleep. I'm unsure about the BME280. You also mentioned the power consumption of the voltage divider being roughly 20% of the battery's power. How long can this thing run without some sun?

I'm having fun with this. I even got solder out of two holes today with my hand tremor! using a spring-loaded solder sucker. I've never done that before.

I hope I didn't ruin anything on the Wemos by exposing A0 to overvoltage!

Thanks again for your help!

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Reading voltage through A0 pin

Post by Roberthh » Thu Jun 27, 2019 8:35 pm

Thank y lot. Actually the BME280 driver is just a port of the Adafruit driver. I invested some time to compare the calculations in the code with the original Bosch specifications and made a PR to adafruit for a change.
The ADC of the ESP8266 is not very precise. Even the full range voltage varies between devices. So using an reference measurement for calibration is appropriate. According to the data sheet, the power consumption of the BME280 is below 0.5 µA between measurements. So that will not drain the battery. If you use deepsleep for the ESP8266, that should also reduce the power consumption. On the other hand, 0.01V is not that much of a power decrease, and looking just at that short time will not tell you a lot.

Update: Looking at the schematics it seems that both the voltage regulator and the CH340G are still supplied, when there is power at Vin or VBat. The former draws at least 40µA, bit the CH340 is good for 50-150µs in sleep condition.That should account to about 200µA, and that is what I get during deepsleep on my Wemos D1 Mini, when powered through Vin at 3.6V.
So that will be the largest standby power sucker, still good for about 20 months from a 3000 mAh battery.

P.S.: I was wrong by a factor of 3600 (hrs vs. seconds) with the estimation of the energy used by the voltage divider. A 3000mAh battery can supply that quite long, about 48 years.

Post Reply