{"id":212,"date":"2014-12-18T00:16:37","date_gmt":"2014-12-18T00:16:37","guid":{"rendered":"http:\/\/int03.co.uk\/blog\/?p=212"},"modified":"2014-12-18T00:16:37","modified_gmt":"2014-12-18T00:16:37","slug":"reading-multiple-switches-with-one-analogue-input","status":"publish","type":"post","link":"https:\/\/int03.co.uk\/blog\/2014\/12\/18\/reading-multiple-switches-with-one-analogue-input\/","title":{"rendered":"Reading Multiple Switches with one Analogue Input"},"content":{"rendered":"<p>The traditional way to connect a button to a GPIO pin is to have a pull up resistor to Vcc, and have the switch pull the GPIO to ground when it&#8217;s pushed. The catch is that you need one pin for every button. An alternative is to use a matrix: for example, using 4 outputs and four inputs to scan a 16 button keypad. To reduce the number of GPIOs still further, we can use a resistor ladder, something like this example below.<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/12\/resistor_ladder_3switches.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-213 size-full\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/12\/resistor_ladder_3switches.png\" alt=\"3 Switch Resistor Ladder\" width=\"817\" height=\"700\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/12\/resistor_ladder_3switches.png 817w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/12\/resistor_ladder_3switches-300x257.png 300w\" sizes=\"(max-width: 817px) 100vw, 817px\" \/><\/a><\/p>\n<p>The diagram above is an attempt to use the minimum number of resistors, and a more optimal design would be possible with additional resistors.<\/p>\n<p>When all the switches are open, we will see 3V3 at the analogue input AIN. When a switch is closed, it connects one of the resistors to ground, forming a potential divider and reducing the output voltage.\u00a0For example, if we press S2 then the output voltage will be half the supply voltage.<\/p>\n<p>When multiple switches are closed, the resistors are connected in parallel, so we will get a different value for each combination of switches. Provided the values are sufficiently different, we can detect all possible switch combinations.<\/p>\n<p>It&#8217;s fairly easy to create a spreadsheet to calculate the output voltage for a given set of resistors, and switch states. The table below shows the predicted output voltage, and the actual measured voltage for every switch combination:<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/12\/3switch_voltages.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-215\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/12\/3switch_voltages.png\" alt=\"Voltages with 3 switch ladder\" width=\"343\" height=\"217\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/12\/3switch_voltages.png 343w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/12\/3switch_voltages-300x189.png 300w\" sizes=\"(max-width: 343px) 100vw, 343px\" \/><\/a><\/p>\n<p>The differences between the actual and predicted voltages are due to resistor and supply voltage tolerances. These show fairly good agreement if they are normalised to supply voltage.<\/p>\n<p>This is what the results look like when graphed. Note that the results are not linear, but the values are sufficiently different to be able to reliably detect different key combinations.<\/p>\n<p><a href=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/12\/3switch_voltages_graph.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-216\" src=\"http:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/12\/3switch_voltages_graph.png\" alt=\"Voltage graph for 3 switch ladder\" width=\"481\" height=\"289\" srcset=\"https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/12\/3switch_voltages_graph.png 481w, https:\/\/int03.co.uk\/blog\/wp-content\/uploads\/2014\/12\/3switch_voltages_graph-300x180.png 300w\" sizes=\"(max-width: 481px) 100vw, 481px\" \/><\/a><\/p>\n<p>Having taken the analogue reading from the sensor, it can be compared to the possible values shown above. This can either be achieved with a look up table, or by fitting a function to the data. Having obtained the integer code 0 to 7, the state of an individual button can be detected by testing individual bits of that code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The traditional way to connect a button to a GPIO pin is to have a pull up resistor to Vcc, and have the switch pull the GPIO to ground when it&#8217;s pushed. The catch is that you need one pin for every button. An alternative is to use a matrix: for example, using 4 outputs &hellip; <a href=\"https:\/\/int03.co.uk\/blog\/2014\/12\/18\/reading-multiple-switches-with-one-analogue-input\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Reading Multiple Switches with one Analogue Input<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/posts\/212"}],"collection":[{"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=212"}],"version-history":[{"count":1,"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/posts\/212\/revisions"}],"predecessor-version":[{"id":217,"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/posts\/212\/revisions\/217"}],"wp:attachment":[{"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/int03.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}