atux_null
Posts: 109
Joined: Thu Jun 21, 2012 1:58 pm

connecting multiple magnetic switches

Sun Oct 01, 2017 5:13 pm

i have a site where we need to monitor the state of 2 doors and 4 windows. So whenever one is opened then an email will be send.
After a reasearch i have found the ]following link https://raspberrypi.stackexchange.com/q ... spberry-pi. The problem is that it is meant only for one magnetic switch and this switch is not named eg front door.
How can i add multiple switches and name them? So i could get an email when front door is open and state that this particular door is open.
Ideally a web page where the state of all the switches will be available and in case on door or window opens, then an email will be sent.

Any help will be appreciated.

User avatar
Burngate
Posts: 5362
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK
Contact: Website

Re: connecting multiple magnetic switches

Mon Oct 02, 2017 4:38 pm

There are two parts to your problem hardware and software.

Hardware:
For each switch, choose a GPIO.
Connect each switch between its GPIO pin and a ground pin.
If necessary, also connect a resistor of about 1k between each GPIO pin and a 3v3 pin.

Software:
Just repeat what you did for the single switch for all the others.

Job done.

atux_null
Posts: 109
Joined: Thu Jun 21, 2012 1:58 pm

Re: connecting multiple magnetic switches

Mon Oct 02, 2017 6:19 pm

hi. thanks a lot for the reply. This is in theory, but i have stuck in implementation. In which GPIO do i have to connect each reed? Also which software part gets the open/close part for each reed?

PiGraham
Posts: 3321
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: connecting multiple magnetic switches

Mon Oct 02, 2017 6:48 pm

How far apart are these doors and windows? Running long wires to 3.3v logic inputs could lead to false alarms due to electrical interference.

atux_null
Posts: 109
Joined: Thu Jun 21, 2012 1:58 pm

Re: connecting multiple magnetic switches

Tue Oct 03, 2017 5:18 am

each magnetic reed will be at max 20meters apart. Lets see the connection first and then we will find a way to boost the current.

B.Goode
Posts: 6194
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: connecting multiple magnetic switches

Tue Oct 03, 2017 7:04 am

A reed switch is simply a switch whose state (On/Off) is determined by an external magnet. The RPi will simply treat it as an open or closed circuit.

There is a basic tutorial from the Raspberry Pi Foundation here, which refers to 'button' instead. At step 5 there is a very simple example which reports when a button has been pressed.

https://projects.raspberrypi.org/en/pro ... -computing

In that example you could name the switch front instead of button.

The header on your RPi probably has 40 pins. Of those, 26 are associated with gpio connections: you can choose to use as many of those pins as you need for your project.

The Raspberry Pi resource already mentioned uses Python as it's Programming language.

There is an alternative already installed on recent releases of Raspbian. Node-RED is a graphical interface that can be used to produce a web page. There is another Raspberry Pi Foundation resource about it here: https://projects.raspberrypi.org/en/pro ... h-node-red

atux_null
Posts: 109
Joined: Thu Jun 21, 2012 1:58 pm

Re: connecting multiple magnetic switches

Tue Oct 03, 2017 10:16 am

Thanks for the replies. I am confused on which pins to connect each reed. eg pins 1 and 2 for reed 1, pins 4 and 5 for reed 2
Please provide an example an example.

B.Goode
Posts: 6194
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: connecting multiple magnetic switches

Tue Oct 03, 2017 10:33 am

Please have another look at Step 5 of https://projects.raspberrypi.org/en/pro ... -computing

That example does not use connections to the board in adjacent pairs of pins. There is no requirement for you to do so.

Each switch circuit must originate from your designated gpio pin and return to a Ground pin. All Ground pins are equivalent, and are electrically interconnected on the Raspberry Pi board. Ground pins can be shared if necessary or convenient.

pcmanbob
Posts: 4128
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: connecting multiple magnetic switches

Tue Oct 03, 2017 10:43 am

Hi atux_null

if you are going to be using switch on cable runs that long I would suggest you use 5v and an adjustable potential divider circuit for each of your switches, I did it this way on a similar project I did using a pi and had no trouble with voltage drop or false triggering.

So this is the circuit I used

Image

you need on of these for each switch you intend to use and you need to set it up before connecting it to your pi GPIO input, using a meter so that the input will be 3v when the switch is closed, in this way you can account for any voltage drop in your long cable runs.

Also but having the operation set this way round you will have a high input when the door is closed which will be the state you system will run in most of the time and it will make it resistant to any inducted random voltages which might result in a false high trigger. as you circuit is already in a high state, and when triggered it will go in to a low state with the low being given by the ground path through the 10K resistor with will be local to the gpio and so giving a good ground path.

I used this in a house with no problems for 2 years and one cable actually ran past a mains fuse board along side the main incoming cable and I had no problems with false triggering.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

atux_null
Posts: 109
Joined: Thu Jun 21, 2012 1:58 pm

Re: connecting multiple magnetic switches

Tue Oct 03, 2017 12:54 pm

thanks a lot pcmanbob. By your statement i realise that only two reed switches could get connected, since there are only 2 pins at 5V. Is that right?
On the other hand if there are not any power leakages then i could use the 3.3v without the 10K resistors. Am i correct?

pcmanbob
Posts: 4128
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: connecting multiple magnetic switches

Tue Oct 03, 2017 1:50 pm

atux_null wrote:
Tue Oct 03, 2017 12:54 pm
thanks a lot pcmanbob. By your statement i realise that only two reed switches could get connected, since there are only 2 pins at 5V. Is that right?
On the other hand if there are not any power leakages then i could use the 3.3v without the 10K resistors. Am i correct?
No

you can connect more than one thing to each 5v pin.

Seeing as you will need to built 6 of the above circuits one for each magnetic switch you will need to use a pcb of some kind in the final build even if its just strip board so you will only need to take one 5v and one ground connection from your pi to the pcb, then you can use the 2 connections for all your circuits.
Last edited by pcmanbob on Tue Feb 27, 2018 10:01 am, edited 1 time in total.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

raceableability
Posts: 3
Joined: Tue Feb 27, 2018 7:25 am

Re: connecting multiple magnetic switches

Tue Feb 27, 2018 7:32 am

can you explain the components that i should buy???
resistors?any voltage step down module?
Thanks
pcmanbob wrote:
Tue Oct 03, 2017 10:43 am
Hi atux_null

if you are going to be using switch on cable runs that long I would suggest you use 5v and an adjustable potential divider circuit for each of your switches, I did it this way on a similar project I did using a pi and had no trouble with voltage drop or false triggering.

So this is the circuit I used

Image

you need on of these for each switch you intend to use and you need to set it up before connecting it to your pi GPIO input, using a meter so that the input will be 3v when the switch is closed, in this way you can account for any voltage drop in your long cable runs.

Also but having the operation set this way round you will have a high input when the door is closed which will be the state you system will run in most of the time and it will make it resistant to any inducted random voltages which might result in a false high trigger. as you circuit is already in a high state, and when triggered it will go in to a low state with the low being given by the ground path through the 10K resistor with will be local to the gpio and so giving a good ground path.

I used this in a house with no problems for 2 years and one cable actually ran past a mains fuse board along side the main incoming cable and I had no problems with false triggering.

pcmanbob
Posts: 4128
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: connecting multiple magnetic switches

Tue Feb 27, 2018 10:05 am

raceableability wrote:
Tue Feb 27, 2018 7:32 am
can you explain the components that i should buy???
resistors?
The diagram shows you what you need to buy

magnetic door switch
10K multi turn potentiometer
10K 1/4 watt resistor

you will also need a digital multi meter to set the output voltage from the circuit before you connect it to the GPIO input.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

raceableability
Posts: 3
Joined: Tue Feb 27, 2018 7:25 am

Re: connecting multiple magnetic switches

Tue Mar 06, 2018 4:48 pm

Thanks a lot! I just bought the components for less than a euro!
I will try today your solution!
I need a potentiometer for every zone, not every magnetic contact right?

pcmanbob
Posts: 4128
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: connecting multiple magnetic switches

Tue Mar 06, 2018 5:23 pm

The circuit was for each individual switch, if you are talking about zones are you trying to use this to connect town existing alarm panel ? Because it was not designed for this.

If you just want to use several switches then you could connect them in series and use the circuit to monitor a number of switches , but you will not know which switch is open just that one of them is.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

raceableability
Posts: 3
Joined: Tue Feb 27, 2018 7:25 am

Re: connecting multiple magnetic switches

Wed Mar 07, 2018 6:30 am

Exactly! i am fine with that!thanks a lot for your precious help!

atux_null
Posts: 109
Joined: Thu Jun 21, 2012 1:58 pm

Re: connecting multiple magnetic switches

Sat Jul 14, 2018 2:04 pm

Hi. At the moment iam using it only with one switch to monitor one door and i getting the status on screen as in https://www.ryansouthgate.com/2015/08/1 ... or-sensor/
I would like to send the status of the door in a mysql.I have a second machine that runs a LAMP server and i would like to have the status in the mysql over here.

How could i achieve that please?

User avatar
Z80 Refugee
Posts: 358
Joined: Sun Feb 09, 2014 1:53 pm

Re: connecting multiple magnetic switches

Sun Jul 15, 2018 10:04 am

pcmanbob wrote:
Tue Oct 03, 2017 10:43 am
if you are going to be using switch on cable runs that long I would suggest you use 5v and an adjustable potential divider circuit for each of your switches, I did it this way on a similar project I did using a pi and had no trouble with voltage drop or false triggering.
This is crazy. There is absolutely no need to buy (or go to the trouble of adjusting) a potentiometer to implement a circuit like this, and I believe stems from inexperience and lack of knowledge of electronics principles. You have, presumably, assumed you don't know how much resistance there will be in a long length of wire, so want to be able to trim it out. There is no need! Just design a circuit that has plenty of latitude for wire length.

It is far more important to provide some interference suppression (on long wires), otherwise you will need to do it in software (by ignoring brief triggers).

Note also that if you wind the potentiometer too low in your circuit, you risk damaging the RPi permanently. If the potentiometer wiper happens to go open-circuit (as they do), it will read as if the switch has gone open circuit.

It would be better to have the reed switch connected to 0V. That's not a problem, you just have to read a '0' as "closed" and a '1' as "open" in the software.

Here is my recommendation, take it or leave it (see below): The three diodes from 5V and resistor to 0V provide an "RPi safe" excitation source for the reed switch circuits, in lieu of simply using the 3.3V output from the RPi (which is not protected from external "accidents"). With the 2k7 resistors in series with the reed, you get about 1mA through the reed contacts - it is important not to go too low with that. The 470nF capacitor provides interference suppression (and debounce), and the 330 ohm resistor to the GPIO prevents a dead short if the GPIO is accidentally programmed as an output.

If you are trying to wire this like a normal alarm system, each reed circuit in the diagram is actually several reed switches in series so that if any one of them opens the whole "zone" is detected as open circuit.

More advice here: Beginners Guide to Wiring Things to the GPIO

CF095AEE-F405-4DB5-B7B0-60EE1EB5582B.jpeg
CF095AEE-F405-4DB5-B7B0-60EE1EB5582B.jpeg (251.19 KiB) Viewed 595 times
Military and Automotive Electronics Design Engineer (retired)

For the best service: make your thread title properly descriptive, and put all relevant details in the first post (including links - don't make us search)!

pcmanbob
Posts: 4128
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: connecting multiple magnetic switches

Sun Jul 15, 2018 8:10 pm

Z80 Refugee wrote:
Sun Jul 15, 2018 10:04 am
pcmanbob wrote:
Tue Oct 03, 2017 10:43 am
if you are going to be using switch on cable runs that long I would suggest you use 5v and an adjustable potential divider circuit for each of your switches, I did it this way on a similar project I did using a pi and had no trouble with voltage drop or false triggering.
This is crazy. There is absolutely no need to buy (or go to the trouble of adjusting) a potentiometer to implement a circuit like this, and I believe stems from inexperience and lack of knowledge of electronics principles. You have, presumably, assumed you don't know how much resistance there will be in a long length of wire, so want to be able to trim it out. There is no need! Just design a circuit that has plenty of latitude for wire length.

It is far more important to provide some interference suppression (on long wires), otherwise you will need to do it in software (by ignoring brief triggers).

Note also that if you wind the potentiometer too low in your circuit, you risk damaging the RPi permanently. If the potentiometer wiper happens to go open-circuit (as they do), it will read as if the switch has gone open circuit.

It would be better to have the reed switch connected to 0V. That's not a problem, you just have to read a '0' as "closed" and a '1' as "open" in the software.

Here is my recommendation, take it or leave it (see below): The three diodes from 5V and resistor to 0V provide an "RPi safe" excitation source for the reed switch circuits, in lieu of simply using the 3.3V output from the RPi (which is not protected from external "accidents"). With the 2k7 resistors in series with the reed, you get about 1mA through the reed contacts - it is important not to go too low with that. The 470nF capacitor provides interference suppression (and debounce), and the 330 ohm resistor to the GPIO prevents a dead short if the GPIO is accidentally programmed as an output.

If you are trying to wire this like a normal alarm system, each reed circuit in the diagram is actually several reed switches in series so that if any one of them opens the whole "zone" is detected as open circuit.

More advice here: Beginners Guide to Wiring Things to the GPIO


CF095AEE-F405-4DB5-B7B0-60EE1EB5582B.jpeg
So I am suggesting you use 1 resistor and 1 pot for each circuit and I clearly stated you need to set up the voltage output form the potential divider circuit before connecting to the gpio.

your suggesting buying 3 diodes 3 resistor and 1 capacitor for each circuit .

then reason I suggested using a normal high circuit is because and passing induced voltages will have no affect because you input is already high, and only goes low on door activation. all bets are off of course should you have a local lightening strike as one posted on the forum discovered recently because it is likely to do damage to the pi anyway.

As the old say goes there's more than one way to skin a cat and there's more than one way to design a circuit, in the end it your choice., use what works for you.
Last edited by pcmanbob on Sun Jul 15, 2018 8:49 pm, edited 1 time in total.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

User avatar
bensimmo
Posts: 3014
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: connecting multiple magnetic switches

Sun Jul 15, 2018 8:36 pm

Another alternative once you have all the switches working.

Look at something like Cayenne, it needs an internet connection but they do everything for you.
You can setup alerts, see it from wherever you are etc.


Though use MQTT, it is the in thing for IOT to send you signals and information around.

User avatar
Z80 Refugee
Posts: 358
Joined: Sun Feb 09, 2014 1:53 pm

Re: connecting multiple magnetic switches

Sun Jul 15, 2018 10:25 pm

pcmanbob wrote:
Sun Jul 15, 2018 8:10 pm
So I am suggesting you use 1 resistor and 1 pot for each circuit and I clearly stated you need to set up the voltage output form the potential divider circuit before connecting to the gpio.
Do you really want to cloud the education I am trying to provide by arguing your case? Do you have 40+ years relevant experience?
pcmanbob wrote:
Sun Jul 15, 2018 8:10 pm
your suggesting buying 3 diodes 3 resistor and 1 capacitor for each circuit .
No I'm not, try to understand the design and count again. If you are criticising the component count, I ask you why you think you need potentiometers in the first place. A fixed resistor would achieve the desired effect, and have zero chance of blowing up the RPi by being set incorrectly (you would know, if you had enough experience, that if it can happen, sooner or later it will: for example, somebody picky up your circuit but lacks the knowledge to set it up before connecting and uses the RPi as the measuring stick).
pcmanbob wrote:
Sun Jul 15, 2018 8:10 pm
then reason I suggested using a normal high circuit is because and passing induced voltages will have no affect because you input is already high, and only goes low on door activation.
Wrong. Induced voltages go both ways. It's best to keep external circuits at the minimum potential, and as "0V" is usually roughly the same as earth, there is least chance of damage by accidental shorts or suffer galvanic corrosion etc etc etc.
pcmanbob wrote:
Sun Jul 15, 2018 8:10 pm
As the old say goes there's more than one way to skin a cat and there's more than one way to design a circuit, in the end it your choice., use what works for you.
That's the difference between cobbling something together that works without knowing how close it is to not working, and designing something fit for purpose and reliable. I don't agree with you. As far as I can see, you are just pushing "what worked for you" without any consideration for quality.

If you don't want to learn about electronics engineering that's fine by me. Everything I need to say has been said. If anyone wants more explanation that's fine, but if you want to pick arguments I'm not interested. Bye.
Military and Automotive Electronics Design Engineer (retired)

For the best service: make your thread title properly descriptive, and put all relevant details in the first post (including links - don't make us search)!

pcmanbob
Posts: 4128
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: connecting multiple magnetic switches

Mon Jul 16, 2018 9:34 am

Not getting it to an argument over this as I have said before we are not in an industrial or military environment here , this is a hobby environment were people will use what works and is often simple to build.

As for my experience work for BT for over 40 years as a power maintenance technician installing and maintaining control and alarm systems on all sorts of standby generators , air conditioning plants, refrigeration plants , AC-DC & DC-DC power conversion and just about any other building services system, on a range of voltages form 12v DC right up to 11000V AC.

most of these systems used much more complicated interfacing circuits than are ever suggested on this forum, but suggesting they be used here would just be over complicating most if not the replies.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

grahamed
Posts: 272
Joined: Mon Jan 30, 2012 7:01 pm

Re: connecting multiple magnetic switches

Mon Jul 16, 2018 11:16 am

Variable resistors to set voltage? Current through wiring and switch is less than 330uA. How much volts drop are you expecting?

drgeoff
Posts: 8367
Joined: Wed Jan 25, 2012 6:39 pm

Re: connecting multiple magnetic switches

Mon Jul 16, 2018 5:07 pm

Voltage drop is not the problem - no need to use more than the 3.3 volt rail. Possible pickup into longish wires, especially when the switch is open is the issue. Simplest way to deal with that is to add a series resistor and a decoupling capacitor. One end of that resistor to the junction of switch and pullup resistor. Other end of resistor to GPIO input. Capacitor between GPIO and ground.

Some additional tips:

1. Use twisted pair wiring if possible.

2. Avoid running the wire to the sensors in close proximity to parallel mains cable, especially those carrying electrically "noisy" loads such as motors, arc-welders, fluorescent lights etc.

grahamed
Posts: 272
Joined: Mon Jan 30, 2012 7:01 pm

Re: connecting multiple magnetic switches

Tue Jul 17, 2018 3:54 pm

Variable resistors are there to set voltage, i.e. to conteract volts-drop. hence my question.

Agree with all the rest.

3V3 is fine, could do 5V plus 3 diodes if the mood takes you.

I suggest resistors in series with inputs also good to protect inputs..

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: No registered users and 11 guests