I have a window 'contact' used for Home security for alarming doors and windows when they are opened.
Contacts are closed when magnet and sensor are together.
I'm not exactly sure how to hook it up to my Arduino.
I want to know when the magnet is in front of the sensor - which would mean when the Sesor is "HIGH" right?
I have one side of the sensor hooked to 5volt and the other side to analog pin 0.
declare the pin as analog 0;
int magsensorPin = 0; // magnetic Sensor input pin
In the Progam:
int magsensorValue = analogRead(magsensorPin);
Serial.print("magsen:");
Serial.println(magsensorValue);
Serial.println("--------------");
When I hold the magnet in front of the sensor it reads 1020, of 1023 on the serial output.
When the magnet is not there it's all over the board, but usually less than 1000.
I thought I could just read it as 'HIGH' if the value was >1000.... but it doesn't appear to be reliable.
if (magsensorValue >1000)
{
digitalWrite(ledPin, LOW);
magcounter += 1;
Serial.print("Magnet Sensed!");
Serial.println(magcounter);
}
else
{
digitalWrite(ledPin, HIGH);
NOmagcounter += 1;
Serial.print("No Magnet: ");
Serial.println(magcounter);
}
To be perfectly honest I don't know 'EXACTLY' what this is? I assumed it was a hall-effect sensor, I've just been bumbling around to see how to use it correctly?
Window Magnet Contact - (use with Arduino)
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- kramerica
- Posts: 36
- Joined: Thu Oct 08, 2009 6:43 pm
Window Magnet Contact - (use with Arduino)
- Attachments
-
- Magnet window contact
- magnet contact.jpg (31.04 KiB) Viewed 12783 times
- adafruit_support_bill
- Posts: 86938
- Joined: Sat Feb 07, 2009 10:11 am
Re: Window Magnet Contact - (use with Arduino)
It could be a hall-effect sensor, but it is probably a reed-relay. In any case, it sounds like you need a pulldown resistor on the digital input. See lesson 5 in the Adafruit Arduino tutorials. http://www.ladyada.net/learn/arduino/lesson5.html
- Franklin97355
- Posts: 23605
- Joined: Mon Apr 21, 2008 2:33 pm
Re: Window Magnet Contact - (use with Arduino)
You are trying to get a digital response (HIGH) from an analog read. Try a digital pin and digital read. Also as mentioned a 10k resistor to ground will help.
- kramerica
- Posts: 36
- Joined: Thu Oct 08, 2009 6:43 pm
Re: Window Magnet Contact - (use with Arduino)
I put the resistor on - and tried the digital pin 7.
One end of the sensor on 5v and the other with a 10k resistor on Digital pin 7.
NO MAGNETS are near the sensor - it should not be reading anything...
Code debug:
Mag sensor value:0 No Magnet keep waiting.... Counter: 37
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 37
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 37
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:38
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 38
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:39
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:40
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 40
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 40
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 40
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:41
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 41
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:42
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:43
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 43
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:44
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:45
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:46
__________________________________________________________
I changed the pin from Digital 7 back to Analog 0 -- and also edited the code to accomodate an analog read.
The sensor is reading something.. it's all over the place?
__________________________________________________________
Mag sensor value:934 No Magnet keep waiting.... Counter: 15
__________________________________________________________
Mag sensor value:138 No Magnet keep waiting.... Counter: 15
__________________________________________________________
Mag sensor value:1023 --------------------Magnet Sensed! Counter:16
__________________________________________________________
Mag sensor value:993 No Magnet keep waiting.... Counter: 16
__________________________________________________________
Mag sensor value:176 No Magnet keep waiting.... Counter: 16
__________________________________________________________
Mag sensor value:1023 --------------------Magnet Sensed! Counter:17
__________________________________________________________
Mag sensor value:978 No Magnet keep waiting.... Counter: 17
__________________________________________________________
Mag sensor value:162 No Magnet keep waiting.... Counter: 17
__________________________________________________________
Mag sensor value:1023 --------------------Magnet Sensed! Counter:18
__________________________________________________________
Mag sensor value:971 No Magnet keep waiting.... Counter: 18
__________________________________________________________
Mag sensor value:143 No Magnet keep waiting.... Counter: 18
__________________________________________________________
Mag sensor value:1023 --------------------Magnet Sensed! Counter:19
__________________________________________________________
Mag sensor value:994 No Magnet keep waiting.... Counter: 19
__________________________________________________________
Mag sensor value:206 No Magnet keep waiting.... Counter: 19
__________________________________________________________
Mag sensor value:1023 --------------------Magnet Sensed! Counter:20
__________________________________________________________
Mag sensor value:1023 --------------------Magnet Sensed! Counter:21
__________________________________________________________
One end of the sensor on 5v and the other with a 10k resistor on Digital pin 7.
NO MAGNETS are near the sensor - it should not be reading anything...
Code debug:
Mag sensor value:0 No Magnet keep waiting.... Counter: 37
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 37
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 37
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:38
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 38
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:39
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:40
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 40
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 40
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 40
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:41
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 41
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:42
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:43
__________________________________________________________
Mag sensor value:0 No Magnet keep waiting.... Counter: 43
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:44
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:45
__________________________________________________________
Mag sensor value:1 --------------------Magnet Sensed! Counter:46
__________________________________________________________
Code: Select all
/*
Goal:
Take a 2 wire window contact alarm - with a magnet, and sense when the magnet is present.
The circuit:
A single window Contact alarm with 2 wires (both wires appear to be the same? - no stripes)
1 wire is connected to 5volt pin on the arduino
the other wire is connected to the ANALOG 0 pin with a 10K resister=(brown/black/orange/gold)
analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.
Note: When the magnet is in front of the sensor and I connect my voltmeter to the ground and Analog 0 I get 5 Volts.
*/
int magsensorPin = 7; // Magnetic Sensor input pin
int ledgreenPin = 13; // select the pin for the Green LED
int magsensorValue = 0; // variable to store the value coming from the sensor
int magcounter =0; // Number of times a magnet was sensed
void setup()
{
Serial.begin(9600);
pinMode(ledgreenPin, OUTPUT); // declare LED as output GREEN
}
void loop()
{
////////////////////////////////////////////
// READ the SENSOR /
////////////////////////////////////////////
Serial.println("__________________________________________________________");
magsensorValue = digitalRead(magsensorPin);
Serial.print(" Mag sensor value:");
Serial.print(magsensorValue);
delay(1000);
////////////////////////////////////////////
// INCREMENT the magsensor counter. /
////////////////////////////////////////////
if (magsensorValue==HIGH)
{
digitalWrite(ledgreenPin, LOW);
magcounter += 1;
Serial.print(" --------------------Magnet Sensed! Counter:");
Serial.println(magcounter);
}
else
{
digitalWrite(ledgreenPin, HIGH);
Serial.print(" No Magnet keep waiting.... Counter: ");
Serial.println(magcounter);
}
}
I changed the pin from Digital 7 back to Analog 0 -- and also edited the code to accomodate an analog read.
The sensor is reading something.. it's all over the place?
__________________________________________________________
Mag sensor value:934 No Magnet keep waiting.... Counter: 15
__________________________________________________________
Mag sensor value:138 No Magnet keep waiting.... Counter: 15
__________________________________________________________
Mag sensor value:1023 --------------------Magnet Sensed! Counter:16
__________________________________________________________
Mag sensor value:993 No Magnet keep waiting.... Counter: 16
__________________________________________________________
Mag sensor value:176 No Magnet keep waiting.... Counter: 16
__________________________________________________________
Mag sensor value:1023 --------------------Magnet Sensed! Counter:17
__________________________________________________________
Mag sensor value:978 No Magnet keep waiting.... Counter: 17
__________________________________________________________
Mag sensor value:162 No Magnet keep waiting.... Counter: 17
__________________________________________________________
Mag sensor value:1023 --------------------Magnet Sensed! Counter:18
__________________________________________________________
Mag sensor value:971 No Magnet keep waiting.... Counter: 18
__________________________________________________________
Mag sensor value:143 No Magnet keep waiting.... Counter: 18
__________________________________________________________
Mag sensor value:1023 --------------------Magnet Sensed! Counter:19
__________________________________________________________
Mag sensor value:994 No Magnet keep waiting.... Counter: 19
__________________________________________________________
Mag sensor value:206 No Magnet keep waiting.... Counter: 19
__________________________________________________________
Mag sensor value:1023 --------------------Magnet Sensed! Counter:20
__________________________________________________________
Mag sensor value:1023 --------------------Magnet Sensed! Counter:21
__________________________________________________________
Code: Select all
int magsensorPin = 0; // Magnetic Sensor input pin
int ledgreenPin = 13; // select the pin for the Green LED
int magsensorValue = 0; // variable to store the value coming from the sensor
int magcounter =0; // Number of times a magnet was sensed
void setup()
{
Serial.begin(9600);
pinMode(ledgreenPin, OUTPUT); // declare LED as output GREEN
}
void loop()
{
////////////////////////////////////////////
// READ the SENSOR /
////////////////////////////////////////////
Serial.println("__________________________________________________________");
magsensorValue = analogRead(magsensorPin);
Serial.print(" Mag sensor value:");
Serial.print(magsensorValue);
delay(1000);
////////////////////////////////////////////
// INCREMENT the magsensor counter. /
////////////////////////////////////////////
if (magsensorValue>1000)
{
digitalWrite(ledgreenPin, LOW);
magcounter += 1;
Serial.print(" --------------------Magnet Sensed! Counter:");
Serial.println(magcounter);
}
else
{
digitalWrite(ledgreenPin, HIGH);
Serial.print(" No Magnet keep waiting.... Counter: ");
Serial.println(magcounter);
}
- adafruit_support_bill
- Posts: 86938
- Joined: Sat Feb 07, 2009 10:11 am
Re: Window Magnet Contact - (use with Arduino)
How do you have the resistor connected? Did you follow the tutorial? It should be between pin 7 and ground.One end of the sensor on 5v and the other with a 10k resistor on Digital pin 7.
- kramerica
- Posts: 36
- Joined: Thu Oct 08, 2009 6:43 pm
Re: Window Magnet Contact - (use with Arduino)
I was confused by only having 2 wires - after reviewing the tutorial, I believe that I have it hooked up correctly.
I am now seeing results! -- Only when the magnet is there . Thank you VERY much for your post, I really appreciate it.
I am now seeing results! -- Only when the magnet is there . Thank you VERY much for your post, I really appreciate it.
- Attachments
-
- Windows Contact REED switch_bb.jpg (34.46 KiB) Viewed 12736 times
Please be positive and constructive with your questions and comments.