Calibrating analogRead for buttons on resistor ladder

All,

I'm getting ready to put together a small control panel with some buttons in a simple resistor ladder, and use the voltage reading across the circuit with each closed button via analogRead. However, as I'm working to design the pieces, I'm reading some information about the proper way to calibrate the readings from analogRead depending on whether the Arduino is powered by USB or external power supply.

What I think I'm reading is that calibrating the readings from the analogRead function while powered by USB is a bad idea because USB power can vary - but I thought the 5V pin on the Arduino was regulated regardless of source. Should I power my button pads from a separate/dedicated source?

I am doing this from scratch so I'd like to do it as "right" as possible from the start - I may be wiring up between four to six buttons on a few separate PCB strips.

Thanks.

The buttons use a resistor ladder so it doesn't matter what voltage you feed it as long as the same voltage is fed to the analog reference.

I would write a small sketch to display the analog reading. Write down the value for each button. The calculate the half-way point between each of the adjacent buttons. Use those values to decide the closest button.

1 Like

Yeah, the main thing is to look for a range of voltages instead of an exact voltage.

Or... Come-up with a better solution and don't use analog when you really want digital. :wink:

1 Like

For example, the DFRobot LCD Shield I have handy produced the following analogRead() results:

0 right
midpoint 50
99 up
midpoint 176
253 down
midpoint 329
406 left
midpoint 521
637 select
midpoint 828
1020 No Button

enum {NO_BUTTON, UP_BUTTON, DOWN_BUTTON, LEFT_BUTTON, RIGHT_BUTTON, SELECT_BUTTON};
int ReadButton()
{
int val = analogRead(A0);
if (val < 50)
  return RIGHT_BUTTON;
if (val < 176)
  return UP_BUTTON;
if (val < 329)
  return DOWN_BUTTON;
if (val < 521)
  return LEFT_BUTTON;
if (val < 828)
  return SELECT_BUTTON;
return NO_BUTTON;
}

Oh there's no question I'll be using ranges for buttons, no magic numbers or expectation of exact I was really just concerned about whether the ranges should be established with regulated external power or if USB were acceptable.

I'd love to use a pure digital solution, but my protect will probably be using components that will claim several of the digital pins.

Buttons can make bad contact, and then you get into trouble.
Please don't use a resistor ladder for buttons :pleading_face:
For example with a I2C I/O expander you can get extra digital pins, but there are many other ways to get more pins.
Most analog inputs can be digital pin as well.

I don't understand. I've seen plenty of discussion and examples of doing this. It seems pretty simple to me. May I ask why do you say not to?

Buttons and a resistor ladder on a single analogue pin works fine for a short-lived project.
The problems start when the switches eventually start to oxidize.

The Arduino A/D in default mode does not measure voltage (as most of us think).
It returns a ratio of it's supply.
And since the ladder also returns a ratio of it's supply, the A/D values are independent of supply voltage.
Leo..

2 Likes

I would like to look at the wiring principle for the buttons. What are the button readings showing when a button is not pressed?

Thanks! That's good having that 100k resistor to GND. If there's no problem, go on. Else reduce to 47k, or even 27k.
I checked that math some time ago and used like 10k. The intervalls are still good enough.

Contacts oxidizing, pressing two or more buttons at the same time, take a sample while that 0.1µF capacitor is still charging. There are too many things that can go wrong.

If you press a button and the Arduino acts as if another button is pressed, that would be very annoying.

My twenty-year-old TV remote control did not oxidize yet, my old keyboard is working well.

TV remote controls use a button matrix, not a resistor ladder.
Leo..

1 Like

So they are contactless and there is nothing to oxidize

Resistor ladder for buttons ?

  • No, never.
  • Yes, it is a smart solution.
  • I don't like this poll.

0 voters

No, a matrix is digital. A resistor ladder is analogue.
Leo..

1 Like

For my purposes, a resistor ladder will work fine. This is a limited use project for personal education and entertainment. The potential long term issue of button oxidation is not a legitimate concern for this project. Thanks for offering the concern.

In the vein of education, I might at some point consider an IR based solution. But that's for a different thread.

Hi,
Your poll does not cover,

It has it place in some projects.

This not a cut and dried solution.
It is used on some LCD display shields to simplify and save pin allocations.
The code to read them is simple and can be made into a function.

Tom.... :grinning: :+1: :coffee: :australia:

1 Like

Don’t forget you’ll get weird results if you press more than one buttons. I too would go for digital, with an I2C expander if need be .

You can set analog inputs to be digital if you want