Reliably counting a reed switch with Tasmota

I've now spent around 15+ hours to get Tasmota on my ESP32-WROOM-32D to reliably count impulses of a reed switch (WH-SP-RG rain meter, which sends one pulse (closed-open) for every 0,33 mm of rain).

I tried (among others) the to follow the following articles: debouncing a reed switch or Reed switch - to de-bounce or not to de-bounce.

With every solution I tried, another problem occurs:

  • Inconsistent double counts
  • "Ghost counts" (~1 - 3 per hour)
  • No counts at all (depending on the input or hardware setup, see below)
  • ...

Throughout my attempts I realized that I probably lack the basic knowledge and experience in making the right decisions in three main areas:

Which GPIO do I best use for what I'm trying to do?

  • In the documentation of the board I read that some GPIOs have "weak pull-up" or "weak pull-down". Which one shall I use?
  • Or would "weak pull-up" or "weak pull-down" of the GPIO mean that I would not need an additional hardware pull-up or pull-down?
  • Are the pull-ups or pull-downs on the board activated by default, or do I have to activate them in software?

Which hardware setup do I best use for what I'm trying to do?

  • Do I have to connect the reed switch to Ground and the GPIO? Or to 3V and the GPIO?
  • Do I need a hardware pull-up or pull-down resistor? Or both at the same time?
  • In one attempt I build a pretty neat hardware debounce in hardware (resulting in a perfectly smooth curve as checked via an oscilloscope), but which still resulted in ghost counts or double counts.

What's the right setting in Tasmota?

  • Shall I use "Counter" or "Counter_n"?
  • I got the impression that "Counter" or "Counter_n" sometimes count "+2" or "+1" for "reed switch closed and opened". Is there a possibility to get "Counter" in Tasmota to count only once (+1) for "reed switch closed and opened"?
  • I also tried using CounterDebounce in Tasmota, which worked great, but also resulted in ghost counts.

Can anyone recommend a setup that reliable counts reed switch impulses?

Not sure what you have tried :thinking: .

How long is the reed switch closed for ?


Generally for reed switches.

  • Sample them every 30-50ms looking for a switch change.

  • Add a capacitor (start with 100nF) across the contacts, tune an external pull-up resistor (start with 33k going down.

See:

Reed switches are horrible anachronisms. Maybe rip the sensor apart and replace it with an integrated Hall switch?

+1 for Hall effect switches. I use the A3144 switches.. They are tiny, ruged and reliable. They don't wear out and they don't bounce.

I have been warned about using hardware debounce on reed switches. I was told that the cap causes tiny arcs when the conracts break. That can ruin the tiny contact surfaces on reed switches prematurely. Dont know how true it is, but it does sound reasonable.

Thank you all for your comments, especially @LarryD for the initial hint. I'm getting closer, but am waiting a few more days until I can announce that it‘s working (and describing what I did). Don‘t want to be too eager. :wink:

@jhaine & @groundFungus, thanks for the hints on reed switches. So I can’t say I haven’t been warned. Let‘s see how long this thing lasts… :wink:

So as said, thanks a lot for your thoughts.

Upfront: Replacing the reed switch with a hall sensor is the last thing I'd like to do, since there must be a way to cound a reed switch reliably. :roll_eyes:

I've implemented the following from the link above:

I chose the following components:

  • C1: 100 uF
  • R1: 10 kΩ
  • R2: 470 Ω
  • D1: MIC FR107 Rectifier Diode

I'm using a ESP32-WROOM-32D running Tasmota to count the pulses by the reed switch.

I've used a couple of different GPIOs on the board, since I learned from the Espressif's documentation below that some behave different than others:

The good:

  • It reliably counts two impulses per "click" of the rain sensor.

The bad:

  • I get "ghost counts" from time to time.

As mentioned, I already tried using different GPIOs (since I understood that some have a weak pull up or weak pull down included), but no change.

Does anyone have an idea? Maybe using a throttle? If yes, where? Or do I need to implement a filter in addition to the setting above? Are the ghost counts induced by the cable lenght (only ~3 meters)?

For a reed switch to work properly you need a BAR magnet that is parallel to the reed switch body. What is your configuration?

Thanks for your response!

From what I understood from your question, you're asking whether the reed switch is working correctly? I'm using the following component, which is basically a seesaw that moves a magnet past the reed switch, in case there's enough water on the seesaw.

Since the number of counted clicks is correct if I manually trigger the rain meter, I assume the manufacturer picked the right configuration.

So in short, the ESP basically counts everything it should count, but also more (--> impulses even if the seesaw does not move past the magnet).

My take so far: The ghost counts are caused by some sort of interference?

Certainly possible. My anemometer has false counts when I operate my high powered VHF ham radio equipment.

But you can quickly determine the source of your problem. Interference will cause false counts with no rain. Faulty equipment will cause false counts with rain.

In the faulty equipment case, add a 1 second wait after reading the first count. Surely you do not have enough rain to cause more than a 1 second pulse.

It’s definitely this one, since I get ghost counts even when testing the setup indoor (just letting it rest on my desk).

The last idea which I had was to add a Ferrite bead around the two wires to/from the reed switch, but without any luck (I believe fewer, but still present ghost counts).

Does anyone have any other Idee? I’m definitely out of ideas by now.

Make c1 100nF and R1 much lower, 5k. D1 is likely rectifying noise and producing a DC voltage for your Arduino to register, so leave it out. And no R2.

Length of wires to the Ardiuno is what? Try very, very short wires for testing,

Think "elimination" when fault finding.
Do you still get ghost counts when the sensor is removed.
Do you still get ghost counts if only sensor ground is connected.
What else is connected to the ESP. Any inductors/relays.
What happens if you use another supply.
Leo..

Maybe it's the code? Disconnect everything, ground the pin, leave it running.

First of all thanks a lot for all your comments!

I have not yet incorporated all your suggestions and run tests (especially @Paul_KD7HB's hints), but believe that the ghost counts are caused by the cable picking up interference, which are then counted. Does anyone have an idea on how to control them? Is there a filter which I can build?

Details:

Nope. If I just have the ESP running with nothing connected to it, there are no ghost counts.

Yes. This makes me believe the "interference-hypothesis" is true.

I haven't tried this one yet, but I believe that the ghost counts will be gone then.

Nothing else for the sake of testing, but still ghost counts.

Tested with different power supplies. No change (still ghost counts if the sensor is connected).

The problem is definitely not code-related, since I'm using the same code to count the signals of a hall sensor.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.