Go Down

Topic: Simple magnetic contact switch is not working right. (Read 2595 times) previous topic - next topic

bigfish

I am almost embarrassed to post this, but I am absolutely perplexed right now. I bought a magnetic contact switch from Adafruit (http://www.adafruit.com/products/375) 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.
Code: [Select]
#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);
  }
}

marco_c

#1
Sep 02, 2012, 12:25 am Last Edit: Sep 02, 2012, 11:58 pm by marco_c Reason: 1
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:
Code: [Select]
#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.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

retrolefty

Quote
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

bigfish

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


Excuse my ignorance, but how would I do that?

retrolefty


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


Excuse my ignorance, but how would I do that?


Code: [Select]
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
}

bigfish

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

Code: [Select]

#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.

retrolefty

Quote
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

retrolefty


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

Code: [Select]

#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:

Code: [Select]
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);
}

marco_c

Look at my post above. The digitalRead needs to be in the loop.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Go Up