@@ -103,10 +103,14 @@ uint8_t System::readTouchpad(uint8_t _pad)
103
103
*/
104
104
double System::readBattery ()
105
105
{
106
+ // Read the pin on the battery MOSFET. If is high, that means is older version of the board
107
+ // that uses PMOS only. If it's low, newer board with both PMOS and NMOS.
106
108
pinModeInternal (IO_INT_ADDR, ioRegsInt, 9 , INPUT);
107
109
int state = digitalReadIO (9 , IO_INT_ADDR);
108
110
pinModeInternal (IO_INT_ADDR, ioRegsInt, 9 , OUTPUT);
109
111
112
+ // If the input is pulled high, it's PMOS only.
113
+ // If it's pulled low, it's PMOS and NMOS.
110
114
if (state)
111
115
{
112
116
digitalWriteInternal (IO_INT_ADDR, ioRegsInt, 9 , LOW);
@@ -115,15 +119,15 @@ double System::readBattery()
115
119
{
116
120
digitalWriteInternal (IO_INT_ADDR, ioRegsInt, 9 , HIGH);
117
121
}
118
- /*
119
- #ifdef ARDUINO_ESP32_DEV
120
- digitalWriteInternal(IO_INT_ADDR, ioRegsInt, 9, LOW );
121
- #else
122
- digitalWriteInternal(IO_INT_ADDR, ioRegsInt, 9, HIGH);
123
- #endif
124
- */
125
- delay ( 1 );
126
- int adc = analogRead ( 35 );
122
+
123
+ // Wait a little bit after a MOSFET enable.
124
+ delay ( 5 );
125
+
126
+ // Set to the highest resolution and read the voltage.
127
+ analogReadResolution ( 12 );
128
+ int adc = analogReadMilliVolts ( 35 );
129
+
130
+ // Turn off the MOSFET (and voltage divider).
127
131
if (state)
128
132
{
129
133
digitalWriteInternal (IO_INT_ADDR, ioRegsInt, 9 , HIGH);
@@ -133,19 +137,8 @@ double System::readBattery()
133
137
digitalWriteInternal (IO_INT_ADDR, ioRegsInt, 9 , LOW);
134
138
}
135
139
136
-
137
- /*
138
-
139
- #ifdef ARDUINO_ESP32_DEV
140
- digitalWriteInternal(IO_INT_ADDR, ioRegsInt, 9, HIGH);
141
- #else
142
- digitalWriteInternal(IO_INT_ADDR, ioRegsInt, 9, LOW);
143
- #endif
144
- */
145
- // Calculate the voltage using the following formula
146
- // 1.1V is internal ADC reference of ESP32, 3.548133892 is 11dB in linear
147
- // scale (Analog signal is attenuated by 11dB before ESP32 ADC input)
148
- return (double (adc) / 4095 * 1.1 * 3.548133892 * 2 );
140
+ // Calculate the voltage at the battery terminal (voltage is divided in half by voltage divider).
141
+ return (double (adc) * 2.0 / 1000 );
149
142
}
150
143
151
144
#ifndef ARDUINO_INKPLATE2
0 commit comments