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.
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.
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
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.
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..
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.
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.
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.