I need to read the input from 8 different push buttons on an ATtiny microcontroller. After researching how to do this with the least number of pins, I found out about using a voltage divider with resistors and reading the input voltage with an analog pin.

This seems to work fine "in the lab", as I'm getting a constant voltage from a power supply or an Arduino.

Now, I was thinking that, since the analog reading is based on the voltage provided, if this voltage changes then the reading won't be the same anymore, right?

I do all my schematics and code using a 3.3 V input, but then I want the final project to run on two 1.5 V batteries connected in series (~3 V). The voltage will be changing depending on the charge of the batteries, so my code won't be able to register the button presses properly anymore.

If this is the case, what solutions or alternative methods can I implement? I don't think using one pin per button is feasible.

The ADC for the ATTiny devices can be selected to use the VCC pin as the voltage reference. Given you are only interested in measuring ratios between resistors, this option would be the one to use (as opposed to the internal band-gap reference(s)).

This mode means that the code values recorded by the ADC will always be proportional to the supply voltage, whatever it is (i.e. for 5V supply then 5V => 1023, for 3.3V supply then 3.3V => 1023).

If your button potential divider is also sourced from the same VCC, then you should get the same code ranges for each push button regardless of the supply voltage.

The only slight problem with the lower voltage is that noise will have a larger influence on the code values - if you had say 50mV noise for a 5V supply, that corresponds to 10 codes. For a 3.3V supply, that same 50mV noise corresponds to 16 codes. This probably wont be an issue for 8 buttons.

• This is very useful information, and exactly what I need in order to achieve reliable readings while (potentially) having different input voltages. Thank you so much :) Commented Mar 18, 2023 at 10:43