Operation of a Reed Switch

I am using a reed switch as a part of a tipping bucket rain gauge project, i.e. the bucket has a magnet that opens to switch as it passes registering a volume of rain fall.

I have connected one end of the reed switch to digital pin 13 and the other to ground using only hook-up wire. The reading are, shall we say temperamental, i.e. the switch normally works but some times stays open and sometimes switches for seemingly no good reason.

Am I missing some vital electronics? or is this simply a cheap / broken switch?

The switch is a Spark Fun COM-08642. I have included a 10ms delay in my sketch between loops.

thanks

Pull up resistor? Debounced?

Hi Mark

void setup(){

Serial.begin(9600); pinMode (REED, INPUT); digitalWrite(REED, HIGH);

}

I (think I) have activated the internal pull-up resistor in the set section of my script.

mattnewbury: void setup(){ Serial.begin(9600); pinMode (REED, INPUT); digitalWrite(REED, HIGH); }

I (think I) have activated the internal pull-up resistor in the set section of my script.

It should work, but this is preferred:

pinMode (REED, INPUT_PULLUP);

Try using a normal push button instead. If that works then the problem is the reed switch.

Thanks

Can you explain to me what the internal pull-up resistor is?

Matt

Just becasue an input isn't set high or set low, doesn't mean it will necessarily be low or high. The value is basically random, and depends on the phase of the moon or other random stuff 8).

In your case, you will be forcing it low (to ground) with your switch.... rest of the time? who knows? To make sure it's at a known value, which obviously can't be low since that what your switch will do, we force it the other way, to 5v.

You can do that either by adding a resistor outside the chip, but the makers, knowing that it's a very common thing to do, have added one inside. That as you saw, is enabled either as you did, or a newer facility added is to just use INPUT_PULLUP as a short-hand.

Bottom line.... you make the pin low with your switch; rest of the time it's forced high by the resistor, either internal or external.

Thank you for such a clear explanation.