Detect Reed Switch State

I have a NodeMCU flashed with ESPHome. I have installed a magnetic reed switch on a door. I want to use that switch to detect the state of said door, perhaps taking action or at least reporting when its state changes.

I assume connecting the switch to D4 and 3.3v is the starting point. I can’t find a pre-configured state-switch YAML code to use it. Any ideas?

1 Like
1 Like

I have it configured on my gate : the ESPHome config is as follows :

esphome:
  name: mcu3
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "XXXXXX"
  password: "XXXXX"

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
      
switch:
  - platform: gpio
    name: "Open Gate"
    pin: 5
    
binary_sensor:
  - platform: gpio
    name: "Gate is closed"
    pin:
      number: 16
#      inverted: true
      mode: INPUT_PULLUP

Yiu should be able to see the sensor in the integrations :

and here :

Now you have a binary sensor that you can add (as an entity or otherwise) in Lovelace and see the status.

Makes sense ?

Thank you for your help.

I can see the entity, but no change when I open or close the switch, either with the magnet or just shorting the wires. Wires are connected to 3.3v and GPIO2 (D4) right next to it. No external resistors. Here’s the YAML:

esphome:
  name: nodemcu1
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "xxx"
  password: "x"

# Enable logging
logger:

# Example configuration entry
sensor:
  - platform: wifi_signal
    name: "WiFi Signal Sensor"
    update_interval: 60s
    
# Enable Home Assistant API
api:
  password: "xxx"

ota:
  password: "xxx"

binary_sensor:
  - platform: gpio
    name: "Garage Door is Open"
    pin:
      number: D4
      inverted: true
      mode: INPUT_PULLUP

Do you see anything in the esphome log?

you must connect to 0v when your input is a input_pullup type.

If you want to know in detail which I recommend read this https://www.electronics-tutorials.ws/logic/pull-up-resistor.html

1 Like

OK, some progress. Switch, which has been tested to work, is now connected across D4 and gnd. When the switch closes (magnet is engaged…) the blue LED on the NodeMCU lights up, but no status change is sent. I confirmed this by observing silence in the ESPHome logs. The logs do show NodeMCU1 sending WiFi status messages, but no status on the switch.

Again, no external resistors.

Thanks for the tutorial. I’m reading it now.

Alternatively, you could use input_pulldown_16, which would require no hardware change. You are conveniently using the only pin that supports this. I use input_pullup FWIW.

1 Like

Here’s the log from ESPHome. It is not reporting the repeated opening/closing cycles I’ve taken the switch through. Every time I toggle the switch, the LED near the USB port toggles with it, so I know the board is seeing the switch toggle (though I can’t say why the LED is toggling, LOL.)

INFO Reading configuration...
INFO Starting log output from nodemcu2.local using esphome API
INFO Connecting to nodemcu2.local:6053 (192.168.2.115)
INFO Successfully connected to nodemcu2.local
[12:10:00][I][app:096]: esphome version 1.13.6 compiled on Aug  2 2019, 15:44:35
[12:10:00][C][wifi:372]: WiFi:
[12:10:00][C][wifi:254]:   SSID: [redacted]
[12:10:00][C][wifi:255]:   IP Address: 192.168.2.115
[12:10:00][C][wifi:257]:   BSSID: [redacted]
[12:10:00][C][wifi:258]:   Hostname: 'nodemcu2'
[12:10:00][C][wifi:262]:   Signal strength: -63 dB ▂▄▆█
[12:10:00][C][wifi:263]:   Channel: 6
[12:10:00][C][wifi:264]:   Subnet: 255.255.255.0
[12:10:00][C][wifi:265]:   Gateway: 192.168.2.1
[12:10:00][C][wifi:266]:   DNS1: 192.168.2.1
[12:10:00][C][wifi:267]:   DNS2: 0.0.0.0
[12:10:00][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Garage Door'
[12:10:00][C][gpio.binary_sensor:015]:   Device Class: 'garage_door'
[12:10:00][C][gpio.binary_sensor:016]:   Pin: GPIO16 (Mode: INPUT_PULLUP)
[12:10:00][C][logger:137]: Logger:
[12:10:00][C][logger:138]:   Level: DEBUG
[12:10:00][C][logger:139]:   Log Baud Rate: 115200
[12:10:00][C][logger:140]:   Hardware UART: UART0
[12:10:00][C][ota:029]: Over-The-Air Updates:
[12:10:00][C][ota:030]:   Address: nodemcu2.local:8266
[12:10:00][C][ota:032]:   Using Password.
[12:10:00][C][api:103]: API Server:
[12:10:00][C][api:104]:   Address: nodemcu2.local:6053

Tried that, no change.

I would try a different pin.

Yes, your D4 pin might have got damaged.

Anyway D4 is not a good idea for a reed switch, if the switch is made then the ESP will not boot properly after reset or power up.

If you try with D0 you’ll need a pull up resistor, i.e. a 10k ohm resistor between D0 and 3v will do.

OK, I finally changed to pin 4 and it worked fine. Pin 3 also works fine. So does pin 9.

At this juncture, I only need three, so I’m good now. I don’t know what’s up with the others. I’ll try gimnet’s resistor idea later.

Thanks for your help, folks!

Hello Jim
I want to do same thing like you. I have Mc38 magnetic switch , ESP-01 (or NodemCU) and buzzer.

Can you share latest code and pinout schema?

Thanks