Author Topic: Control 4 switch with 2 pins  (Read 1080 times)

0 Members and 1 Guest are viewing this topic.

Offline YarooooTopic starter

  • Regular Contributor
  • *
  • Posts: 71
  • Country: it
Control 4 switch with 2 pins
« on: September 26, 2019, 09:33:13 am »
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 Closed
01 - Switch 1 & 2 Closed
10 - Switch 1 & 2 & 3 Closed
11 - Switch 1 & 2 & 3 & 4 Closed

Is there a small package device that allow me this?

Thanks for any support!
 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2417
  • Country: us
Re: Control 4 switch with 2 pins
« Reply #1 on: September 26, 2019, 09:55:01 am »
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 Closed
01 - Switch 1 & 2 Closed
10 - Switch 1 & 2 & 3 Closed
11 - Switch 1 & 2 & 3 & 4 Closed

Is there a small package device that allow me this?

Thanks for any support!

That 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.
 

Offline German_EE

  • Super Contributor
  • ***
  • Posts: 2399
  • Country: de
Re: Control 4 switch with 2 pins
« Reply #2 on: September 26, 2019, 05:46:13 pm »
A   B

0   0      Switch 1 wired permanently closed


0   1      OR gate connected to A and B with
             output to switch 2, switches 1 and 2 now closed

1   0      Switch 1 on, Switch 2 on through OR gate
             Switch 3 connected to A. Switches 1, 2 and 3 now closed

1   1      AND gate connected to A and B with
            output to switch 4. Switch 1 on, switch
            2 on through OR gate, switch 3 on
            through connection to A, switch 4 on
            through 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
« Last Edit: September 26, 2019, 06:00:18 pm by German_EE »
Should you find yourself in a chronically leaking boat, energy devoted to changing vessels is likely to be more productive than energy devoted to patching leaks.

Warren Buffett
 

Offline Caliaxy

  • Frequent Contributor
  • **
  • Posts: 301
  • Country: us
Re: Control 4 switch with 2 pins
« Reply #3 on: September 27, 2019, 03:17:55 am »
Hello,

00 - Switch 1 Closed
01 - Switch 1 & 2 Closed
10 - Switch 1 & 2 & 3 Closed
11 - Switch 1 & 2 & 3 & 4 Closed

Is there a small package device that allow me this?


If 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   f
1   0      1   1   1   1
0   0      1   1   1   0
0   1      1   1   0   0
1   1      1   0   0   0

There might be other possible combinations too.
 

Offline German_EE

  • Super Contributor
  • ***
  • Posts: 2399
  • Country: de
Re: Control 4 switch with 2 pins
« Reply #4 on: September 27, 2019, 07:18:12 am »
Damn, that's a really neat solution, you win  :clap:
Should you find yourself in a chronically leaking boat, energy devoted to changing vessels is likely to be more productive than energy devoted to patching leaks.

Warren Buffett
 

Offline YarooooTopic starter

  • Regular Contributor
  • *
  • Posts: 71
  • Country: it
Re: Control 4 switch with 2 pins
« Reply #5 on: September 27, 2019, 09:34:37 am »
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.
 

Offline DetlefA

  • Newbie
  • Posts: 1
  • Country: de
Re: Control 4 switch with 2 pins
« Reply #6 on: September 27, 2019, 11:26:58 am »
It is possible with a quad NOR gate CD4001 or similar.

AB     S1 S2 S3 S4
   
00     1    0   0   0   
01     1    1   0   0
10     1    1   1   0
11     1    1   1   1

S1  = 1                     Hardwire to 1
S2  = (A OR B)          Two NOR Gates needed
S3  = A                    Hardwire to A
S4  = (A AND B) = NOT((NOT A) OR (NOT B))  3 NOR Gates needed

Cheers
Detlef
« Last Edit: September 27, 2019, 01:56:07 pm by DetlefA »
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10229
  • Country: nz
Re: Control 4 switch with 2 pins
« Reply #7 on: September 27, 2019, 11:43:22 am »
You can detect all 4 switches from just one MCU pin if that pin happens to have an ADC input on it :D
You 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 pressed
If the input reads low when the pull up is enabled then the other switch is pressed.
« Last Edit: September 27, 2019, 11:53:07 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Someone

  • Super Contributor
  • ***
  • Posts: 4981
  • Country: au
    • send complaints here
Re: Control 4 switch with 2 pins
« Reply #8 on: September 27, 2019, 10:28:52 pm »
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 pressed
If the input reads low when the pull up is enabled then the other switch is pressed.
If 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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf