EEVblog Electronics Community Forum
Electronics => Projects, Designs, and Technical Stuff => Topic started by: Nikos A. on November 06, 2020, 10:59:04 am
-
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 a
PCA95https://www.ti.com/lit/ds/symlink/pca9536.pdf?ts=1604659328106 (https://www.ti.com/lit/ds/symlink/pca9536.pdf?ts=1604659328106)36
for each of the reed switches and then before the MCU a
TCA9548A https://www.ti.com/lit/ds/symlink/tca9548a.pdf?ts=1604646722204&ref_url=https%253A%252F%252Fwww.google.com%252F (https://www.ti.com/lit/ds/symlink/tca9548a.pdf?ts=1604646722204&ref_url=https%253A%252F%252Fwww.google.com%252F)
With 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 advance
Nick
-
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 state
You 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 debounce
Just to protect against the ADC reading right on a switch change when the voltage might not be stable.
-
Thanks for your reply Psi
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.
Yes this is an approach but I need daisy chain configuration
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...
That's sounds like a promising solution for my application. If I have understand corrently you mean something like this:
(https://i.ibb.co/WkLkfNT/Screenshot-84.png) (https://ibb.co/WkLkfNT)
The way you wire the switches up is using a resistor voltage divider where the resistors are multiples of 2.
Could you provide more information on this?
-
How about using a 1-Wire device like the DS28EA00 (https://www.maximintegrated.com/en/products/interface/sensor-interface/DS28EA00.html). 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 ;-).
-
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.
-
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...
-
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?
-
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.
-
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.
-
Yes this is an approach but I need daisy chain configuration
So 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
-
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.
-
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 :)
-
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 :)
How 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.
-
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.
-
ESP8266 per switch with mesh network :-DD
The funny thing is, cost wise, it's not that much different to IO expanders :)
Since ESP8266 are like $1
-
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?
-
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.
-
A 74HC165 seems to be popular
8 Parallel in serial out (SPI)