I'm planing on putting up a magnet and a reed switch on my window at home and I'm planing on using a RaspberryPi to sense when it's open/closed. I'm going to pull the wire up in the attic so the distance can vary a bit from 3 meters to maybe 20 meters.

My question is if I'm going to run into trouble having so long cables? I found a 0.50mm²/20AWG cable that I was planing on using.

  • Probably best to ask on a dedicated electronics site. I would guess radio EMF would cause false signals at long line lengths.
    – joan
    Feb 4 '19 at 19:02
  • Shielded cables would help with that, the co-ax from your old analogue TV antenna for example. The other problem you might have is in getting enough current to drive the 3.3v from the Pi out to your switch and back. Might need some "active" components to buffer the signal or change your setup to some form of current sensing. Feb 4 '19 at 20:05
  • 2
    Related (if not dupe) raspberrypi.stackexchange.com/q/57951/19949 Key take-away message: The most important is to use a low impedance circuit - i.e. a low value Pull up (say 470Ω) near the Pi itself.
    – Ghanima
    Feb 4 '19 at 20:31

You won't run into problems if you do it correctly :) Let's go over a few basics:

  1. You're talking about switching dry contacts in a reed switch, so you should size your pullup (or pulldown) resistor to keep the current in a range of 1 to 5 milliamps.

  2. Long-ish cables can act as an antenna to pick up "EMI", so you'll need a simple "filter" to squelch spurious signals. We'll cover that filter in a moment.

  3. Using a twisted, shielded wire pair (instead of two discrete wires) can help reduce noise pickup. If you don't have a STP wire, simply "twist" the pair of wires you do have. A twist every inch or two will be fine.

  4. Situations vary! You will have some spots in your house that have more interference than others. Consider this when you're planning your signal wire routing, and try to avoid adjacency to large current users (ovens, heat pumps, etc).

The "filter": I'll assume you don't have a lot of instrumentation, and so we'll approach this as a trial-and-error effort. Keep things simple... it's silly to worry about problems you don't have - you'll never run out of problems to solve, so don't waste time solving those that don't exist. That said, the filter you should start with is very simple: a 0.1 uF capacitor. Place it near your GPIO pin: one end to the GPIO pin, the other to Ground.

If you find that you are detecting "false triggers" from time to time, perhaps the easiest thing to try first is a "software timer". In other words, when your GPIO pin is pulled to its "active" state, start a counter in software; count to perhaps 500 msec, then check the GPIO pin again. If it's in the "triggered" state (active), then you have a "real event". If it's not, it was a spurious signal.

Try this, and let us know if you have any other questions, or run into difficulties. Also know that our SE site has a "schematic tool" that may be useful if you need or want more details on pin connections, component values, circuit diagrams, etc.

  • Thank you very much for this detailed answer. It helps a lot. I'm just in the beginning of this project and don't really know when I will have time to get this done but I might be able to get a test setup going soon in the basement with long cables to see how it works. About the filter you mentioned. Do I essentially "short" the GPIO to Ground with a capacitor?
    – PatricF
    Feb 5 '19 at 7:37
  • Yes. Use a small ceramic capacitor. One lead goes to the GPIO pin, the other to Ground. The capacitor is not actually a "short" - except to high frequency signals; i.e. it looks "open" to DC, and "short" only to HF AC. Hope that makes sense... if not, try this
    – Seamus
    Feb 5 '19 at 15:16

If you're referring to the cables that you stick on the GPIO pins, they work on 1-5 V typically,on arduino they have higher voltages, so the voltage will fall slowly with distance. i wouldn't suggest using longer cables. You can however move the RPi to the attic or wherever you need it and connect it wirelessly to your pc via SSH, or you can even extend the monitor cable from the pi to your room depending on how you want it, there are many ways around this problem, but using 20m cables with 5V, I'm afraid simply won't work.

  • 1
    Voltage drop at 20AWG is not an issue given the low current needed (it's 10 mV at 20 m of 20AWG and a current of 10 mA -> roughly the current you get if you use a rather low value Pull Up/Down resistor like the other answer I linked at the question suggests).
    – Ghanima
    Feb 4 '19 at 20:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.