Control 4 switch with 2 pins - Page 1
Forum section: electronics / projects

author: Yaroooo
author_rank: Regular Contributor
timestamp: September 26, 2019, 09:33:13 am
content: Hello,I've left 2 free pins on my MCU, unfortunately I can't change MCU pin package and I must control 4 switches.I don't need a MUX to choose witch switch to open, but something like:00 - Switch 1 Closed01 - Switch 1 & 2 Closed10 - Switch 1 & 2 & 3 Closed11 - Switch 1 & 2 & 3 & 4 ClosedIs there a small package device that allow me this?Thanks for any support!

author: Nusa
author_rank: Super Contributor
timestamp: September 26, 2019, 09:55:01 am
content: Quote from: Yaroooo on September 26, 2019, 09:33:13 amThat example simplifies to switch 1 hardwired, so only 3 switches left to deal with.But if you intended to control 4 switches plus an off, then you have the problem of 5 states for 4 combinations.I'd suggest using the two lines for data and clock and using a shift register to control your switches, plus the ability to use the switches in any combination you like.

author: German_EE
author_rank: Super Contributor
timestamp: September 26, 2019, 05:46:13 pm
content: A B0 0 Switch 1 wired permanently closed0 1 OR gate connected to A and B withoutput to switch 2, switches 1 and 2 now closed1 0 Switch 1 on, Switch 2 on through OR gateSwitch 3 connected to A. Switches 1, 2 and 3 now closed1 1 AND gate connected to A and B withoutput to switch 4. Switch 1 on, switch2 on through OR gate, switch 3 onthrough connection to A, switch 4 onthrough AND gate.So, you can do this with one OR gate and one AND gate, available in a single 74HC58 IC, isn't Boolean Logic fun!Edit: Added 74HC58 information

author: Caliaxy
author_rank: Frequent Contributor
timestamp: September 27, 2019, 03:17:55 am
content: Quote from: Yaroooo on September 26, 2019, 09:33:13 amIf you only need these four combinations (i.e. you never need all four switches off), a creative solution would be to use a BCD decoder (such as CD4543B,https://www.jameco.com/Jameco/Products/ProdDS/13696.pdf).Use segment outputs a, d, e and f to drive the switches and inputs C and A to control their state, as below (connect input D to GND and input B to Vdd):C A a d e f1 0 1 1 1 10 0 1 1 1 00 1 1 1 0 01 1 1 0 0 0There might be other possible combinations too.

author: German_EE
author_rank: Super Contributor
timestamp: September 27, 2019, 07:18:12 am
content: Damn, that's a really neat solution, you win

author: Yaroooo
author_rank: Regular Contributor
timestamp: September 27, 2019, 09:34:37 am
content: Thanks guys for your answer. I'll prefer a BCD as solution since I'm already used to this devices.In the case I'd like to expand my IOs, so I want to use my 2 pins as CLK and Data. Are there common ICs that I can use? Lower price I've found it's about 1e/cad, little expensive.

author: DetlefA
author_rank: Newbie
timestamp: September 27, 2019, 11:26:58 am
content: It is possible with a quad NOR gate CD4001 or similar.AB S1 S2 S3 S400 1 0 0 001 1 1 0 010 1 1 1 011 1 1 1 1S1 = 1 Hardwire to 1S2 = (A OR B) Two NOR Gates neededS3 = A Hardwire to AS4 = (A AND B) = NOT((NOT A) OR (NOT B)) 3 NOR Gates neededCheersDetlef

author: Psi
author_rank: Super Contributor
timestamp: September 27, 2019, 11:43:22 am
content: You can detect all 4 switches from just one MCU pin if that pin happens to have an ADC input on itYou just use some resistors so each switch adds to the voltage on the ADC pin in powers of 2.That way you have unique voltages that match up to all of the possible switch combinations.There's another method you can use to detect two switches on one digital IO, however it only works for push buttons because you can't detect if both are pressed at once.You have one switch pull the pin high and the other pull the pin low.Then you generate a slow speed square wave on the pin using the pull ups and pull downs.If the input reads high when the pull down is enabled the one switch is pressedIf the input reads low when the pull up is enabled then the other switch is pressed.

author: Someone
author_rank: Super Contributor
timestamp: September 27, 2019, 10:28:52 pm
content: Quote from: Psi on September 27, 2019, 11:43:22 amIf you have programmable pull strengths (with some level of accuracy) or add an external pullup/pulldown and a series resistor on the switches you can fully decode all states. Many possibilities.