You could also put the resistors from each switch in parallel and determine the resulting resistance to know if multiple switches were pressed at the same time
Great idea. I wasn't sure why you needed to add Rg (until later when you said it was to minimize current flow). So technically you could make it simpler without it.
Ironically I have an alarm clock that uses this principle. The only problem is sometimes pressing the time set button changes the alarm time. Turning the alarm on/off is a button clicking concerto in the morning.
Or, for N push=buttons, connect N+1 of all the same resistor value (doesn't really matter, just chose the sum to give maybe 0.1-10mA through the lot) and then use the switches to select a tap on the chain to the ADC input. No complex math, no problematic values. If the ADC is not reading the rail (put a big puller to one of the rails) then a switch is pressed, the voltage read tells you which.
So to use multiple switches at once, we can separate out the resistance to modulate the voltage output by 2^Rx, we can undo the math on the other end of the equation to essentially figure out which switches are closed, right?
cool! great idea did you use software to de-bounce the switches? it seems like if you carefully selected the resistors you could have values for multiple button press signals? too bad the esp8266 only has one adc, guess you would need a ADS1115 in that case if you needed the adc for other sensors
About multiple buttons pressed at the same time: Since his values' distribution is linear (you add 0.083V each time (14:55 or this times 5 idk)) he cannot. This is an efficient way to distribute values for single presses while leaving lots of room for imperfect physical resistors' values. However, as you noticed, there is a way- make the distribution exponential (you multiply by 2 each time). By choosing the base to be 2, you can easily implement it in the binary system on the software side (Divide all values by the 1st increment and represent the resulting number as a binary number- each consecutive bit is the state of each consecutive button.). The downside would be less buttons that you could theoretically use, since the increment at the lowest values could be too small (in the case of too many buttons) for the Arduino to notice. Idk what the smallest safe increment is (would require a little bit of testing), but it determines the maximum amount of buttons you can use. About de-bouncing: These are standard buttons, so if anything he had to de-bounce them in the software. There exist expensive buttons that don't bounce when pressing and releasing. You don't always need to debounce, but for logic or other sensitive purposes you absolutely have to. You can do it by either adding a delay (with 'millis()' preferably) or by making a bunch of x previous measurements into an array and either counting whether there is more high or low states or by taking an average of those x measurements and comparing it to 0.5 (less than 0.5 would be low and more than 0.5 would be high (0.5 itself can be high or low, depending on your personal preference), since in Arduino Reference LOW==0 and HIGH==1). This comment is mainly for future adventurers, however if you check it out after those 2 years and find it still helpful, I'd be glad. lol cheers, mate
You could also put the resistors from each switch in parallel and determine the resulting resistance to know if multiple switches were pressed at the same time
Great idea. I wasn't sure why you needed to add Rg (until later when you said it was to minimize current flow). So technically you could make it simpler without it.
Nice! takes me back to when I studied electronics 40 years ago but then switched to programming mainly because I found the maths hard to do
This is a ladder resistor I've just done this on 1 of my projects it's fine as long as you don't need to press more than 1 button at a time
Really nice video! Very well explained. I'll be using the math for a rotary switch that just so happens to be 12 position so the example is perfect!
Ironically I have an alarm clock that uses this principle. The only problem is sometimes pressing the time set button changes the alarm time. Turning the alarm on/off is a button clicking concerto in the morning.
Can this be done with rotary encoder?
Or, for N push=buttons, connect N+1 of all the same resistor value (doesn't really matter, just chose the sum to give maybe 0.1-10mA through the lot) and then use the switches to select a tap on the chain to the ADC input. No complex math, no problematic values. If the ADC is not reading the rail (put a big puller to one of the rails) then a switch is pressed, the voltage read tells you which.
So to use multiple switches at once, we can separate out the resistance to modulate the voltage output by 2^Rx, we can undo the math on the other end of the equation to essentially figure out which switches are closed, right?
Nicely explained, thank you 🙂
Does this work with toggle switches or will they flicker on/off?
cool! great idea
did you use software to de-bounce the switches?
it seems like if you carefully selected the resistors you could have values for multiple button press signals?
too bad the esp8266 only has one adc, guess you would need a ADS1115 in that case if you needed the adc for other sensors
About multiple buttons pressed at the same time:
Since his values' distribution is linear (you add 0.083V each time (14:55 or this times 5 idk)) he cannot. This is an efficient way to distribute values for single presses while leaving lots of room for imperfect physical resistors' values.
However, as you noticed, there is a way- make the distribution exponential (you multiply by 2 each time). By choosing the base to be 2, you can easily implement it in the binary system on the software side (Divide all values by the 1st increment and represent the resulting number as a binary number- each consecutive bit is the state of each consecutive button.). The downside would be less buttons that you could theoretically use, since the increment at the lowest values could be too small (in the case of too many buttons) for the Arduino to notice. Idk what the smallest safe increment is (would require a little bit of testing), but it determines the maximum amount of buttons you can use.
About de-bouncing:
These are standard buttons, so if anything he had to de-bounce them in the software. There exist expensive buttons that don't bounce when pressing and releasing. You don't always need to debounce, but for logic or other sensitive purposes you absolutely have to. You can do it by either adding a delay (with 'millis()' preferably) or by making a bunch of x previous measurements into an array and either counting whether there is more high or low states or by taking an average of those x measurements and comparing it to 0.5 (less than 0.5 would be low and more than 0.5 would be high (0.5 itself can be high or low, depending on your personal preference), since in Arduino Reference LOW==0 and HIGH==1).
This comment is mainly for future adventurers, however if you check it out after those 2 years and find it still helpful, I'd be glad. lol cheers, mate
What I did on my ladder resistor was use 16 samples I also used a capacitor. So you press the button and it gives you the average value over 16 loops
@@shokdj1 What should be the value of the capacitor when using it? I made a 16 button system. Which capacitor should I use?
@@ertugrulbt .1uf
@@ertugrulbt I’d like to add I’m no expert but that seemed to help for me
Nice project india👌
Issue is dirty switch contacts as they age causing errors.
Thank you!
Or you can spend $1 and use an expander and cut the code by about 90%. Debounce would be a nightmare and multi-press will cause a number of issues.
By expander do you mmean a multiplexer or something else?
💖💖💖💖