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')
Entryton
  • 106
  • 1
  • 8
  • 1
    If you attempting to control a relay from the Raspberry Pi GPIO, you should follow this guide – RubberStamp Nov 22 '17 at 03: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 https://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 '17 at 05: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 '17 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 '17 at 18:10
  • If I take the magnet away from the switch, my Pi's LEDs dim. – Entryton Nov 22 '17 at 18:19
  • 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: https://raspberrypi.stackexchange.com/q/5206/19949 – Ghanima Nov 22 '17 at 18:41
  • @Ghanima, yes the onboard LEDs.... opps.... well nothing is broken so far. This is terrible. – Entryton Nov 22 '17 at 18:58
  • If you wired it as I suggested PUD_DOWN should be PUD_UP. – joan Nov 22 '17 at 19:06
  • GPIO 23 to NO and Ground to COM, right? I'm not getting any status change. – Entryton Nov 22 '17 at 19:18
  • GPIO23 (pin 16) or GPIO22 (pin 15)? See http://abyz.me.uk/rpi/pigpio/#Type_3 – joan Nov 22 '17 at 19:27
  • I meant GPIO22 (pin 15) ! – Entryton Nov 22 '17 at 19:28
  • Ok, I'm trying this on a Pi3 just in case my Pi2 got damaged from my lack of knowledge. The Pi3, using GPIO 21 (physical 40) and a GND produce the same results. Does the switch need power? You said data was enough though because data gives 3.3v right? – Entryton Nov 22 '17 at 19:53
  • The switch should not need a power supply. Can you wire it without the breadboard? The breadboard just adds one more thing to go wrong. – joan Nov 22 '17 at 20:25
  • I just did that and still nothing. I'm going to conclude that the switch is bad. I will order another one. :( - I can hear the switch switching though. It's faint, but I can hear it. – Entryton Nov 22 '17 at 20:30
  • I'm going to the store for a new switch. – Entryton Nov 22 '17 at 20:45
  • As written this code will do NOTHING! You read the value once, on startup, then have an infinite loop. All this will do is use CPU resources. – Milliways Nov 22 '17 at 22:55
  • I caught that + I had a bad switch. So I had 2 issues fighting me, one was my fault. Thanks for your help. – Entryton Nov 22 '17 at 23:03

2 Answers2

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.

  • 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 '17 at 17:52
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.

joan
  • 71,024
  • 5
  • 73
  • 106
  • 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 '17 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 '17 at 18:49