1

Final edit: Got it working! Thank you all for your suggestions.

The first issue was that my magnet was broken. I got a multimeter and tested the switch open and closed, and the meter was detecting current regardless of the state.

I bought a new switch and it works as intended. (current stopped, current flowing)

The second issue was in my code. I was reading the GPIO status outside of the while Loop.

In the end, I learned some valuable information. Thank you all!


I'm using a 2.5A Canakit power supply to power the Pi.

Since I need the magnet switch, how can I use an external DC power supply to send power to the magnet?

If I use 3.3v or 5v, like the diagram suggests, my Pi's LEDs dim. This is because I have a Sharp IR sensor and a Pi camera connected already. Not enough juice I guess.

This is how I currently have the magnet switch hooked up: This is how I currently have the magnet hooked up:

Thank you!

EDIT: I'm using a magnet switch. Sorry for calling it a relay. enter image description here

EDIT 2: @Joan suggested using GPIO to NO and GND to COM. I did that but there were no status changes if the switch was on or off according to the Pi.

enter image description here

EDIT 3: Here's my code. Am I enabling the internal resistor?

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) 

GPIO.setup(22, GPIO.IN, GPIO.PUD_UP)

read = GPIO.input(22)

while True:
    if read == True:
        print ('Door is True')
    else:
        print ('Door is false')
17
  • 1
    If you attempting to control a relay from the Raspberry Pi GPIO, you should follow this guide Nov 22, 2017 at 3:47
  • 2
    I really don't get where you are going with this circuit? You say that you want to power the relay but you hook up a switch. Why the connection between 3V3 and the GPIO pin? Just don't! See here for a simple circuit on how to power a relay raspberrypi.stackexchange.com/a/28201/19949 if that does not address your issue, feel free to edit your question to reflect the differences.
    – Ghanima
    Nov 22, 2017 at 5:43
  • I'm sorry. This is indeed a switch, not a relay. Why does the switch need 3.3v? I don't know. That's what I found online. Switch needs 3.3v and GPIO fed into 1 COM port. Then GND to the NO port.
    – Entryton
    Nov 22, 2017 at 17:47
  • That appears to be a switch which will return 0 or 1 when read depending on whether is is currently open or closed. You seem to have it wired sort of correctly. You do not send power to the magnet, you put a magnet close to the switch to switch it on and off. Reading the GPIO will tell you if the switch is in the on or off position.
    – joan
    Nov 22, 2017 at 18:10
  • 1
    @Entryton, to clarify, since there are no LEDs in your circuit. When you say the LEDs dim, do you refer to the on-board LEDs of the Pi? If so, stop doing it, you're shorting something. The circuit as shown is, uhm well, not what you want. You should be going for something like this: raspberrypi.stackexchange.com/q/5206/19949
    – Ghanima
    Nov 22, 2017 at 18:41

2 Answers 2

1

I suspect you are using lower value resistors if the Pi LEDs dim.

I would wire a GPIO direct to NO and connect ground to COM.

Set the GPIO to be an INPUT and enable its internal pull-up to 3V3.

The GPIO will read back as 1 when the switch is open and 0 when the switch is closed. If you connect to NC rather than NO the read values will be reversed.

2
  • Sadly this didn't work. The Pi doesn't detect any changes if the switch is on or off. I updated post with how I wired it.
    – Entryton
    Nov 22, 2017 at 18:40
  • How did you enable the internal pull-up? It would be useful if you added the code you are using to your question.
    – joan
    Nov 22, 2017 at 18:49
3

You could use a power supply module, like the one linked here: https://www.amazon.com/JBtek-Breadboard-Supply-Arduino-Solderless/dp/B010UJFVTU (First result on a quick google search, you might want to look further)

If you're adventurous you could cut apart a cable (a USB works well if you're near a computer - just be careful to prevent short circuiting)

EDIT: Just to clarify, you would connect the 3.3v to the RPi GPIO pin, and the 5v/gnd to the power supply.

1
  • Thank you for answering. I appreciate the help. As I mentioned to @Ghanima, this is a switch not a relay. I'm not sure why the switch needs 3.3v. It needs data, I understand that part. But the 3.3v I don't. If I remove the 3.3v, the switch does not work.
    – Entryton
    Nov 22, 2017 at 17:52

Your Answer

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

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