Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: RogMoe on Sep 16, 2020, 08:35 pm

Title: reading multiple SPDT (On-Off-On) switches
Post by: RogMoe on Sep 16, 2020, 08:35 pm
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?
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: UKHeliBob on Sep 16, 2020, 08:56 pm
Do your SPDT switches really turn on-off-on ?

If so then they are not SPDT switches which only have two positions
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: Erik_Baas on Sep 16, 2020, 09:29 pm
A 4 by 6 matrix (with 24 12 diodes!) will use only 10 pins.

Edit: changed "24" to "12"; thanks to Paul__B.
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: CrossRoads on Sep 16, 2020, 09:32 pm
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.
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: RogMoe on Sep 16, 2020, 10:15 pm
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
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: UKHeliBob on Sep 16, 2020, 10:53 pm
Quote
Every switch generates three states.
Then it is not an SPDT switch
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: CrossRoads on Sep 16, 2020, 11:08 pm
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? (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.
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: Erik_Baas on Sep 16, 2020, 11:36 pm
Or 3 pcs. PCF8574, an 8-bit I/O expander for I2C.
Title: Reading multiple SP3T (On-Off-On) switches
Post by: Paul__B on Sep 16, 2020, 11:58 pm
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.
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: CrossRoads on Sep 17, 2020, 01:31 am
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.



Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: RogMoe on Sep 17, 2020, 10:09 am
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
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: UKHeliBob on Sep 17, 2020, 11:09 am
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
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: RogMoe on Sep 17, 2020, 11:54 am
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!
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: RogMoe on Sep 24, 2020, 11:58 am
One more question please about the PCF8575.
I'm looking at the datasheet (https://www.ti.com/lit/ds/symlink/pcf8575.pdf) 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.

(https://forum.arduino.cc/index.php?action=dlattach;topic=705337.0;attach=382985)

(https://forum.arduino.cc/index.php?action=dlattach;topic=705337.0;attach=382987)
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: Paul__B on Sep 24, 2020, 01:25 pm
One more question please about the PCF8575.
I'm looking at the datasheet (https://www.ti.com/lit/ds/symlink/pcf8575.pdf) 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.
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: RogMoe on Sep 24, 2020, 04:05 pm
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
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: RogMoe on Sep 25, 2020, 12:01 pm
Me again ;-)

I found these really nice switches (https://nl.mouser.com/datasheet/2/295/M2100-1662197.pdf) 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)

(https://forum.arduino.cc/index.php?action=dlattach;topic=705337.0;attach=383115)

(https://forum.arduino.cc/index.php?action=dlattach;topic=705337.0;attach=383116)

(https://forum.arduino.cc/index.php?action=dlattach;topic=705337.0;attach=383118)
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: Paul__B on Sep 25, 2020, 05:10 pm
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.
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: PaulRB on Sep 25, 2020, 06:00 pm
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.
Title: Re: reading multiple SPDT (On-Off-On) switches
Post by: RogMoe on Sep 26, 2020, 12:00 am
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