Unusual readings trying to use multiple buttons on one analog pin.

Not to take anything away from the search for a solution to your problem, but if you only need to accommodate four buttons, you can do this without any gymnastics, because analog inputs can also be used as digital inputs. Just hook each switch up to an analog pin and use A0 through A5 as the pin numbers from which you digitalRead() and that you call pinMode() on. Treat A0-A5 like any other digital pin.

For $10, you can get an SPI/I2C "backpack" for your LCD that allows you to write to the screen using either SPI (three digital pins used) or I2C (two digital pins used). This will conserve your digital pins for other uses. Alternatively, you can effectively increase your inputs by using a 4051 mux chip. A software library exists to allow you to easily read from the 4051 using analogRead() or digitalRead() calls, basically exactly as if you were reading from a directly-connected pin. The 4051 is good for both reading and writing, but it can only select a single line at a time. For devices, like LEDs, or like the chip-select line on an SPI device, where you need to supply constant voltage, a shift register is the right choice.

Between a 4051 mux and a shift register, you can basically have as many inputs and outputs as you want, with only a few digital and analog pins used.

One last thing: if you already have one SPI device in your project, you could benefit from using an SPI-addressable digital-to-analog converter chip, instead of the 4051 analog mux. The first SPI device you add to your project uses three digital pins: data in, data out, and clock. If you only have one SPI device, you just ground the chip-select line and permanently select the chip. If you have more than one SPI device, you need to run a chip-select line to each of the devices, so the second SPI device you add means you're using five pins (data in, data out, clock, chip-select 1, chip-select 2). And after that, each additional SPI device only uses another chip-select pin. So, for example, if you had an SPI-addressable LCD display, it would be using three digital pins. If you also wanted to add a 4051 mux, it would be using three digital pins to select the input. But if you added an SPI-addressable ADC chip instead of the mux, it would only use two additional digital pins (the two additional chip-select pins you have to add in order to get a second SPI device on your project). This is the real advantage of SPI--you can add a lot of devices to the same data bus, without using up a ton of digital pins. If you start running out of chip-select pins, just use a shift register to choose which SPI device is active.

joshuabardwell:
Not to take anything away from the search for a solution to your problem, but if you only need to accommodate four buttons, you can do this without any gymnastics, because analog inputs can also be used as digital inputs. Just hook each switch up to an analog pin and use A0 through A5 as the pin numbers from which you digitalRead() and that you call pinMode() on. Treat A0-A5 like any other digital pin.

For $10, you can get an SPI/I2C "backpack" for your LCD that allows you to write to the screen using either SPI (three digital pins used) or I2C (two digital pins used). This will conserve your digital pins for other uses. Alternatively, you can effectively increase your inputs by using a 4051 mux chip. A software library exists to allow you to easily read from the 4051 using analogRead() or digitalRead() calls, basically exactly as if you were reading from a directly-connected pin. The 4051 is good for both reading and writing, but it can only select a single line at a time. For devices, like LEDs, or like the chip-select line on an SPI device, where you need to supply constant voltage, a shift register is the right choice.

Between a 4051 mux and a shift register, you can basically have as many inputs and outputs as you want, with only a few digital and analog pins used.

One last thing: if you already have one SPI device in your project, you could benefit from using an SPI-addressable digital-to-analog converter chip, instead of the 4051 analog mux. The first SPI device you add to your project uses three digital pins: data in, data out, and clock. If you only have one SPI device, you just ground the chip-select line and permanently select the chip. If you have more than one SPI device, you need to run a chip-select line to each of the devices, so the second SPI device you add means you're using five pins (data in, data out, clock, chip-select 1, chip-select 2). And after that, each additional SPI device only uses another chip-select pin. So, for example, if you had an SPI-addressable LCD display, it would be using three digital pins. If you also wanted to add a 4051 mux, it would be using three digital pins to select the input. But if you added an SPI-addressable ADC chip instead of the mux, it would only use two additional digital pins (the two additional chip-select pins you have to add in order to get a second SPI device on your project). This is the real advantage of SPI--you can add a lot of devices to the same data bus, without using up a ton of digital pins. If you start running out of chip-select pins, just use a shift register to choose which SPI device is active.

This is extremely helpful information. I had no idea that these backpacks existed for LCD screens. While a 4501 may be overkill for this project, it is also really good to know I can use those for other projects. I am going to order one of the backpacks right now.

The top resistor of the switch network has a red wire going directly from the + rail to A5.
There should be another resistor there so that the A5 rail is not directly connected to the power rail.

Also if you are powering all this of a 3.3 volt rail, but have the top part of the chain connected to 5 volts, you will actually be causing 5 volts to go into the A5 pin. Thats bad.

But the immediate fault i can see is that your A5 rail is directly connected to the positive rail.
Try connecting A5 to the first switch instead.

Hi,
I use 4 x 10K resistors from 5V to GND and put a switch at each junction, so 3 switches. The analogue input is 0 to 1023 and I get the following values 144, 248 and 436. There is nothing really from you making that 6, 8 or 10 switches/resistors, etc. 5v div 1023 = 5000/1023 = 488mv soon allows you to work out the value for each switch. A 10 resistor from the switch common to GND means no switch press is 0v, or zero analogue.

Hope it helps, regards.

Mel.

In this situation, what happens if one presses more than one button at a time? Seems like you would end up with a parallel resistor situation. If this hashed out to the same voltage range as another button, you would end up with a misread. But perhaps with clever selection of resistor values, you could come up with a scheme that allows exact determination of all combinations of button presses. Or maybe I'm over-thinking it.

I ignore multi button presses. There is really no situation requiring it, and if there is, you use a multiplexer or the digital inputs.