Reed switches and Arduino

Hi,

I know that there are several topics around reed switches, but my questions are a little different. 1. What is the maximum number of reed switches I connect to the arduino simultaneously? Assuming that in order to trigger my action I need all reed switches to cause the pin to be high at the same time. I know that there is a maximum draw of 200mA from the pins collectively 2. Is there a way to regulate the current going to each pin so that if I reduce the current drawn by each pin I can increase the number of pins that I use simultaneously? (resistors in the circuit maybe)

Thank you in advance

    1. Use this in setup() to set them all as inputs with internal pullup enabled:
for (byte x = 0; x<20; x=x+1){
pinMode (pinX, INPUT_PULLUP); // pin s 0 to 19
}

and wire the switch to connect the pin to Gnd when closed.
2. Not a concern if using the internal pullups. 20 pins low will use <=3.3mA thru the internal pullups.

If you need all reed switches to be closed to bring the pin high, just put them all in series, and treat it like a single switch. You could have as many switches in series as you want. With long wires, use lower values for the pullup resistor to deal with potential interference.

Actually, you can have as many as you want in any configuration, including each switch on it's own pin (within the limits of the number of pins) - if the pin they're connected to is an input, as long as the voltage you're putting on the pin is between ground and Vcc (5v on most Arduino boards, 3.3v on the Due and some other similar new ones), there will be negligible current flowing to/from the pin. In technical terms, a pin set input is "high impedance". Voltages higher than Vcc or lower than ground should not be applied to an Arduino pin.

The only concerns are the current flowing through the pullup resistors (or pulldown ones, if for some weird reason you're doing it that way), which is typically small, and is usually not sourced from an Arduino pin anyway.

  1. You don't need to regulate the current going to/from a pin that is set as input (except as noted above), only current from a pin that is an output.

rcbart:
Hi,

I know that there are several topics around reed switches, but my questions are a little different.

  1. What is the maximum number of reed switches I connect to the arduino simultaneously? Assuming that in order to trigger my action I need all reed switches to cause the pin to be high at the same time. I know that there is a maximum draw of 200mA from the pins collectively
  2. Is there a way to regulate the current going to each pin so that if I reduce the current drawn by each pin I can increase the number of pins that I use simultaneously? (resistors in the circuit maybe)

Is this with the reed switches opened or closed?

If you want them to be all open to do something, then simply put them all in parallel, tie any one Arduino pin to VCC through a reasonable resistor (like 4.7K) and use the parallel switches to pull that node to ground.

Now, ANY ONE reed switch being closed pulls the pin low and only if ALL are open to you get a logic high.

If you want them to be all closed to initiate an action, wire them in SERIES and connect the series string, one end to ground and the other end to the Arduino node.

Now, ANY ONE switch being open lets the node go to logic high, they must be ALL CLOSED to pull the node to logic low.

No worries about “input current” needed.

(oops sorry DrAzzy already said the same thing).

So there is no need to actually set the pins to HIGH initially to detect the signal? Is it enough to set the pin to INPUT_PULLUP and once the switch is closed it will remain high as long as the switch is closed? My initial thought was to set all the required pins to HIGH and then only when the switch is closed I would get a HIGH on the pin.

rcbart:
So there is no need to actually set the pins to HIGH initially to detect the signal?
Is it enough to set the pin to INPUT_PULLUP and once the switch is closed it will remain high as long as the switch is closed?
My initial thought was to set all the required pins to HIGH and then only when the switch is closed I would get a HIGH on the pin.

Open and Closed don’t mean what it sounds like you think they mean. In electronics:

“Closed” = switch is connected
“Open” = switch not connected

Typically you have one end of a switch on ground, and the other end on a pin, with a pullup (internal or external). Then when the switch is open, the pin will read high, and when the switch is closed, it will be connected to ground, and will read low.

Do not set the pins as outputs if they are connected to a switch which can connect directly to Vcc or Ground, that can damage the pins.

Note that
pinMode(pin,INPUT_PULLUP);

does the same thing as:
pinMode(pin,INPUT);
digitalWrite(pin,HIGH);

And since the power on state for the pins is as input, if you never call pinMode() on the pin, digitalWrite()'ing it high will turn on the pullups, and digitalWrite()ing it low will turn them off.

rcbart: So there is no need to actually set the pins to HIGH initially to detect the signal? Is it enough to set the pin to INPUT_PULLUP and once the switch is closed it will remain high as long as the switch is closed? My initial thought was to set all the required pins to HIGH and then only when the switch is closed I would get a HIGH on the pin.

You have it backwards. What you should do is setup your input pin as "pinMode (n, INPUT_PULLUP)" and then use your reed switch(es) to pull the pin to ground as needed.

For long wire runs, where you have the possibility of picking up stray electrical noise, I suggest these 3 things:

  • Use a tightly twisted pair of wires to connect your reed switches to the Arduino.
  • Lower the input impedance of the Arduino (making it less susceptible to noise) by adding an external pullup resistor between 1K and 10K (4.7K is a good value) between the Arduino VCC and the digital input pin.
  • Place a 0.1 uF ceramic capacitor between the Arduino digital input pin and ground. This will tend to absorb (snub) very short duration noise pulses. Note that if your reed switches are going to switch VERY fast (like 100's of times per second or more), this capacitor may snub out the signal you are looking for (!) so then you will either need a smaller value capacitor, a lower value pullup resistor or leave the capacitor off entirely.

Hope this helps.

rcbart:
So there is no need to actually set the pins to HIGH initially to detect the signal?
Is it enough to set the pin to INPUT_PULLUP and once the switch is closed it will remain high as long as the switch is closed?
My initial thought was to set all the required pins to HIGH and then only when the switch is closed I would get a HIGH on the pin.

Here’s a little sketch I made to help you understand what a reed switch does and how they work.

(click pic for hi-res)
reed_switches.jpg

Krupski:
Place a 0.1 uF ceramic capacitor between the Arduino digital input pin and ground.

And a 100 Ohm resistor in series with the reed switch. This not only prevents contact erosion due to the capacitor (which might be considered desirable with some contacts, but not reed switches), but ads to the suppression of noise pick-up.

Paul__B: And a 100 Ohm resistor in series with the reed switch. This not only prevents contact erosion due to the capacitor (which might be considered desirable with some contacts, but not reed switches), but ads to the suppression of noise pick-up.

Yup, I have to agree with you on this one. A reed switch is hermetically sealed, so the contacts don't need a "cleaning current" to keep them working, and the way they work (the little leaf things bending towards each other) create a wiping action anyway.

As far as suppressing noise, you mean contact bounce, right? Because a series resistor won't stop long leads from picking up EMI from the surroundings (hence why I suggested twisted pair for long runs).

A series resistor and parallel capacitor on the Arduino end, however, would probably work to suppress noise, since you have an RC low pass filter right at the input........

Thanks everyone for all the help. I have my circuit working. I will definitely take into account the suggestions made about long run wires. You have really made me understand the ins and outs of what I am doing. Especially the arduino pin ins and outs.