Offline
Newbie
Karma: 0
Posts: 43
|
 |
« on: March 08, 2011, 02:41:38 pm » |
Hi
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.
thanks
Geoff
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Online
Brattain Member
Karma: 279
Posts: 15318
Measurement changes behavior
|
 |
« Reply #1 on: March 08, 2011, 03:05:14 pm » |
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
|
|
|
|
« Last Edit: March 08, 2011, 03:08:26 pm by retrolefty »
|
Logged
|
|
|
|
|
London, GB
Offline
Sr. Member
Karma: 7
Posts: 332
Nothing works.
|
 |
« Reply #2 on: March 08, 2011, 03:27:09 pm » |
Debounce?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 43
|
 |
« Reply #3 on: March 08, 2011, 04:37:00 pm » |
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; }
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Online
Brattain Member
Karma: 279
Posts: 15318
Measurement changes behavior
|
 |
« Reply #4 on: March 08, 2011, 05:05:36 pm » |
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. Lefty
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 43
|
 |
« Reply #5 on: March 08, 2011, 05:28:30 pm » |
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Online
Brattain Member
Karma: 279
Posts: 15318
Measurement changes behavior
|
 |
« Reply #6 on: March 08, 2011, 05:37:18 pm » |
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. Lefty
|
|
|
|
|
Logged
|
|
|
|
|
London, GB
Offline
Sr. Member
Karma: 7
Posts: 332
Nothing works.
|
 |
« Reply #7 on: March 08, 2011, 05:49:43 pm » |
You said in the title “basic reed switch”. That’s not what this is. This is a thing that contains a reed switch.
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 277
Posts: 25556
Solder is electric glue
|
 |
« Reply #8 on: March 08, 2011, 06:32:47 pm » |
Debounce? 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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 52
Arduino rocks
|
 |
« Reply #9 on: March 08, 2011, 06:56:41 pm » |
That is your basic pneumatic cylinder reed switch, usually they are connected to a 24vdc input on a PLC or similar.
John
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 52
Arduino rocks
|
 |
« Reply #10 on: March 08, 2011, 07:35:30 pm » |
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. Thanks John
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 43
|
 |
« Reply #11 on: March 09, 2011, 03:14:10 pm » |
Hello 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 } }
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 277
Posts: 25556
Solder is electric glue
|
 |
« Reply #12 on: March 10, 2011, 08:03:21 pm » |
Look up my sneak thief project (on my portable so I haven't got the link) . That has code that does exactly that.
|
|
|
|
|
Logged
|
|
|
|
|
|