Connecting reed sensor to ESP32

I would like to hook a reed sensor (this one: https://photos.app.goo.gl/VW4YKFUqLu7AmyCL7, which I assume is the same as this one MS-214-3) up to my ESP32 board (this one: ESP-32 Dev Kit C op az-delivery.de).

But I’m not sure whether I can connect both wires to pins just like that. I’d like to avoid a short, obviously. Now, looking at the data sheet from PIC GmbH, it looks like the reed sensor also consists of a resistor, so I assume it’s safe?

Also, this is the code:

esphome:
  name: watermeter

esp32:
  board: az-delivery-devkit-v4
  framework:
    type: arduino

logger:

api:
  password: ""
  encryption:
    key: "xxxxxxx"
  # Set pulse counter total from home assistant using this action:
  actions:
    - action: set_pulse_total
      variables:
        new_pulse_total: int
      then:
        - pulse_counter.set_total_pulses:
            id: watermeter_pulse_counter
            value: !lambda 'return new_pulse_total;'

ota:
  - platform: esphome
    password: ""

wifi:
  ssid: "xxxxxxx"
  password: "xxxxxxx"

  manual_ip:
    static_ip: 192.168.1.169
    gateway: 192.168.1.1
    subnet: 255.255.255.0

  ap:
    ssid: "xxxxxxx"
    password: "xxxxxxx"

captive_portal:

switch:
  - platform: restart
    name: opnieuw_opstarten
    id: opnieuw_opstarten
time:
  - platform: homeassistant
    id: tijd
sensor:
  - platform: uptime
    type: timestamp
    name: laatste_herstart
    id: laatste_herstart
  - platform: pulse_counter
    pin:
      number: 14
      inverted: true
      mode:
        input: true
        pullup: true
    unit_of_measurement: l/s
    name: watermeter_pulse_counter
    id: watermeter_pulse_counter
    filters:
      - multiply: 15  # (60 s / 4 pulseringen per liter)

    total:
      unit_of_measurement: l
      name: watermeter_pulse_counter_total
      id: watermeter_pulse_counter_total
      filters:
        - multiply: 0.25  # (4 pulseringen = 1 liter)

Does that look good?

Thanks for anyone’s answer (who knows more of it than I ;))!

A reed sensor is just a normal open switch, like a push button. But the ‘push’ in this case is done with a magnet that closes 2 strips.

I see that you use ‘pullup’ in your code. In that case you can connect one wire to the pin and the other wire to gnd. There is no polarity to the wires. Just use one or the other. of metal.

2 Likes

I figured that. But sending a voltage directly to the ground without any resistor or ‘power consumer’ seems like a bad idea to layman me? Or is that not a problem?

You don’t send anything. Your pin is configured as input with weak internal pullup.

1 Like

Mmm, that I don’t understand, unfortunately. Hopefully that understanding or knowledge comes soon.

But for now, I’ll take your word for it :wink:

I conclude I can go ahead as planned.

Thanks!

The key is that it’s a pull-up resistor, so you’re not directly connecting Vcc to GND. As soon as there’s an easier path (the switch closes), the pin sees GND instead of Vcc.

1 Like

A great learning experience is to set it up without the pull up resistor and have it control something like a light. You won’t need to do it twice.