Switch Flip Flop

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
gregoinc
Normal user
Posts: 63
Joined: 21 Jan 2019, 06:08
Location: Australia

Switch Flip Flop

#1 Post by gregoinc » 14 Feb 2021, 11:06

Hello,

Long time ESPEasy user and have been very happy. I recently installed a reed switch on a door and following the instructions documented here https://espeasy.readthedocs.io/en/lates ... witch.html

Everything seemed to go well, and with the door closed everything appears to be fine, see here...
Screenshot from 2021-02-14 20-58-36.png
Screenshot from 2021-02-14 20-58-36.png (7.94 KiB) Viewed 688 times
But when I open the door and leave it open the switch appears to flip flop back and forwards from open to closed to open to closed... etc. I've tried using a connection to ground and a connection to 3 volts as per the guide at the link above, but nothing seems to fix it? The issue only presents itself when the door is lext open... when it is closed it doesn't flip flop?

Any ideas?

Thanks, Mark

User avatar
Ath
Normal user
Posts: 565
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Switch Flip Flop

#2 Post by Ath » 14 Feb 2021, 11:17

- Be sure to use a pull-up resistor on the switch contact, mounted close to the ESP
- Don't use very long wires (>5 meters could be problematic, depending on conditions)
- Use a longer de-bounce time (max possible is currently 250 msec)
/Ton

TD-er
Core team member
Posts: 4272
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Switch Flip Flop

#3 Post by TD-er » 14 Feb 2021, 13:01

You could also place a small capacitor over the GPIO pin and the GND.
Something like 100 nF, but capacity doesn't really matter as long as it isn't too large.
This will cause a slight delay in the processing of the switch, but for a door this doesn't matter a lot.
What it also does, is prevent short electromagnetic signals to be picked up by the wires as signals.

gregoinc
Normal user
Posts: 63
Joined: 21 Jan 2019, 06:08
Location: Australia

Re: Switch Flip Flop

#4 Post by gregoinc » 15 Feb 2021, 08:18

Hi Ath and TDer,

Thank you for your replies. I wonder, could you please point me towards an example circuit diagram? I know my way around electronics but have no knowledge of hooking up a reed switch utilising a resistor or capacitor. A diagram would be a massive help.

Thanks, Mark

User avatar
Ath
Normal user
Posts: 565
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Switch Flip Flop

#5 Post by Ath » 15 Feb 2021, 09:32

The generic pull-up resistor would look like this Wikipedia page describes: https://en.wikipedia.org/wiki/Pull-up_resistor

When combining a resistor and a capacitor, this discussion seems appropriate. (Just use the capacity TD-er suggested, not 10 uF)

Both situations assume that the switch (either a reed switch or regular switch) connects the I/O pin to ground when active.
/Ton

TD-er
Core team member
Posts: 4272
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Switch Flip Flop

#6 Post by TD-er » 15 Feb 2021, 09:58

In short, a pul-up resistor may pull the voltage of the connected GPIO pin to the same level as on the other side of the resistor unless it is pulled down.
So your reed switch may pull it down as the resistance of the reed switch is close to 0, especially compared to the resistance of the pull-up resistor (typically in the order of 10k)

Thus reed switch closed, level low, reed switch open, level high.
A reed switch may "bounce" a bit when almost closed or being closed.
A capacitor may help to limit this bouncing a bit as the response of the signal is delayed a bit by the capacitor. (it takes longer for a voltage to change)

The wires to the reed switch act as an antenna and thus generate voltage changes, which will be dampened by the capacitor.

The "schematic":
Vcc -> pull-up resistor -> GPIO pin -> capacitor -> GND

Your reed switch will be connected on the GPIO pin and GND.

You may also want to try to twist the wires to the read switch, the same as is done in UTP cables.

gregoinc
Normal user
Posts: 63
Joined: 21 Jan 2019, 06:08
Location: Australia

Re: Switch Flip Flop

#7 Post by gregoinc » 15 Feb 2021, 10:07

I found these diagrams.

This one with a capacitor and resistors:-
Hardware-Debounce-False-Interrupt-Fix.jpg
Hardware-Debounce-False-Interrupt-Fix.jpg (77.38 KiB) Viewed 644 times
And this one... with just a resistor:-
reed2_bb.png
reed2_bb.png (102.6 KiB) Viewed 644 times
I am thinking it might be an idea to start with the resistor first... or not?

TD-er
Core team member
Posts: 4272
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Switch Flip Flop

#8 Post by TD-er » 15 Feb 2021, 10:19

The resistor should be used anyway.
Either you have it already in place when you're using a pin which needs a pull-up resistor like GPIO-0 or -2. Or you have checked the "use internal pull-up resistor" in the switch plugin.

gregoinc
Normal user
Posts: 63
Joined: 21 Jan 2019, 06:08
Location: Australia

Re: Switch Flip Flop

#9 Post by gregoinc » 15 Feb 2021, 10:38

Hi TD-er,

Funny you mention the pull up resistor option in the switch device settings. When I tick the Internal PullUp option the reed switch never detects the door is open, yet when the Internal PullUp is un-ticked the door is detected opening/closing. I will place a resistor as per the resistor only diagram and see what happens.

Very interesting indeed... keeps me entertained during our COVID-19 lockdown ;)

TD-er
Core team member
Posts: 4272
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Switch Flip Flop

#10 Post by TD-er » 15 Feb 2021, 10:44

Hmm when you're experiencing it like this, I wonder how you connected the reed switch.
Did you maybe connect the reed switch between 3V3 and the GPIO pin?

gregoinc
Normal user
Posts: 63
Joined: 21 Jan 2019, 06:08
Location: Australia

Re: Switch Flip Flop

#11 Post by gregoinc » 15 Feb 2021, 11:09

TD-er wrote: 15 Feb 2021, 10:44 Hmm when you're experiencing it like this, I wonder how you connected the reed switch.
Did you maybe connect the reed switch between 3V3 and the GPIO pin?
Yes, correct... which I am thinking now was not such a good idea... :oops:

TD-er
Core team member
Posts: 4272
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Switch Flip Flop

#12 Post by TD-er » 15 Feb 2021, 12:16

Enable the internal pull up resistor and connect the reed chip between the GPIO pin and GND.
Just make sure you're not using GPIO-0 or GPIO-2 for this on an ESP8266 as you may then prevent the ESP to boot if the reed switch is closed during reboot.

gregoinc
Normal user
Posts: 63
Joined: 21 Jan 2019, 06:08
Location: Australia

Re: Switch Flip Flop

#13 Post by gregoinc » 16 Feb 2021, 05:47

TD-er wrote: 15 Feb 2021, 12:16 Enable the internal pull up resistor and connect the reed chip between the GPIO pin and GND.
Just make sure you're not using GPIO-0 or GPIO-2 for this on an ESP8266 as you may then prevent the ESP to boot if the reed switch is closed during reboot.
That was my original design, but it flip/flopped with that... even with the internal pull up turned on. That prompted me to changed it to the 3v3 <---> GPIO and found it flip/flopped that way as well. I think I will try the physical resistor as per the design above. I am using GPIO-13.

TD-er
Core team member
Posts: 4272
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Switch Flip Flop

#14 Post by TD-er » 16 Feb 2021, 08:37

I really think the resistor doesn't play a big role here.
What you can do if you only have resistors laying around, is place 2 resistors.
The internal pull up is in the order of 50k - 100k, so it is a rather weak one.
What you can consider is this:

3V3 -> R1 -> GPIO -> R2 -> GND

R1 is now your internal pull-up and R2 is now not present. (N.B. the reed sensor will short R2 when closed)

You must make sure the resistance of R2 is quite a bit higher than R1.
I would say a factor 5 - 10.

So if you stick with the internal pull-up, your R2 must be somewhere between 470k and 1M

If you also place one for R1, then just pick something like 10k (or +/- 50%) and pick one for R2 which is 5 - 10x as high.

Advantage of lower resistance is less prone to noise.
Disadvantage is higher power consumption and higher current through the reed switch.

You may also want to twist the wires to the reed switch as this reduces noise considerably.

gregoinc
Normal user
Posts: 63
Joined: 21 Jan 2019, 06:08
Location: Australia

Re: Switch Flip Flop

#15 Post by gregoinc » 17 Feb 2021, 06:17

Hi TD-er,

Thanks for that. Here's a diagram I've drawn from your advice.
ResistorSchematic.png
ResistorSchematic.png (4.89 KiB) Viewed 474 times
Assuming I have it correct, where would the reed switch connect?

Thanks, Mark

TD-er
Core team member
Posts: 4272
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Switch Flip Flop

#16 Post by TD-er » 17 Feb 2021, 08:35

The reed switch should be bridging the resistor with the highest resistance.
In rest (reed switch open), the voltage over GPIO & GND = 3V3 * (R2 / (R1 + R2))
Or if you're more familiar to reading ratios.
3V3 is split in the ratio R1 : R2

If you short the reed switch, you end up changing the resistor it bridges to a resistance of close to 0 Ohm.

Let's assume you place it parallel to the lowest resistance (R1), then it will hardly change the voltage over GPIO (to GND) as the ratio is already 1 : 10.
If you place it however over R2, then the ratio changes from 1 : 10 to almost infinity : 1.

Thus if you want "reed closed = 1", then you should use R1 with the highest resistance and the reed switch bridging GPIO and 3V3.
To invert it, use the lowest resistance for R1 and bridge GPIO and GND with the reed switch.

gregoinc
Normal user
Posts: 63
Joined: 21 Jan 2019, 06:08
Location: Australia

Re: Switch Flip Flop

#17 Post by gregoinc » 21 Feb 2021, 04:02

I tried a number of combinations, they didn't work. Ended up using one of these boards... works perfectly.
20210221_132703.jpg
20210221_132703.jpg (58.82 KiB) Viewed 356 times
Thanks for all your help TD-er.

TD-er
Core team member
Posts: 4272
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Switch Flip Flop

#18 Post by TD-er » 21 Feb 2021, 13:37

What kind of board is it exactly?
I assume it is a flipflop, but what's the trimmer for?
Setting a level?

gregoinc
Normal user
Posts: 63
Joined: 21 Jan 2019, 06:08
Location: Australia

Re: Switch Flip Flop

#19 Post by gregoinc » 22 Feb 2021, 04:44

TD-er wrote: 21 Feb 2021, 13:37 What kind of board is it exactly?
I assume it is a flipflop, but what's the trimmer for?
Setting a level?
Here's the link to the board... basically replaced the 'rain pad' with a connection to the reed switch.

https://www.aliexpress.com/item/3261565 ... 4c4djGpVbg

Essentially the same as this board...

https://www.aliexpress.com/item/1005001 ... web201603_

Still working perfectly :)

TD-er
Core team member
Posts: 4272
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Switch Flip Flop

#20 Post by TD-er » 22 Feb 2021, 08:55

What is the chip model nr?
It looks like an opamp.

Edit:
Ah found a similar looking board, which does have a schematic mentioned:
https://nl.aliexpress.com/item/32764113 ... 3dfewY59YN
Is indeed an opamp.
And it uses 100 nF (10 with 4 zeroes in pF) to filter out noise with a pulse duration of 0.001 sec = 1 msec.
Used this time constant calculator

Image

gregoinc
Normal user
Posts: 63
Joined: 21 Jan 2019, 06:08
Location: Australia

Re: Switch Flip Flop

#21 Post by gregoinc » 22 Feb 2021, 12:10

Looks exactly like the board I used, and it works well. I must confess, I still dont understand why the other solutions didn't work? Perhaps the distance?

TD-er
Core team member
Posts: 4272
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Switch Flip Flop

#22 Post by TD-er » 22 Feb 2021, 13:31

Looking at the schematic, it does provide some filtering and setting a threshold.

So what it does is very similar to the resistor + capactor I suggested.
With as extra, it allows to tune the level at which it will switch over.

The capacitor does get charged via the 10k resistor on the board, which takes a while. (depending on the resistor + capacitor values)
In this setup the so called "RC-time" is 1 msec, meaning it does take 1 msec to go from 0 to roughly 70% of the charge voltage.
A pulse introduced by noise may have a shorter duration and/or a lower voltage (which is used to charge the capacitor) so it will not cross the level change threshold.

The trimmer on the board can be used to set the level at which you need to trigger a change, which makes it much more flexible, so I would say the perfect solution for your setup.

Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests