I have just got a magnetic reed switch and want to connect it to my arduino but don't know where to start?
Cant find much on the web regarding connecting this up. I've read I need to include a pull down/up resistor
and believe the arduino has some built in accessed via the software. The switch has two wires brown & blue.
Should be simple. Just treat it like any push button switch.
pinMode(pin#, INPUT); // define which pin you are going to use for the reed switch
digitalWrite(pin#, HIGH); // turn on the internal pull-up resistor for this pin
Now wire the reed switch between the pin defined and a arduino ground pin.
Later when you do your digitalRead(pin#); statements, a LOW returned value means reed switch is on and a HIGH means it's off.
Thanks for your help, I used the "debounce" code as a start and have managed to connect the reed :), I used the serial monitor to display when the pin goes from 1 to 0 with the magnet attached and it works fine. I have a question about the Reed, when I connect it directly through the 5V and Ground on the Arduino and close the switch with a magnet I get a LED light in the switch. However when I hook the 5V to Pin 7 and ground to Arduino I can read the change but the LED doesn't light? Any Ideas?
Thanks
Geoff
// constants won't change. They're used here to
// set pin numbers:
const int ReedPin = 7; // the number of the Reed Switch pin
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int ReedState; // the current reading from the input pin
int lastReedState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(ReedPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the state of the switch into a local variable:
int Reed_Val = digitalRead(ReedPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (Reed_Val != lastReedState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
ReedState = Reed_Val;
}
digitalWrite(ReedPin, HIGH);
Serial.println(Reed_Val);
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastReedState = Reed_Val;
}
I have a question about the Reed, when I connect it directly through the 5V and Ground on the Arduino and close the switch with a magnet I get a LED light in the switch. However when I hook the 5V to Pin 7 and ground to Arduino I can read the change but the LED doesn't light? Any Ideas?
Simple reed switches do not have internal LEDs in them. How it should be wired depends on the specifics of your 'not simple' reed switch. If it has an internal led, does it also have a current limiting resistor to prevent damage to the led? Is the led in series with the contacts or parallel? Wiring straight across +5vdc and ground may not be safe for the switch or what you are powering it with.
Only a link to a datasheet for your specific 'not simple' reed switch can tell us how it should be operated and wired up.
Thanks for the link. however that is a pretty poor datasheet. It doesn't explain electrically how the led is wired in relationship with the contacts at all. I can offer no advice based on that information. Not your fault, just a poor datasheet.
I do a lot of projects and contact bounce is a problem I rarely encounter if you write the code correctly. My advice would be to never use the library.
It looks like that data sheet is designed for someone replacing the reed switch in a specific piece of equipment.
However as you have the switch you can test with a meter which connectors short in the presence of a magnetic field.
retrolefty:
Should be simple. Just treat it like any push button switch.
pinMode(pin#, INPUT); // define which pin you are going to use for the reed switch
digitalWrite(pin#, HIGH); // turn on the internal pull-up resistor for this pin
Now wire the reed switch between the pin defined and a arduino ground pin.
Later when you do your digitalRead(pin#); statements, a LOW returned value means reed switch is on and a HIGH means it's off.
Lefty
Lefty,
Thanks for this, you have also helped me with a switch input.
I will order some basic reed switches but for testing this will do I think, haven't blown anything yet :~. I have it now so when I remove the reed switch from the magnet a buzzer sounds two pitches and a LED flashes. The obvious flaw now in the program is when I put the magnet back the alarm stops. I want it to continue until I press a reset button. I figure that when the alarm sounds I read the input from the button and continue sounding the alarm until the button is pressed and the switch is closed. How would I incorporate that into the code?
// constants won't change. They're used here to set pin numbers:
const int ReedPin = 7; // the number of the Reed Switch pin
const int ledPin = 13; // the number of the LED pin
const int buzzPin = 11; // the number of the buzzer pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int ReedState; // the current reading from the input pin
int lastReedState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(ReedPin, INPUT); // sets the pin as input
pinMode(ledPin, OUTPUT); // sets the pin as output
pinMode(buzzPin, OUTPUT); // sets the pin as output
Serial.begin(9600); // Serial com port
}
void loop() {
// read the state of the switch into a local variable:
int Reed_Val = digitalRead(ReedPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (Reed_Val != lastReedState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
ReedState = Reed_Val;
}
digitalWrite(ReedPin, HIGH);
// Serial.println(Reed_Val); //uncomment this to check monitor to see if we are getting 0 or 1's
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastReedState = Reed_Val;
if (Reed_Val ==LOW) { //If we get 1's switch is closed
Serial.print("Everything is fine");
digitalWrite(ledPin, LOW);
delay(1000); //monitor every second
}
if (Reed_Val ==HIGH) { //If we get 0's switch is open so sound the buzzer
Serial.print("Call the Police!");
digitalWrite(ledPin, HIGH); //switch LED on
analogWrite(buzzPin,64); // sound buzzer
delay(500); //sound buzzer for 0.5 sec
analogWrite(buzzPin,128);
delay(500);
digitalWrite(buzzPin, LOW); //switch buzzer off//
digitalWrite(ledPin, LOW); //switch LED oFF
delay(500); //buzzer off for 0.5 sec
}
}