05-08-2020, 04:07 PM
@pgzh1
My apology for this lengthy word description but I discovered after a few minutes that my favorite diagramming tools are great for circuits and lousy for diode matrices.
Let's consider the 8-position switch I mentioned. It will be obvious from the following how to use switches with fewer or more positions.
Eight states can be represented by three bits: 000, 001, 010,..., 111, which we old minicomputer programmers think of as octal 0-7.
So take three GPIO input pins pulled down to ground internally (and maybe with external 10K pulldown resistors to ensure adequate diode current flow).
Wire the common pole of the single-pole 8-position rotary switch to +V (probably +3.3V for RPi GPIO).
Position 1 is left unwired. (state 000)
Position 2 is connected via 1 diode to input pin 1 (state 001).
Position 3 is connected via 1 diode to input pin 2 (state 010)
Position 4 is connected via 2 diodes: one to input pin 1, one to input pin 2 (state 011).
Position 5 is connected via 1 diode to input pin 3 (state 100).
Position 6 is connected via 2 diodes: one to input pin 1, one to input pin 3 (state 101).
Position 7 is connected via 2 diodes: one to input pin 2, one to input pin 3 (state 110).
Position 8 is connected via 3 diodes: one to input pin 1, one to input pin 2, one to input pin 3 (state 111).
In all cases, the diode's anode is wired to the switch position and the cathode to the input pin. (If one decides to reverse the sense of the circuit so that pins are normally pulled up to +V and the switch/diode selections pull them down to ground, then the diode directions must be reversed.)
That's 12 diodes (and 3 resistors). Easy peasy for a hacker's junkbox like the one in my basement.
Usually this is represented in a rectangular diode matrix aka diode crossbar form (look up "diode matrix").
It can be extended to more states but the diode count keeps mounting. More likely one would use 7400-series OR gates, a bespoke 7400-series IC encoder, or even a microcontroller chip (there's a ton of nice little chips-on-boards out there, all of them cheap and easy to program).
Things to think about:
- Garden variety diodes have an internal voltage drop of about 0.6V, so a GPIO input pin would see about 2.7V in the high state if we wire to +3.3V. As I understand the RPi docs, the voltage threshold for a GPIO pin is guaranteed to be somewhere in the range of 0.8V-2.0V, so we should be good.
- All three input pins would have to be monitored for a change of state.
- As with any mechanical switch, debouncing is required, either in hardware or software, but the situation is made complicated by the nature of a rotary switch. Whether the contacts are break-before-make or make-before-break, the short-term outputs generated by rotating the switch through one position or many are "interesting". Gray code was invented to avoid some of this but it's not a panacea. Probably just a long time-out is the easiest way out.
Alternatives one could think of for making one-of-many selections (like, say, a specific radio station) include using an entry keypad. O, the tangle webs we weave!
Regards,
Kent
My apology for this lengthy word description but I discovered after a few minutes that my favorite diagramming tools are great for circuits and lousy for diode matrices.
Let's consider the 8-position switch I mentioned. It will be obvious from the following how to use switches with fewer or more positions.
Eight states can be represented by three bits: 000, 001, 010,..., 111, which we old minicomputer programmers think of as octal 0-7.
So take three GPIO input pins pulled down to ground internally (and maybe with external 10K pulldown resistors to ensure adequate diode current flow).
Wire the common pole of the single-pole 8-position rotary switch to +V (probably +3.3V for RPi GPIO).
Position 1 is left unwired. (state 000)
Position 2 is connected via 1 diode to input pin 1 (state 001).
Position 3 is connected via 1 diode to input pin 2 (state 010)
Position 4 is connected via 2 diodes: one to input pin 1, one to input pin 2 (state 011).
Position 5 is connected via 1 diode to input pin 3 (state 100).
Position 6 is connected via 2 diodes: one to input pin 1, one to input pin 3 (state 101).
Position 7 is connected via 2 diodes: one to input pin 2, one to input pin 3 (state 110).
Position 8 is connected via 3 diodes: one to input pin 1, one to input pin 2, one to input pin 3 (state 111).
In all cases, the diode's anode is wired to the switch position and the cathode to the input pin. (If one decides to reverse the sense of the circuit so that pins are normally pulled up to +V and the switch/diode selections pull them down to ground, then the diode directions must be reversed.)
That's 12 diodes (and 3 resistors). Easy peasy for a hacker's junkbox like the one in my basement.
Usually this is represented in a rectangular diode matrix aka diode crossbar form (look up "diode matrix").
It can be extended to more states but the diode count keeps mounting. More likely one would use 7400-series OR gates, a bespoke 7400-series IC encoder, or even a microcontroller chip (there's a ton of nice little chips-on-boards out there, all of them cheap and easy to program).
Things to think about:
- Garden variety diodes have an internal voltage drop of about 0.6V, so a GPIO input pin would see about 2.7V in the high state if we wire to +3.3V. As I understand the RPi docs, the voltage threshold for a GPIO pin is guaranteed to be somewhere in the range of 0.8V-2.0V, so we should be good.
- All three input pins would have to be monitored for a change of state.
- As with any mechanical switch, debouncing is required, either in hardware or software, but the situation is made complicated by the nature of a rotary switch. Whether the contacts are break-before-make or make-before-break, the short-term outputs generated by rotating the switch through one position or many are "interesting". Gray code was invented to avoid some of this but it's not a panacea. Probably just a long time-out is the easiest way out.
Alternatives one could think of for making one-of-many selections (like, say, a specific radio station) include using an entry keypad. O, the tangle webs we weave!
Regards,
Kent