New: the AI firewall watches the whole platform and explains each incident in plain words. See how it works

Reply To: Measuring a 12 V battery without the divider draining it, and a low-battery alarm that flaps in the sun

Aisha, the single low-side MOSFET has a catch I’ve been bitten by. When it’s off, the bottom of the divider floats, and the top resistor pulls the ADC pin up to the full 12 V through 100k. That’s above the ESP32’s 3.3 V limit; the pin’s protection diode clamps it and current leaks into the 3.3 V rail. So you save the divider current and stress the pin instead.

The two-transistor version switches the top: a P-channel (AO3401 or similar) between the battery and the divider, gate pulled up to the battery with 100k so it’s off by default. A small N-channel (2N7000) driven by a GPIO pulls that gate down to switch it on. You need the N-channel because a 3.3 V GPIO can’t pull the gate of a P-channel sitting at 12 V high enough to turn it off. With the top switched off, the whole divider sits at 0 V, the ADC pin too.

In the firmware: switch on, wait about 10 ms, take 16 readings and average them, switch off. Use the calibrated reading (analogReadMilliVolts) and send battery_v as a number with two decimals.

For the flapping: a lead-acid battery’s voltage jumps when charging starts, so a plain threshold will always bounce. Give the rule hysteresis so it only clears well above the threshold, and two severities:

{
  "type": "battery_low",
  "title": "{{device.name}}: battery {{value}} V",
  "severities": [
    { "severity": "MAJOR", "condition": { "key": "battery_v", "op": "<", "value": 11.5 } },
    { "severity": "MINOR", "condition": { "key": "battery_v", "op": "<", "value": 11.8 } }
  ],
  "hysteresis": 0.4,
  "debounce": { "mode": "count", "raise": 2, "clear": 2 },
  "notify": { "recipients": [ { "kind": "email", "email": "field-team@example.com" } ], "minSeverity": "MINOR" },
  "enabled": true
}

It opens below 11.8 and clears only at 12.2 or above, two readings in a row. Kevin’s idea would hide the problem, not fix it. Hysteresis and debounce are explained on this page.

Accepted solution♥ 9