Contact sensor and pullup/pulldown resistors

Dear all,
I have a rain sensor which works like a contact sensor, when is dry the contact is close when it’s raining the contact is open.
I would like to connect this to the Argon. What is the best way to do it considering:

  • Length of the cable is 2m (with eventual interference)
  • The contact is closed for 70% of the time

Options:

  1. Connect one side to GND the other to the digital pin and use pinMode(pinNumber, INPUT_PULLUP);
  2. Connect one side to 3.3V, the other to the digital pin and use pinMode(pinNumber, INPUT_PULLDOWN);

Anything else I should consider, like resistor or ideas how to reduce interference?

If I instead decide to connect the sensor using a MCP23017/MCP23008 following this guide based the above options is this the correct configuration?
Option1: gpio.pinMode(myPin, INPUT_PULLUP); using the internal 100k resistor
Option2: gpio.pinMode(myPin, INPUT); plus a 100k resistor from the myPin to GND

Are 100k Ohm enough?

Kind regards,
dk

The main thing to consider with a 2m length of wire connected to either a pin on the Argon (any MCU) or a pin on the GPIO expander like the MCP23008 is that it can act like an antenna and because the resistance is very low even if only a small voltage is generated (through EMI) this could result in a high current - this will blow out the Pin.

Generally it is better to have external pull-up/pull-down resistors because they are passively always there rather than having to be set at startup. If your rain sensor is acting like a switch (but an electronic one) then using a pull-up or pull-down will depend upon how the “switch” works i.e. closed pulls the signal to ground or up to VCC.

The Pull-up/down resistor in the MCP23008 or Argon isn’t going to protect it from a voltage spike.

The typical protection for this is a small resistor (100 ohms) in line with the signal (this creates a small voltage drop but critically effectively limits the current. The other thing to add is a zener diode of what ever the voltage tolerance of the pin is (best is 3V zener for the Argon). This limits the maximum voltage.

Another approach is an opto-coupler where there is a physical break between the signal in and the connection to the pin on the Argon.

Lastly, it is good practice to shield the signal and ground cables in a woven mesh cover and have this attached to the ground plane on the MCU end.

Lastly, the rain sensor “switch” may exhibit some flutter or dead-band where it is neither solidly high or low. In this case you will need to look at using a software debounce algorithm that gives a clean signal raining or not raining to ensure your application works correctly.

@armor I appreciate your answer.

The sensor I believe is just a reed switch with the magnetic component which floats when is raining a lot and opens the circuit. I’m not so sure that the cable is shielded. The original purpose is very basic: cut the power supply to the valve if raining.

A 100Ohm resistance from the pin to: 3.3V (option 1) and to the GND (option 2)?
The zener diode is an addition to the reisistance you mean? With the anode to GND and the cathode to the pin in both options?

Which one would be the safest option 1 or 2?

That would be parallel to the load/signal (permanently drawing 33mA) but you want the resistor in series (in line) with the load to limit the max. current through the load.
Typically a Zener Diode is used to “short circuit” any voltage spikes (that exceed its rated break-through voltage) towards GND (anode to GND, cathode to signal pin).

1 Like

Here is a schematic of 1 of 16 channel of 5V signal that is going off to a MCP23017. The sensor lead is connected to the jack J1 and the cables can be up to 10m and are not shielded but are not in a hostile environment. D1 is a 5.0V zener diode to ground and R1 is a Pull-down resistor. There was no 100 ohm resistor in series with the signal in this case because … I am not sure and an Electronic Engineer would.
image

The main point to be aware of is that it is very easy to blow a pin on an Argon (which are only 3V3 tolerant) so best to be careful. The 100 ohm resistor in series is a basic step to limit the current, but capping the voltage at 3V3 needs a zener diode really.

The reed switch no doubt will need debouncing.

Ok, so for Option 1, this is the final schema, where R2 replaces the internal pullup resistor of the Argon (pinMode(pinNumber, INPUT);) or of the MCP23017 (gpio.pinMode(myPin, INPUT);). Do I have understood correctly?

Sensor rain_schem

In addition when checking the value, the suggestion is to read it a couple of times in a short interval to confirm it’s really high or low (debouncing).

I’m sure that will work OK for you. If you search for software debounce algorithms you can some very simple and effective ones.

@armor and @ScruffR I really appreciate your help.

An additional question:

  • Having an analog sensor instead connected with 3 wires (GND, 3.3V and analog pin) over a long cable (2 to 5m) shield for most of the length: do I still need to put a resistor in serie, a zener to GND and connect a pull down (?) resistor as well or it’s a different thing for analog inputs?

You can’t put a 100ohm in series if you are reading a voltage since will drop voltage across the resistor according to the current V= IR and you don’t know the current?

You definitely need the zener and shielding grounded at the MCU end.

Better to read the voltage and digitise (ADC) it then send over an I2C bus.

:speak_no_evil: @armor ops… right it’s analog.

In case of the analog sensor: the zener diode would go from the analog pin to the GND with the anode to GND.
Use a shielded cable and connect the shield to GND as well.

I was actually considering using a I2C or SPI with an ADC and collect data from analog sensors but I was not sure about the maximum length of cable as there are different opinions on the web.

The schema I draw does not really work. I tried it on a breadboard with the zener diode it looks like always LOW to GND. What did I miss please?

I would suggest a google of zener diode or looking at the wikipedia entry. They don’t just work as a voltage clipper on their own - there has to be a specific minimum current and R set to the right value. Also the one used in the schematic I showed was a waveform clipper i.e. protects against +ve and -ve overvoltages.

@armor I have reversed it as follow:


It looks like working… hope it’s correct now…