Go Down

Topic: reading multiple SPDT (On-Off-On) switches (Read 612 times) previous topic - next topic

RogMoe

For a new project I want to use 12 SPDT (On-Off-On) switches using a Mega2560. One option would be to use 24 pins...  :smiley-confuse:   There must be better ways! What's the best way to get this done?

UKHeliBob

Do your SPDT switches really turn on-off-on ?

If so then they are not SPDT switches which only have two positions
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Erik_Baas

#2
Sep 16, 2020, 09:29 pm Last Edit: Sep 17, 2020, 02:15 am by Erik_Baas
A 4 by 6 matrix (with 24 12 diodes!) will use only 10 pins.

Edit: changed "24" to "12"; thanks to Paul__B.

CrossRoads

The Mega only sees High or Low.
An SPDT is usually 3 pins, with Common in the middle.
Is your plan to connect the Common to Gnd, and the two sides to two Input pins on the Mega (with internal pullup resistors enabled) and have the inputs read as High unless the switch is thrown to one side or the other to take one of the two pins Low?
Otherwise, a common On/Off toggle switch per input would do, with Off being read as High and On being read as Low.

Alternately, you could use parallel input/serial output shift registers and read the High/Low states in from the shift registers. The only 3 pins are needed - Latch, Clock, and DataIn.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

RogMoe

The Mega only sees High or Low.
An SPDT is usually 3 pins, with Common in the middle.
Is your plan to connect the Common to Gnd, and the two sides to two Input pins on the Mega (with internal pullup resistors enabled) and have the inputs read as High unless the switch is thrown to one side or the other to take one of the two pins Low?
(...)

Alternately, you could use parallel input/serial output shift registers and read the High/Low states in from the shift registers. The only 3 pins are needed - Latch, Clock, and DataIn.
Yes, this is what I have in mind. Every switch generates three states. 12 switches times 2 pins is 24 pins.
I knew I would probably end up with some kind of shift register. Besides the 12 switches, I'd like to turn on 12 bi-color led's too...

Which shift register would you recommend?
Thx

UKHeliBob

Quote
Every switch generates three states.
Then it is not an SPDT switch
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

CrossRoads

Open your mind to possibilities UKHeliBob:

On-Off-On is considered an SPDT switch by the distrubutors.
https://www.digikey.com/product-detail/en/apem-inc/TL39P005000/679-4090-ND/1796422


If it was wired up 5V, 2.5V, 0V, that would be three states. Mega can't do much with the middle the on a single pin tho, so that's not too handy/


If the two NOs are wired to two input pins, and the common to Gnd, then the three states are: 
input1 high-input2 high,
 input1 low-input2 high, and
 input1 high-input2 low


The switch then does not support 
input1 low-input2-low.


I'd recommend this part
https://www.ti.com/lit/ds/symlink/cd74hc597.pdf?
Supports parallel load of data, serial output of data, and daisychaining of data thru multiple devices.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Erik_Baas

Or 3 pcs. PCF8574, an 8-bit I/O expander for I2C.

Paul__B

The switches are SP3T.

As per another discussion here, each switch requires only one diode on the common pin to be used in a matrix, there are however in effect 24 switch positions for 12 switches, so a 6 by 4 matrix will use ten pins.

If you have ten pins available on the processor you do not need a Mega 2560, and a PCF8575 port expander would require only two pins to connect to the I2C interface.

CrossRoads

PCF8575 over $2.15 each at Digikey, and SMD only. Need two, $4.30 I2C
PCF8574, $1.30ish,  to $1.40 SMD or thruholle, 8 bits. Need 3, $3.90 to $4.20 I2C
cd74hc597 $1.56 thruhole, $0.39 SMD - huh. 8 bits.  Can do SPI.transfer(), or shiftIn(). 3 or 4 pins needed.

Don't forget pullup resisters on all inputs so the switches can pull them low, and 0.1uF caps on al power pins.



Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

RogMoe

#10
Sep 17, 2020, 10:09 am Last Edit: Sep 17, 2020, 10:12 am by RogMoe
I'll get a few PCF8575's. That will get me 16 IO per IC. I'll read all remarks above.
Thanks everyone!

ps I'll be using this kind of switch, with a centre position.
Naming a switch (in English) turns out to be rocketscience...
https://www.allelectronics.com/mas_assets/cache/image/4/d/6/9/19817.Jpg

UKHeliBob

Quote
Naming a switch (in English) turns out to be rocketscience...
Naming a switch in English is not a problem. 

The problem is trying to use a short form such as SPDT because it leaves out most of the important information
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

RogMoe

Yep, for me -being Dutch- it's sometime not easy to find my way around with all these abbreviations being used.
Never the less, thx for your help!

RogMoe

One more question please about the PCF8575.
I'm looking at the datasheet and I'm confused how to hook up the switches. In the datasheet there is a pic with a few examples. The example shows a 100K pull-up on port 05 and a switch, but there is a second resistor just beside the switch. What's its function and what value should it be?

About the project: it will be a controller for my ventilation, cooling, heating etc. All switches have two led's (red and green). All switches will have three positions: on, off or auto (middle position). For example, a valve can be forced open, forced closed or controlled by the system (auto) in which case any led will be off. In total there will be 12 switches and 24 led's. That makes 48 pins, so three PCF8575's will do fine.




Paul__B

One more question please about the PCF8575.
I'm looking at the datasheet and I'm confused how to hook up the switches. In the datasheet there is a pic with a few examples. The example shows a 100K pull-up on port 05 and a switch, but there is a second resistor just beside the switch. What's its function and what value should it be?
I am feeling some Déjà vu here, that we have been here before.  :smiley-roll:

However surprising it might be, the switch and resistor to Vcc is obviously a mistake.  It is quite useless to connect switches from PCF8575 pins to Vcc, with or without series resistors.

If you want the IC to read switches, you connect them from pins to ground.  The PCF8575 provides relatively weak pull-ups and they are not linear.  Paragraph 9.2.1.1 explains why you might need to provide the external 100k pull-ups (including for LEDs) if you are concerned about current consumption as you might be for battery operation - I do not believe this is a concern to you so you can ignore them.

The LEDs do connect to Vcc as the PCF8575 can pull the outputs down but not up to any useful extent as far as LEDs are concerned - this is a frequent trap for newbies.

If it suits you that the LED will always be illuminated if the corresponding switch is closed, you can use the same pin for the switch and LED, needing only half as many pins.

Go Up