With my arduino I want to measure if one / two AAA batteries are in their single / double battery holder.

When the Arduino is running, connected to some other power supply and the batteries are in place everything is fine: I get my measurements of around 1.5V / 3V on the analog pin the battery is connected to (- of battery and Arduino GND are connected as well of course).

Problems arise when I either

a) pull the batteries out: The analog pin is left floating. Not good. Can I put the pin into INPUT_PULLUP mode? Is the internal resistor big enough? Or will the then connected 5V damage my batteries when they are back in the battery holder?

b) dissconect the Arduino from its external power supply: Because the Arduino is then "powered" by the 3V through the analog pin my blinking LED keeps blinking (at less brightness, but still...). I neither want the batteries to get drained nor damage my Arduino with it. So I would like to avoid this.

Actually I don't care about the actual battery voltage - I just want to know if the batteries are in their holder or not. And I want to leave the batteries there even though the Arduino is not powered. I would even consider something like "a button is held down when a battery is in its holder" or any other (simple) creative ideas.

Is this possible? How?

  • do you want to use batteries as a [backup] power source or not ? if not (and you didn't connect the + of batteries to arduino supply line), why the arduino is powered by them when you disconnect the main power source in section b of your question? Aug 31, 2019 at 21:43
  • @Tirdad Sadri Nejad That is a parasitic current through the input pin. Not good for the microcontroller.
    – chrisl
    Aug 31, 2019 at 23:46

2 Answers 2


I see 1 main problem: Keeping the batteries isolated, so that no current flows between them and the Arduino (especially no parasitic current into the input pin of a turned off Arduino), while still reading a voltage level (HIGH or LOW).

I would suggest using a fitting N-Channel MOSFET. Connect the minus pin of the batteries to ground and the plus pin to the gate of the MOSFET. Then source and drain of MOSFET to ground and a digital arduino pin respectively. Enable the internal pullup resistor. Add a pulldown resistor between gate and ground.

Now, without the batteries, the gate is at ground level, so the MOSFET is not conducting. The level at the input pin will be high. If the batteries are in, their voltage will activate the MOSFET, which pulls the Arduinos input pin to ground, thus LOW level. Since the MOSFET is somewhat like a capacitor between gate and the rest, almost no current will flow here (only enough to load the small capacitance). Though I'm not sure, I think that no current will flow either in the case of the Arduino being unpowered.

The task now would be to find a fitting MOSFET, which goes in saturation for 1.5V.

Of course you can also use a microswitch, that gets pushed down by the batteries. That is electronically easier, but maybe mechanically challenging.

  • Sounds reasonable, thanks! I tried to draw a schematic off your instructions: i.ibb.co/pW7n4D6/sch.png Is that what you thought of? Now I go find a MOSFET and try it :-)
    – Tetopia
    Sep 1, 2019 at 10:39
  • Almost. I intended to connect to a digital pin instead of an analog one, since you only need information, if there are batteries or not. Doing this electrically also means, that empty batteries are like no batteries.
    – chrisl
    Sep 1, 2019 at 15:16
  • I built this circuit with a "ZVN3310A", a 560k Pulldown and internal pullup. Seems to work :-) For the one-battery-version I had to measure the analog value though, because the transistor I got has a VGS(th) of 0.8 to 2.4V and the 1.5V of a single battery were just not enough to make the transistor fully conductive (I had no other transistor available). But it conducted enough to get the analog value from about 1000 (no battery) to about 700 (1.5V battery). All LEDs stay off even with a 3V battery when no power is connected, so I guess there is no (significant) parasitic current.
    – Tetopia
    Sep 4, 2019 at 0:00
  • Yes, I assumed a transistor, that would be already fully conductive for 1.5V, but if you don't have a better transistor, using analog input is also fine.
    – chrisl
    Sep 4, 2019 at 6:50

You could add a pulldown to the analog pin. If the ADC reads a value below e.f. 10, you could consider this the state when there are no batteries.

From the signal perspective a pullup would be better, because without batteries you would read 5V which is unambigous as it can not be provided by the batteries themselves. But in general I don't think that applying higher voltage to the batteries is good practice, even though you could limit the current with a reasonably high resistor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.