Pages: [1]   Go Down
Author Topic: Simple magnetic contact switch is not working right.  (Read 2138 times)
0 Members and 1 Guest are viewing this topic.
Oklahoma, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
  }
}
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1270
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
« Last Edit: September 02, 2012, 04:58:51 pm by marco_c » Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17292
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Oklahoma, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Excuse my ignorance, but how would I do that?
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17292
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Excuse my ignorance, but how would I do that?

Code:
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
}
Logged

Oklahoma, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17292
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17292
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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:
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);
}
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1270
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Pages: [1]   Go Up
Jump to: