# Connecting NPN proximity sensor to rpi3 using reed relay

I have an NPN LJ12A3-4-Z/BX proximity sensor that I want to use to detect water usage on a water meter that has a rotating iron disk.

To make sure I don't overpower the 3.3v GPIO inputs I want to use a reed relay SIP-1A05 to keep the 5v circuit separate. But connecting input pin 18 to the relay does not seem to work.

Do I also need to connect the ground? If so, where to? With a resistor to the relay also?

Thanks

Edit with updated circuit from accepted answer.

• Nothing in this circuit makes sense. The coil of the relay is connected to the base of an NPN? Also pin 17 is connected to pin 18 through the relay?? And if the NPN is active, the 5V supply is shorted! I hope you just drew the diagram wrong, because otherwise something is going to be / already is damaged.
– Bort
Aug 18, 2017 at 13:01
• That's why I dotted the line, as I want to verify first before connecting.
– Olaf
Aug 18, 2017 at 14:52
• You follow the false assumption an open input pin would be read out as "0". It's exactly the opposite, open pins are internally pulled up to 3.3V and thus, read as "1". You have to wire the contact of your reed relay between the input pin and GND to "outvote" that internal pullup as soon the contact is closed. Aug 18, 2017 at 14:54
• like I drew in the comment of the first answer? imgur.com/a/VHMs3
– Olaf
Aug 18, 2017 at 15:24

According to LJ12A3-4-Z/BX datasheet the sensor should be powered by 6-36V, so 5V may be insufficient ( brown wire = +V, blue wire = -V/GND ). Connect coil of the relay between +V and output of the sensor ( black wire ). Also connect a pulldown resistor ( a few kohm ) between pin 18 and 20.

• ok, thanks, let me draw a new picture to verify if I understand
– Olaf
Aug 18, 2017 at 14:53
• don't know how to insert an image as comment: imgur.com/a/VHMs3
– Olaf
Aug 18, 2017 at 15:12
• looks good to me, should work Aug 18, 2017 at 19:21
• btw, it seems that the gpio pins of a raspberry pi3 have internal pulldown resistors of 60kOhm that can be enabled in software. But by doing that the ground is not connected to the GND of the external 5v. Does that matter, should it also be connected? raspberrypi.org/forums/viewtopic.php?t=62191&p=462221
– Olaf
Aug 18, 2017 at 19:47
• Just tested the circuit and it is working fine! thanks. I guess a benefit of a wired resistor is protection against a programming error if the pull down is not activated in software. If I do want to add resistor R, would 10kOhm be enough, or should I make it 60kOhm also?
– Olaf
Aug 18, 2017 at 20:25

Your original and updated drawings do not represent the internals of the switch - and if they did it wouldn't work!

simulate this circuit – Schematic created using CircuitLab

The NPN sensors are designed specifically to interface with various other voltages. There is no need for the reed relay.

• Configure your GPIO with internal pull-up resistor.
• When the sensor detects the meter iron its Q1 will turn on giving a low resistance between the black and blue wires thus pulling the GPIO low. Effectively the NPN transistor does exactly what your reed contact does.

You didn't provide a link to the sensor datasheet (and you should have). The only one I could find was here.

When I measure with my multi meter across black and blue when the sensor detects metal it measures 0.6v. Without metal it measures 5.1v. When there is no load would that not give a problem on the gpio pin? That's the part I don't understand.

I did some research on the sensor and this guy has discovered the problem. There is, apparently a 10k pull-up resistor built into the switch. Your caution may have paid off! You may be able to measure this with your multimeter between brown and black if there are no protection diodes in series.

simulate this circuit

Figure 2. Modified circuit showing fix for undocumented internal 10k pull-up.

The lessons of the story are:

• Purchase components from reputable suppliers that provide proper datasheets with all the relevant data. No datasheet - no sale.
• Do your own checks (as you have done) to confirm operation prior to integration with the expensive part of the system.
• Don't believe every piece of free advice!

simulate this circuit

Figure 2. Reed-relay version. This requires internal pull-down on the micro-controller.

• But the proximity sensor works at 6v-36v (and in practice runs ok at 5v also). The GPIO input pins, however, can only handle 3.3v. In stead of using a voltage divider I opted to use a reed relay. I don't understand why you say it does not work? I can't just connect the black wire to the gpio input pin.
– Olaf
Aug 19, 2017 at 9:09
• You've got it to work OK. It's your understanding of how the NPN transistor works is completely wrong. The relay is not powered via the NPN base. Note the orientation of yours and mine. "I can't just connect the black wire to the gpio input pin." Yes you can and the reason is explained in my answer. Aug 19, 2017 at 9:41
• Ok, willing to learn here. When I measure with my multi meter across black and blue when the sensor detects metal it measures 0.6v. Without metal it measures 5.1v. When there is no load would that not give a problem on the gpio pin? That's the part I don't understand.
– Olaf
Aug 19, 2017 at 12:31
• See the update. I'm editing this on patchy WiFi on a bus and I can't see if the schematics uploaded correctly. I'll check it later. Aug 19, 2017 at 13:12
• Indeed I am able to measure the 10kOhm resistor between Brown and black. I don't have a diode lying around though, so I'm better off using the reed relay so the 3.3v of the pi itself powers the gpio input.
– Olaf
Aug 19, 2017 at 19:55