reading multiple SPDT (On-Off-On) switches

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... :confused: There must be better ways! What's the best way to get this done?

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

If so then they are not SPDT switches which only have two positions

A 4 by 6 matrix (with 24 12 diodes!) will use only 10 pins.

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

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.

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?
(...)

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

Every switch generates three states.

Then it is not an SPDT switch

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.

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

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.

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.

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

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

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!

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.

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?

I am feeling some Déjà vu here, that we have been here before. :roll_eyes:

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.

How am I suppose to know that there is a mistake in the datasheet? Stupid me...

Now I know there is a mistake in the datasheet, things are obvious again.

I use the led's in a different manner. If the function is in automatic mode, the led is solid. If any function is in manual (on or off) mode, the led flashes. So, the switches controls the function and the software sets the led. Therefor separate pins. PCF8575 come cheap.

Thx

Me again :wink:

I found these really nice switches with built-in bi-color led's (red/green).

What's the best way to control the led's, in such manner that I have the option to turn them BOTH off using as few as possible pins?

If the controlpin is not high or low, what happens to the leds?
or should I change the pinmode to INPUT in the loop?

or should I go with the second option, using two control pins? (last picture)

haEfe.jpg

bicolor_two_lead.png

haEfe.jpg

bicolor_two_lead.png

Well, the first option might allow you to illuminate either or neither (when the pin is set to INPUT) LED, but it works as a voltage divider - the LED voltage is limited to half the supply voltage - 2.5 V if using a 5 V supply and this may not illuminate the green LED properly, not at all if you were using a 3.3 V supply. And of course, it wastes current.

R4 and R5 don't have to be equal, I guess. You could make R5 slightly lower to ensure the green led gets enough voltage?

But as Paul__B already mentioned, you cannot do that with pcf chips. They don't have a true HIGH output state, only a weak pullup.

Maybe a mcp23017 instead? That has a true HIGH output state, I think.

Hi,
yes, I moved to MCP23017 wich is basically the same IC. Have a few of those lying around somewhere. Easier to control too. I'll go with the second option anyway. Using two pins to control a bi-color led is okay I guess and makes coding a bit easier.

Thx