Simple magnetic contact switch is not working right.

I am almost embarrassed to post this, but I am absolutely perplexed right now. I bought a magnetic contact switch from Adafruit (Magnetic contact switch (door sensor) : ID 375 : $3.95 : Adafruit Industries, Unique & fun DIY electronics and kits) and wrote the simplest sketch in the world just to see it working, and it's not working for me. The circuit is my 5v connected to the switch, connected to a 560 ohm resistor, then pin 12. The serial monitor just prints continuous "Close!" no matter how close together the sensors are. Here's my code, I don't see how something this simple could go wrong.

#include <Serial.h>

int sensorPin = 12;
int magnetSensor = digitalRead(sensorPin);

void setup(){
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);


}

void loop() {
  if(magnetSensor == HIGH){
    Serial.println("Close!");
  }
  
  if(magnetSensor == LOW){
    Serial.println("Far...");
    delay(200);
  }
}

Couple of potential problems.

Firstly, if this a simple reed switch then you need to have it arranged with a pull up or pull down resistor to give you a valid signal. Not sure if this is the case from your description.

Secondly, in your code you never actually reads the switch. Your code needs to look like this:

#include <Serial.h>

int sensorPin = 12;

void setup(){
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
}

void loop() {
  int magnetSensor = digitalRead(sensorPin);

  if(magnetSensor == HIGH){
    Serial.println("Close!");
  }
  
  if(magnetSensor == LOW){
    Serial.println("Far...");
    delay(200);
  }
}

And you can simplify the two if statements to an if..else.

The circuit is my 5v connected to the switch, connected to a 560 ohm resistor, then pin 12.

In which case you have wired it incorrectly and have a floating input condition when the switch is not conducting. You need to wire the resistor from the input pin to ground to establish a pull-down condition to ground when the switch is not conducting. A 560 ohm resistor in such service is kind of wasting current, should be 5k or 10k. Cheapest method is to use no external resistor, wire switch between ground and the input pin, then enable the internal pull-up for that pin number in software. Then reading a 0 means switch activated and reading a 1 means switch inactive.

LEfty

then enable the internal pull-up for that pin number in software

Excuse my ignorance, but how would I do that?

bigfish:

then enable the internal pull-up for that pin number in software

Excuse my ignorance, but how would I do that?

int switchInput = 10;                 // Magnetic switch contact wired from ground to pin 10

void setup()
{
  pinMode(switchInput, INPUT);      // sets the digital pin as a input pin
  digitialWrite(switchInput, HIGH);  // sets the pin's internal pull-up resistor
}

Here's my new code, now I'm getting a constant flow of HIGH reading, regardless of the position of the sensors.

#include <Serial.h>

int sensorPin = 12;
int magnetSensor = digitalRead(sensorPin);

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

void loop() {
  if(magnetSensor == HIGH){
    Serial.println("Close!");
  }
  else{
    Serial.println("Far...");
  }
  delay(200);
}

I'm going to do some research on a reed switch, because I don't know enough to troubleshoot properly.

Here's my new code, now I'm getting a constant flow of HIGH reading, regardless of the position of the sensors.

Did you remember to change the wiring on the switch contact from +5vdc to ground? That is require for this internal pull-up method to work for you. Switch contacts should be wired ground on one side and wired to input pin on the other side.

Lefty

bigfish:
Here's my new code, now I'm getting a constant flow of HIGH reading, regardless of the position of the sensors.

#include <Serial.h>

int sensorPin = 12;
int magnetSensor = digitalRead(sensorPin);

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

void loop() {
  if(magnetSensor == HIGH){
    Serial.println("Close!");
  }
  else{
    Serial.println("Far...");
  }
  delay(200);
}




I'm going to do some research on a reed switch, because I don't know enough to troubleshoot properly.

There is a flaw in your programming, you need to read the input pin for each pass through the main loop, not just were you define and assign it before the program even runs. So check the differences here:

int sensorPin = 12;
int magnetSensor;  

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

void loop() {
  magnetSensor = digitalRead(sensorPin);  // find out switch state by reading input pin
  if(magnetSensor == HIGH){
    Serial.println("Close!");
  }
  else{
    Serial.println("Far...");
  }
  delay(200);
}

Look at my post above. The digitalRead needs to be in the loop.