Best way to integrate several magnetic door switches

Would a board like this work? It looks like I can run 3.3v, gnd, and 4 GPIO pins to one side and then connect 4 binary sensors to the other. Is that correct?

Correct, that board would work.

Here’s a much cheaper version:

I gave that board a try, but couldn’t get it to work. I tried with and without a 4.7k pullup resistor.
Yellow = 3.3v
White - GND
Grey = GPIO


I’ll buy the board you linked to and see if that one will work.

1 Like

Does the LED on the board for the appropriate input on the input side of the opto light when your reed switch is conducting? And what about the output side?

The output should be high when the input is high.

You don’t need a pullup resistor in this case as the output of the opto can be connected directly to the GPIO pin.

I’m not sure what the circuit is on your terminal block but you can eliminate it for testing and connect the opto board directly to the ESP32.

No, the LED on the input or output side of the board does not light. The only LED that lights on the opto board is the green power light.

That terminal block is just a pass-through. The white wire was going to GND and the grey to the GPIO 23 pin.

Hmm, I just looked at my diagram and I think I may have had the 3.3v pin incorrect. I’ll try moving that and see what happens. It might be a couple days before I have time.

I moved the 3.3v to the correct pin on the ESP board (I had it incorrect the first time) but still no luck. If I jump the GND and output 1 terminal on the opto-board I get a red light on that side, but I can’t get a light on either side if I create continuity between the input 1 + & - terminals.

Brown = 3.3V
Orange = GND
Yellow = GPIO 23


I also tried the opto board you recommended but couldn’t that to work either

Brown = GPIO 23
Blue = GND
White = 3.3v

My orange wire is on the GND side of the input. I don’t get anything if I connect that GND input side wire to any of the 4 inputs. I also tried moving the orange wire to the NC terminal on the other end and touching all 4 input terminals but didn’t get anything.

Interestingly, when I connected the opto board you recommended to my ESP chip it must have grounded my GPIO 23 pin, because it showed my binary sensor as “on” in HA.

1 Like

You need to supply +VCC to the 1+ terminal, and ground the 1- terminal to activate the LED in the opto-isolator. That should illuminate the output side LED.

Thanks, that worked. It makes sense now, I’m not sure why I wasn’t grasping this earlier.

If anyone else wants to do this, and for my documentation, here is my wiring
Opto Board → ESP32
VCC → 3.3v
GND → GND
01 → GPIO23 (could be any IO pin)
1- → GND
1+ → Door sensor
Connect 3.3V to the other door sensor terminal

3 Likes

Idea of the optoisolator is to break conductive part between side A and B. You are using gnd as common for both. Ground (or minus) is also an conductor that will carry current. It is not only input pins that need protection.

Correct way of using opto would be to have own power supply on both sides that are galvanically different circuits. And preferably use higher voltage e.g. 24vdc, if your opto is ok with that. This way signal in wires is ensured to go longer way and with the opto esp stays on 3.3v logic level.

If it works, I’d say let it run. If your esp gets fried some day, replace it and improve the circuitry.

For the pins in esp32. If you run into wifi problems, it’s most like because of pins used for ADC2.

Pulling wires like in your project should be standard! I have wireless stuff and I hate those…

Correct in that ideally, the LED side of the coupler would be interdependently powered and not share anything with the transistor side of the coupler. However the goal here is to prevent long antennae wires from being connected directly to the MCU pins. The shared power supply and ground plane could be a source of interference in some cases. In practice, unless galvanic isolation is required, it is usually fine to share. Commercial alarm panels, in my experience anyway, aren’t galvanically isolated on the sensor side.

Thanks for all the tips. I feel like an idiot for not trying to power the input side of the opto-isolator with a different source. That makes a ton of sense.

At this point, I’m going to leave it as it is, with the wires plugged in to the GPIO pins. If I fry a board I’ll change over to the opto-solution.

@bphillips921

Hi Brad,

How did you get past this issue of sending different MQTT payload depending on the door that tripped the switch?

Thanks!

I’m using ESPHome. Here is my code for one of the binary sensor boards

binary_sensor:
  - platform: gpio
    pin: 
      number: GPIO23
      mode: INPUT_PULLUP
    name: "Terminal 1"
    device_class: door
  - platform: gpio
    pin: 
      number: GPIO22
      mode: INPUT_PULLUP
    name: "Terminal 3"
    device_class: door
  - platform: gpio
    pin: 
      number: GPIO21
      mode: INPUT_PULLUP
    name: "Terminal 4"
    device_class: door
  - platform: gpio
    pin: 
      number: GPIO19
      mode: INPUT_PULLUP
    name: "Terminal 6"
    device_class: door
  - platform: gpio
    pin: 
      number: GPIO18
      mode: INPUT_PULLUP
    name: "Terminal 7"
    device_class: door
  - platform: gpio
    pin: 
      number: GPIO5
      mode: INPUT_PULLUP
    name: "Terminal 9"
    device_class: door
  - platform: gpio
    pin: 
      number: GPIO17
      mode: INPUT_PULLUP
    name: "Terminal 10"
    device_class: door
  - platform: gpio
    pin: 
      number: GPIO16
      mode: INPUT_PULLUP
    name: "Terminal 12"
    device_class: door
  - platform: gpio
    pin: 
      number: GPIO32
      mode: INPUT_PULLUP
    name: "Terminal 13"
    device_class: door
  - platform: gpio
    pin: 
      number: GPIO33
      mode: INPUT_PULLUP
    name: "Terminal 15"
    device_class: door
  - platform: gpio
    pin: 
      number: GPIO25
      mode: INPUT_PULLUP
    name: "Terminal 16"
    device_class: door
  - platform: gpio
    pin: 
      number: GPIO26
      mode: INPUT_PULLUP
    name: "Terminal 18"
    device_class: door
  - platform: gpio
    pin: 
      number: GPIO27
      mode: INPUT_PULLUP
    name: "Terminal 19"
    device_class: door
  - platform: gpio
    pin: 
      number: GPIO14
      mode: INPUT_PULLUP
    name: "Terminal 21"
    device_class: door
  - platform: gpio
    pin: 
      number: GPIO12
      mode: INPUT_PULLUP
    name: "Terminal 22"
    device_class: door
  - platform: gpio
    pin: 
      number: GPIO13
      mode: INPUT_PULLUP
    name: "Terminal 24"
    device_class: door
1 Like

Excellent friendly name(s), seems like these match with the numbers on the green terminals!!

I saw several PCB terminal connectors on Amazon but none of them looked like the green terminal block you have used, if possible please share a link to the SKU.

Thanks a lot for the help!

Yep, I’ll change the friendly names to reference the door once I have it all installed.

I used these Din rail mount terminals. I 3D printed my base and took the terminal board off the din rail mount then screwed it to my 3D printed board.

1 Like

Hi there @bphillips921! I’ve been eagerly following your advice on the reed switches and I bought everything you mentioned and tried to wire up everything as you showed.

Some context: I bought a home roughly a year ago and it came with a disabled alarm system. I tested it and apparently it was working, sensors included.

I replaced the old DSC alarm panel with a ESP32 with ESPhome and an optocoupler. I wired as you suggested in another thread, 1+ to hot wire from adapter, then jumper wires between positives on the “outside” of the opto coupler. I connected one of the legs of the reed switch on the negative on the optocoupler and another leg on the distributor you linked to the GND (or negative) of the adapter. 3.3v, ground and GPIO wires on the “inside” of the optocoupler.

The problem is… nothing works! I open windows and doors and nothing changes on ESPhome. I checked the optocoupler and all LEDs are always turned off. If I use a jumper wire I can make them light up… Should I plug the positive (or hot wire) into the reed switch instead? I’m not an expert at all, just a regular software guy here, trying to get my head around this :slight_smile:

Thanks for the detailed posts - helped a lot!

Can you post a picture of your Optocoupler wiring? That might help me visualize how you wired it.

Do you have an electrical tester you can use to test the reed switch? Make sure it is switching from open to close based on the door/window being open or closed.

Hi Brad,

I’ve got my door se so working without the opto, but now I’m trying to add it. I tried to follow your wiring but I must have it wrong.

Here’s what I’ve got. Two pins are in the ground on the opto. When I plug in the ESP32 to the power, the opto doesn’t light up.

Thoughts?

Your opto board is a 12v input and 3.3 output, the ESP board shouldn’t be connected to the 12v side of the opto board at all. Use the 3.3v out and the gnd pins on the ESP and connect those to the gnd and vcc on the 3.3v side of the opto. Then connect the “o1” on the 3.3v side of the opto to any GPIO pin on the ESP.

On the 12v side of the opto, hook the + line from any external 12v power source to the 1+ terminal. Then hook either wire from your Reed switch to the 1- terminal on the 12v side of the opto. The other Reed switch wire connects to the - wire on your external 12v power supply.

The Reed switch opening and closing will connect/disconnect the 12v power feed to the opto. That’s how it detects the Reed switch. If the opto receives a 12v signal the door is open, if not its closed, or vice-versa

Got it (and wired it up). That makes so much more sense! I had read the comment above about separate power sources for the opto and the esp32. Now it makes more sense. I also thought I had bought the 3.3v version of the board. Whoops.

I’ve been reading but really struggling to understand how this works. I was missing the two sides, different power sources, and then basically one to one in to out. (meaning +1 and -1 to o1, so I’m assuming +2 and -2 to 02 to another pin on the esp32).

One last question. I assume that all the other reed switch wires that are connected to the negative on the 12v power are wired together?