I'm very new on my electronics journey so I'd like some guidence please.
Is the below concept possible (but probably not the execution) ?
How could I achieve it properly?
I'm playing with the classic Arduino robot car and I'm moving on to the crash-detect bumpers. As I want to keep some pins free, I thought I might be able to use a single analogue-in pin to trigger different responses depending on the current/voltage it receives.
The assumption is that I have 3 push switches ( left, middle and right) all connected across the same two wires, with a resistor added in between them in series. For example the left switch goes through 1x220 ohm, the middle switch goes through 2x220 ohm and the right switch goes through 3x220 ohm. The picture below may convey the concept, but is likely to be electronically bad.
Hopefully the analogue pin can detect the difference between each switch due to the difference in current/voltage, then I can respond differently in the code.
I tried this some time ago on a ESP32 for direction keys + OK. I tried to detect multiple keypresses, so the switches were parallel to the resistors. The jitter of the ADC ruined everything In the end I used 1x1k pullup and 4x1k for the buttons (same design as in 1. posting) and direct to GND for "OK".
Yes, you need a voltage ladder and you can measure the voltage (via ADC).
But you cannot realize multiple keys pressed with this schematic provided by LarryD: S2 would already go to GND, never mind if S6 is also pressed (to GND).
Instead, you can put the switches (S2...S6) in parallel to the R2... R5:
now, you would shorten the resistors and change the voltage. Now you can see different voltages, also a double key press (resulting in another voltage).
If switch S2 is closed the bottom of R6 is at GND.
We therefore have 5V across R6.
5V / R6 (2k) = the current flowing through R6.
5V / 2K = 2.5mA when Switch S2 is closed.
2.5mA is not a lot of current; the UNO 5V regulator can supply ~500mA (with Vin = 7-9V).
We could have used 20k, then the S2 closed current would be 250uA.
However you, the designer, need to decide if very low current is necessary.
The less the current, the more likely external electrical noise can effect readings.
With the selected resistors seen in the schematic, RAW A/D values are: 0, 145, 329, 505 and 741.
The different between the values is reasonably large and almost equal ~150 to 250.
Remember also, the external ladder supply voltage can vary some.
You should add window/range to your code when you decide a switch is closed as seen below.
#define ButtonWindow 10 //range for a valid button sensing
. . .
//are we within range of the BUTTON_UP RAW value (between 145-10 = 135 and 145+10 = 155) ?
if (buttonVoltage >= (UP_10BIT_ADC - ButtonWindow) && buttonVoltage <= (UP_10BIT_ADC + ButtonWindow))
{
button = BUTTON_UP;
}
. . .
Thank you @LarryD, that makes sense now. I believe I've got enough knowledge to move forward.
I was planning to use an external 5V supply (Some batteries with a buck converter), so I believe I have enough power, I was perhaps mixing up how much power I could deliver to the A1 pin, not from the 5V. I think the concepts are still a little fuzzy in my head in that respect.
My apologies if my comment implied wrongdoing on somebody else's part, it was a self criticism. I added a comment because I edited the post around 4 times, but I've now removed it.
I was falling into the trap of just asking for all the answers, without putting some effort in myself first. Only a little research answered the question I had asked in one of the edits.
For me, electronics and microcontrollers are a new hobby and a chance to learn something new, so just asking for all the answers is cheating