11

Working my way through a garage door project. I purchased a few of these but I'll be danged if I can find a consistent answer on how they're wired to a Rpi.

My thought was just start hooking it up and giving it a shot, I'm worried about blowing up my pi though.

There are some sites that say use a pull up, some say use multiple resistors. Some have you connect through ground (which make the least sense to me at all) that reed switch isn't powered, I have no idea how Adafruit's example works.

I assumed I needed power to the the common, then off of the NO/NC post I need to capture the state on a GPIO pin, but I don't really understand where to put the ground? Does it get split off like the multiple resistor link? Does putting the switch to ground engergize the whole circuit that way?

I tried to read some of the posts on here but they're not really getting me to the why. Can someone help me demystify this a bit? If you've got any resources to build some fundamentals I would surely appreciate it. I'm a programmer by trade so I'm out of my element.

EDIT:

I had some confusion on the NO/NC. I think that has cleared up a bit, I was looking at it from the standpoint of my door and not the contact of the switch. The SW should be normally open.

I have three diagrams here and am seeking clarification How do you configure pull up vs pull down with the resistor. Do I need an additional resistor to do the 'pulling' and ensure the safety of my brave #18 GPIO soldier? Does moving the resistor change the properties/functionality of the circuit?

Circuit Diagram

Do I need a smaller resistor? I had a 4.7 on the board already so I used it. Is there a way to calculate an appropriate one? I am not getting a pin change.

So, I understand this is a breach of SO protocol, asking a question on a question but I would rather understand what's going on than just smash one of your answers in and call it done.

EDIT 2:

When I got a working solution, I tried to scale it up but needed a resistor on each line and switched it to the schematic shown. This works and has not exploded my pi nor has it set on fire.

I'm using a software pulldown and debounce.

What would moving the switch power to the ground side of the resistor do? Nothing?

wireDiagram

1
  • 1
    Your switches have normally closed and normally open contact. This will make your project easier. Wire the 4.7K resistor to the pin and 3.3V rail. Take a wire from ground and the GPIO pin and wire it to the normally closed (NC) and common (COM) contacts of the switch and "bingo" GPIO Pin 18 will be low until the door is opened. The switch opens and the 4.7k pull up resistor "pulls up" the GPIO pin to 3.3V to provide the input you are looking for. This is a much safer option to protect the pi from wiring faults.
    – Bob Fk
    Commented Sep 6, 2015 at 2:03

6 Answers 6

7

It's a switch. Connect it like you would any other switch. The only thing to decide is if you want to use it as normally open (normally reads as off) or normally closed (normally reads as on).

There are two basic ways - connect common to ground or connect common to 3V3. Whichever you choose it is safer to make that connection through a resistor to prevent the risk of a short circuit. Something between 1-5 thousand ohms should be suitable.

Connect the other chosen contact to a GPIO set as an input.

Connecting common to ground and normally closed to a GPIO

Set the GPIO to be an input with the internal pull-up to 3V3 set.

The GPIO will normally read 0 (when the switch is off). It will read 1 when the switch is on.

Connecting common to ground and normally open to a GPIO

Set the GPIO to be an input with the internal pull-up to 3V3 set.

The GPIO will normally read 1 (when the switch is off). It will read 0 when the switch is on.

Connecting common to 3V3 and normally closed to a GPIO

Set the GPIO to be an input with the internal pull-down to ground set.

The GPIO will normally read 1 (when the switch is off). It will read 0 when the switch is on.

Connecting common to 3V3 and normally open to a GPIO

Set the GPIO to be an input with the internal pull-down to ground set.

The GPIO will normally read 0 (when the switch is off). It will read 1 when the switch is on.

In all the above the weak internal resistor (circa 50k) will be overriden by the connection to ground or 3V3 through the more powerful (less resistance) external resistor.

2
  • Also, Reed switches have a very high bounce rate. So some debounce would be very useful in software.
    – Piotr Kula
    Commented Sep 2, 2015 at 19:16
  • Why would you not connect the reed between a IO (configured as Output) to a IO (configured as Input)?
    – Al Grant
    Commented Oct 30, 2021 at 20:51
4

Exactly, Just use it as a switch. I assume when the door is open or closed the Pi wants to know about it via one of it's input pins. Is the pi looking for a low? Then wire the switch from the input pin to ground and take a pull-up resistor to the +3.3V line. When the switch is open i.e. no magnet present, the input will be logic high. When the magnet activates the switch the input pin will be logic low. The pull-up resistor is not critical, start with a 4k7Ohm (yellow,purple,red, X). Measure the pin with switch off, it should be close to 3.3V if not try a resistor with less Ohms. say 2k2. Don't go below 1k, that would draw 3.3mA. If you are looking for a high, use the normally closed contacts of the reed switch. Don't run 3.3V to an external switch. A short could kill your Pi regulator chip. Hope this helps and not cause more confusion, Regards Bob

1
  • Thanks for answering, I think I'm getting closer. I put some edits on my question if you want to take a look.
    – Bmo
    Commented Sep 4, 2015 at 17:03
1

Your diagram left and middle is no good at all GPIO is shorted to ground and can do nothing.

Right hand diagram is correct if your GPIO pin 18 program is looking for an input to go high to take the next step. However we don't normally connect a power rail, the 3.3V, directly to an input, we normally feed it through a resistor of 2.2k or so. Try the 4.7k and see if the pin goes high. I would wire the resistor from pin GPIO 18 to the switch and have the other side go to the 3.3V power line. I'm not happy to have the 3.3V line going to the garage door without some resistor to limit the current in case of a short circuit in your wiring. If the 4.7k resistor is enough to pull the pin high I would split it up in the final product with 2.2k either side of the switch at the Pi end to protect the Pi Input and the Pi power supply from accidental shorts in the external wiring to the garage door switch. The resistor in your right hand circuit would be called a pull down and they are not usually needed. Again check if with a voltmeter or logic probe if the input is normally low until pulled high by the switch and make sure it goes close to 3V. I'm your opposite, good with hardware, know nothing of programming, trying to learn a bit of Linux.

1
  • Thanks again. I have added a diagram of my current set up. Are you saying I should move the 3.3V supply on the ground side of the resistor? I should get more comfortable with my tester...
    – Bmo
    Commented Sep 16, 2015 at 18:34
0

Remove the 4.7K resistor to ground. It is not needed. It's just another small, unnecessary load on the power-supply. Your current circuit will work as is, BUT it would be better to have a resistor in series from the 3.3v power rail to each of the switches for protecting the power-supply in case one of the switches is accidentally shorted to ground. What size resistor? Try a 10k, if it works, that is you have about >3v on the GPIO pin, great, if not try a 4.7k or a 2.2k or even a 1k

0

Only your right hand circuit will work. The answers you got above are probably correct (attempting to describe a circuit in words is confusing, even to an electrical engineer).

I feel these give too many options. I recommend the following. http://elinux.org/RPi_GPIO_Interface_Circuits#Buttons_and_switches

For technical reasons related to interference and safety a pull up to Vcc with a switch to ground is preferable, but either will work. (You can use a Normally Open or Normally Closed switch).

If you want to be super safe, you could include a small resistor in series with the lead from the GPIO. This is only necessary if you connect a switch to a pin configured as an output. The Pi will not be damaged even if you do, as it has internal protection from overload, but may impact on the operation of other pins.

PS reed switches bounce like a kid on a trampoline. You will need some anti-bounce technique, either in hardware or software.

-1

I was able to get it to work by following this example that uses voltage from the GPIO pin and ground. I skipped the Streamer stuff as I wasn't interested in it.

Reed switch tutorial

1
  • 1
    Please put a small summary from the link in case it goes down.
    – Human
    Commented Nov 4, 2017 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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