Bus to read multiple read switches?? - Page 1
Forum section: electronics / projects

author: Nikos A.
author_rank: Regular Contributor
timestamp: November 06, 2020, 10:59:04 am
content: Hi everyone,I have 8 x reed switches and I want to read if they are Open or Close using an MCU. My problem is that I want to avoid cable mess and I try to minimize them as much as possible. So, is there any serial bus I can use for this purpose? I have some initial thoughts regarding the I2C.. I was condering to use aPCA95https://www.ti.com/lit/ds/symlink/pca9536.pdf?ts=160465932810636for each of the reed switches and then before the MCU aTCA9548Ahttps://www.ti.com/lit/ds/symlink/tca9548a.pdf?ts=1604646722204&ref_url=https%253A%252F%252Fwww.google.com%252FWith this approach only two cables are required and I will connect all the reed switches in daisy chain configuration. From the other hand, it's a bit complicated in terms of firmaware and hardware.So, I am asking your advice and ideas regarding this problem.Thanks in advanceNick

author: Psi
author_rank: Super Contributor
timestamp: November 06, 2020, 12:01:46 pm
content: You only need 1 chip, an IO Expander chip with 8 inputs so you can wire each input to a switch with a pullup resistor (or use an io expander with pullups built in)Then you need to run 4 wires from the IO expander back to the MCU (power, ground, SCL, SCA).That is the correct way to do it.Alternatively, this is a bit more hacky, but if the MCU you're using has ADC inputs you could use the switches to adjust an analog voltage on a single wire and have the MCU read that and decode it.It would have 2 wires in total (GND and the voltage signal) but you would need an ADC that is high enough resolution so that you can accurately decode all the switch state combinations from the ADC readings, remembering that more than one switch can be set at once.A 12+bit ADC should be able to decide 8 switches (256 possible levels), but it would be safer to use two analog lines so each only has to handle 4 switches. Since 16 possible levels is much easier to handle than 256 and you can do 16 levels on any cheap 8bit ADC.The way you wire the switches up is using a resistor voltage divider where the resistors are multiples of 2. Each switch shorts out one of the resistors.This way the overall resistor string will then change value and be unique to that switch combination giving you a voltage level for each possible switch stateYou would need to code it so a voltage level is only valid if it has been that way for 50ms or so. Basically just some software switch state debounceJust to protect against the ADC reading right on a switch change when the voltage might not be stable.

author: Nikos A.
author_rank: Regular Contributor
timestamp: November 06, 2020, 01:04:50 pm
content: Thanks for your reply PsiQuote from: Psi on November 06, 2020, 12:01:46 pmYes this is an approach but I need daisy chain configurationQuote from: Psi on November 06, 2020, 12:01:46 pmThat's sounds like a promising solution for my application. If I have understand corrently you mean something like this:Quote from: Psi on November 06, 2020, 12:01:46 pmCould you provide more information on this?

author: Twoflower
author_rank: Frequent Contributor
timestamp: November 06, 2020, 01:57:20 pm
content: How about using a 1-Wire device like theDS28EA00. It is a thermometer device with two GPIO pins. The trick is to a) find a multi-slave 1-Wire library (sometimes it is assumed only one device attached). And b) your software needs to 'learn' the position of the devices to correlate the 1-Wire serial number to the position in the chain.Not as cheap as a bunch of resistors. But probably a nice alternative with a bonus of providing the local temperatures ;-).

author: Renate
author_rank: Super Contributor
timestamp: November 07, 2020, 12:52:42 am
content: Oh, no. I wouldn't do that ADC thing unless you are only doing 16 levels on an 8 bit ADC.Even then, I wouldn't do it.So, these 8 switches are all in different places?You didn't really give the dimensions of this problem.It makes a difference how they are grouped and how far apart they are.Just run the raw contacts back on zip cable back to your central point.Put some conditioning/protection on them and just run them straight into the µC if you have 8 pins.If not use a shift register on SPI or some I²C expander.

author: Niklas
author_rank: Frequent Contributor
timestamp: November 07, 2020, 06:39:37 am
content: The easiest way is to connect them together with resistors and to measure the analog voltage as mentioned earlier. This is how it is usually done on tank level sensors with a magnetic float for boats, trucks, buses etc. With careful selection of resistor values you can extend this solution to more than 60 reed switches on a single signal and still have head room for open/short detection.A hint to the selection: How can you get equal voltage steps on the ADC for each step? R1 != R2...

author: rfmerrill
author_rank: Regular Contributor
timestamp: November 07, 2020, 06:46:30 am
content: So you have a bunch of these switches in a chain going far from the microcontroller, and you need to handle all combinations of switches open/closed?

author: Niklas
author_rank: Frequent Contributor
timestamp: November 07, 2020, 07:29:34 am
content: rfmerrill is right, I forgot that option. It would still be possible with the resistors, but you have to rearrange them a bit so that each resistor+reed are in parallel from the point of measuring.The German company Multiplex used this on a keypad for remote control (Profi 3030 or 4000). The resistors were selected as R, 2R, 4R... if I remember correctly. Then you could get all combinations of the switches, but the practical maximum of switches would be around 8 or 16.

author: Unixon
author_rank: Frequent Contributor
timestamp: November 07, 2020, 07:37:50 am
content: Are the switches physically separated?If they are, you can make a distributed shift register with individual D-ffs.This would take 2 D-ff per switch, 1 for holding result, 1 for shifting data out to MCU.Look at 74HC165 internal schematic for example.

author: Psi
author_rank: Super Contributor
timestamp: November 08, 2020, 07:03:15 am
content: Quote from: Nikos A. on November 06, 2020, 01:04:50 pmSo all 8 of these switches are a long distance apart? and you want to avoid needing to run 8+GND wires to the first switch and then 7+GND wires from their to the next switch etc..If so, then yes,Having one IO Expander at each switch location will work. You will have a 4 wire bus to all switches (PWR/GND/SCL/SDA)However it will take quite a bit more effort to setup that compared with just running 9 wires.Hm.. you could even drop it to 3 wires if you coded your own bi-directional 1-wire bus system using Manchester coding with a cheap MCU at each node. But this is crazy and A LOT more effort compared to a 4 wire system with IO expanders.Hm.. Unless you can find some 1-wire bus addressable io expanders, maybe someone makes something like that. I dunno

author: BrianHG
author_rank: Super Contributor
timestamp: November 08, 2020, 07:39:11 am
content: You can use a 74LS/74HC589 for every 8 relays, only 2-3 wires to connect between them and your MCU as they can be chained to create 1 massive parallel to serial stream converter.Using a 6cent Padauk MCU for every 12-13 relays, you can program a 1-2 wire interface can read each relay. (You can use a ~1$ Microchip PIC as well)For the long space solution between relays with a 2 wire interface, use a 3cent 6pin SOT-23-6 Padauk MCU or equiv ~40cent Microchip MCU at each relay and have a 1 serial wire between each where your control master MCU sends 1 wire to start clock while each MCU shifts and adds data to a serial stream while the last MCU sends the return signal with all the relay states on 1 wire back to your master control MCU.

author: Psi
author_rank: Super Contributor
timestamp: November 08, 2020, 09:59:03 am
content: You can modulate the data on top of the power wire and get it down to 2 wires (Power and Gnd)but we are getting carried away

author: BrianHG
author_rank: Super Contributor
timestamp: November 08, 2020, 10:25:45 am
content: Quote from: Psi on November 08, 2020, 09:59:03 amHow about an old coil microphone listening and keeping track of the reed relay sound and magnetic surge as the switch. Completely wireless, though may be prone to error.

author: Twoflower
author_rank: Frequent Contributor
timestamp: November 08, 2020, 10:33:14 am
content: OK, if you want to go over the top: How about TDR. Feed in a pulse and time when you see the reflection. Adding a resistor in series to the contact (all the same value might be OK) to identify which are closed in case more than one contact is closed. You 'just' need some processing time to compute the reflected waveform.Still I would use the 1-Wire I mentioned earlier ;-). Or check for a CAN (or similar bus) device.

author: Psi
author_rank: Super Contributor
timestamp: November 08, 2020, 10:50:59 am
content: ESP8266 per switch with mesh networkThe funny thing is, cost wise, it's not that much different to IO expandersSince ESP8266 are like $1

author: rfmerrill
author_rank: Regular Contributor
timestamp: November 24, 2020, 06:57:38 pm
content: How quickly do you need to sense changes to the reed switches? Like is it ok if you only poll the switches every 100ms or does it have to be faster than that?

author: gcewing
author_rank: Regular Contributor
timestamp: November 25, 2020, 07:27:55 am
content: Clock N times to clear the flip flops.Pulse READ low to set flip flops whose switches are closed.Clock N times to read data. This also resets the flip flops ready for the next cycle.

author: bingo600
author_rank: Super Contributor
timestamp: November 25, 2020, 03:41:26 pm
content: A 74HC165 seems to be popular8 Parallel in serial out (SPI)