Reed Switch sensor values floating all over the place

My garage door opener project is using reed switches to detect whether the door is opened/closed. They are the type of switch that is normally closed.

Honywell 945T Reed Switch

http://www.explainthatstuff.com/howreedswitcheswork.html (scroll down to the heading Normally Closed).

These switches are mounted on the metal rails that the door slides on. I’m guessing this is the normal way everyone does these garage door switches.


Switch used to detect if door is open. On top of metal rail that door slides on.


Switch used to detect if door is closed. Here the door is in a closed state or what I’m calling an ‘activated’ state. The two pairs of the switch are within 1" of each other thus causing the switch to activate and open the circuit (turn if off). I used black duct tape to fasten the other half of the switch to the door.


Each switch is connected directly to analog pins (A5 and A2) and ground. When the switch is activated the readings generally fall below 20. When it’s not activated, and in the normally open state, the readings are all over the place but seldom go below 20. I took some readings over time of an activated and non-activated switch with nothing moving during that time. The readings were taken every 5 seconds (via serial console) over a period of 25 minutes for 300 separate data points. analogRead value is Y axis and minutes is X axis.


The way the above values are jumping all over the place just seems so weird like I must be doing something seriously wrong. There seems to be some kind of sine wave pattern to the fluctuations which makes it even more strange – like it’s moving up and down according to some mysterious cosmic force. The lowest value in this chart was a 16.


I don’t see a sine wave pattern here.


Questions:

  • Is this wild fluctuation for a non activated switch somehow the normal expected behavior ? If it is normal then I’d sure like to know how it makes sense this could be happening.
  • Do I just continue to make a best guess of what the threshold should be to determine if the switch is activated? Right now I’m using 20 but it seems that this value is not absolute as there was one case where switch returned a 16 even though the switch was not activated! And when the switch activated there was a case where the value was above 20 (as shown in the chart). So it making an impossible situation where there is no 100% certain way to determine the status.
  • Is my approach all wrong and I’m needing to do some kind of hardware thing to fix this so the determination can be made with something closer to 100% certainty ?

Thank you!

Unplug the power to your Garage Door Opener and see if that wave stops :smiley:

Why are you reading the switch’s state with an analog input instead of a digital one? Try connecting one end of the switch to ground and the other end to a digital input set to INPUT_PULLUP (or you could do the opposite, connecting the switch to 3v3 and use INPUT_PULLDOWN). When the switch is closed it should read LOW, and HIGH when it’s open.

5 Likes

When you read digital then any voltage above 1.5v is High and any voltage reading below 1.5v is Low. On 5v systems the High is above 2.5v and below 2.5v is considered LOW. Any readings above or below the threshold voltage is ignored and does not trigger a state change.

Analog read will provide number for the voltage on that pin which can range between 0 - 5v.

Sorry about adding a post and then deleting it. Had been doing some trouble shooting this morning and accidentally disconnected one of the switches. The readings I was taking were meaningless as the switch was not even connected! So I had to get rid of that post where I was referring to results from the bad data.

Am now doing some new tests with the garage open door unit powered down. But the results are still the same with the values coming back all over the place as before. It looks like the same wave is there – goes up and down about every 90 seconds.

It seems like digital read and analog read are interchangeable. It just depends on whether or not you want to figure out what the voltage is and check whether the value from the sensor is above the halfway threshold? In the case of this reed switch I’m not seeing how that will offer any help as this Normally Open switch is coming back with values that are in a range that is falling way below the halfway point when that switch is not activated.

Maybe the PULLUP and/or PULLDOWN concepts can help? I’m kind of confused by them but maybe it will start making sense if I look at it again.

The pull up pull-down settings can help so try the advice on how to emplement this from above and report back.

Sounds good. Got to do some errands so it might not be for a few more hours till I come back with something. Thanks to you guys for the ideas to look into!

start by hooking a continuity meter up to you reed switch with it disconnected from the circuit. Make sure it’s reliably opening when the magnet is near it. In you picture there the magnet has a fairly significant gap. These work best when they are close together, but I realize you need some mechanical tolerance there so they don’t slam into each other.

Once you have that working, start to implement one of @Ric’s suggestions above for digital input to see if it works.

It would be worth adding a lower value external pullup/pulldown resistor as well to ensure noise on the long wires (I’m assuming they might be long) don’t pull your input around. 1k ohm should be sufficient and much better than the internal 40k ohm weak pullup/down resistors.

If the wires are long, you could also benefit from a pair of clamping diodes on the input. Anode on input, cathode to 3V3. Cathode on input, anode to GND.

2 Likes

Prior to building the circuit I ran many tests to see how reliably the switch would open as the magnet was brought nearer. It proved to work very well and could easily handle a 1" gap. I don’t think the installation I have is pushing that boundary.

I’m getting over my head right now. Trying to read the Particle INPUT, OUTPUT, INPUT_PULLUP, INPUT_PULLDOWN documentation but am just not getting it. Hoping eventually it will start to click after I read a bunch of articles. Also need to read up on what is a clamping diode.

The wires are fairly long. Maybe 10 feet. I used whatever wire I had available so they are not the same gauges. The longest wires were stripped from a phone line cable and are pretty light gauge copper.

Don’t try to make it to hard. Break it down into what you want to do.

The reed switch will provide an input the photon, a digital input takes a voltage and decides if it is true (voltage applied) or false (connected to ground).

If the input has neither a voltage applied or connected to ground it could be read either way. That is were the pull-up or pull-down term comes in. It forces it to be read as true (pull-up) or false (pull-down) when the pin is not connected.

Digital pins are normally used to read switch and buttons etc.

Analog pins are normally used to read a voltage.

In you case if you are connecting one wire of the switch to ground and the switch is normally closed when the magnet is not near it. I would use a digital pin set as input with pull-up. That way when the magnet approaches the switch the switch will open, the pull-up resistor will make the pin go true.

When the magnet moves away from the switch the switch closes and drops the voltage to ground. The pin will read the ground voltage as false (the switch less resistance than the pull-up resistor).

Hope that helps.

1 Like

I might actually have something that works now. But I was sooooo confused :confused: . So in the dark that I did not even have the awareness that a switch and a sensor are very different categories of electronic things. Thank you very much to all of you for helping me see the light!

Before I was thinking this switch was either on or off. And if it had current flowing, like a light bulb, then it was on. But am now realizing that makes no sense. I was thinking of the switch as a sensor that is providing a current value to a pin indicating the relative status of something being measured. The data sure made it look like a current value was being sent from the switch. I was figuring that the value was telling the relative ‘openess’ of the switch and that I had messed something up with the hardware aspect that was causing the value to not be consistent.

Another light bulb went on when I realized the values were still appearing and changing in the same pattern after I had absent mindlessly forgot to connect one of the switches. So that blew my confused understanding even more out of the water as the switch was not even connected and therefore it was not able to supply the sensor current I was looking to measure. Yet some kind of phantom ‘current’ was still appearing!?

Now both pins are set INPUT_PULLUP and am doing digital reads. Since the switches are closed by default they are at HIGH until the magnet is near and then they are LOW. The triggering occurs when a move from 1 (circuit closed) to 0 (circuit open) occurs. Yeah!

1 Like

One misconception that may have lead to your confusion is that you seem to expect to measure currents with INPUT pins, but you don’t.
You measure voltages (electric potential).

Would INPUT pins be measuring voltage in the case of switches or would this it also apply to sensors like a sound detector or a piezo vibration sensor (both of which I’m using in the my project).

Sorry if I’m asking such basic questions. I have made some effort to get a basic understanding of electronics but find I keep tripping over myself when I try to apply that understanding.

There are basically two types of Input pins, Digital ( I detect the presence of a voltage or not) and Analog( I detect how much of a voltage is present).

One would typically use a digital pin for a digital signal like your switch, but analog pins may also serve that purpose.

Digital pins may not be used as analog pins, however…

3 Likes