The reason that the transistor is on the high side of the divider is because otherwise the ADC input will be exposed to the battery voltage when the transistor is "open"(high impedance).
Using a high side transistor(PMOS) introduce another problem. To be able to put it in "open" state, the gate(1) - source(2) voltage needs to be <=0V. A 3.3V µcontroller can't put out say 4.2V like in a fully charged LiPo battery. One way could be to but transistor in between to step up the voltage, but I found one way that works really good in this case and it is cheaper by using a capacitor instead as the step-up stage.
It works in the way that the capacitor is conducting the AC part of the signal on ADC_ON. So falling and rising edge on a pulse will "jump over" the capacitor and "open/close" the transistor. According to some forum post a falling edge will "close" the transistor for 2 ms, enough time to make one sample of the voltage on the ADC pin.
Here is some example code I have used with success:
digitalWrite(15, LOW); //Close transistor
int sensorValue = analogRead(A0); //Measure
digitalWrite(15, HIGH); //Open transistor
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (6.85 / 1023.0);
ADC_ON is connected to pin 15 on the ESP.
I tried with delay(1) between LOW and READ but that was to long. This code have worked without problems.
The 6.85 value is the value that corresponds to the battery value when the ESP measures 1V, but this was tested on one of my units and your miles my vary.
Tip: connect to a good power supply and check what this code read and the calibrate the 6.85 value to something that works good for you.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.
This is awesome. I was looking for something like this. I run some simulations and it looks cool. With 100n + 10k it was bit over 1ms of time for measurement.
I will go for 100n+22k pullup, that will make 3.4ms. All with transistor's Vgs(th) around 1.3V, with lower Vgs(th) there is more time.
Are you sure? yes | no