Using a reed switch with Arduino

Hi, I'm a complete noob with electronics, so sorry if this is a stupid question...

I've just got myself an Arduino Duemilanove to use for a university project I am working on, I want to use a series of reed switches to send different messages to a computer via the Arduino.

At the moment I'm just trying to get to grips with it, and all I'm trying to do is basically the "turn on LED when button is pressed" example in Arduino, but using a reed switch instead of a button, but it doesn't work, does anyone have any pointers as to why this might be? I know its probably really simple, and I'm just being stupid, but any help would be greatly appreciated.

I am actually copying the example from the "Getting started with Arduino" book by Massimo Banzi, which is pretty much the same as the example in the sketchbook of the Arduino IDE (and using a 10k resistor.)

The trick of getting an answer is to give us something to go on. So post the code and / or post the schematic and we can review it. As it is all I can say is there is probably something wrong with the software or the hardware.

Start with the blinking LED to see if the basic hardware is working.

If you can ignore ESRs masturbatory "hacker hacker hacker" rants this is actually worth reading: "How To Ask Questions The Smart Way" How To Ask Questions The Smart Way

Ok, I have done the blinking LED, and everything seemed to be fine, I'm using an LED attached to the digital input 13 (which I have tried the blinking example with and was fine also.)

Here is the code, I'm taking power from the Arduino (5v) and using the 10k resistor as a pull-down. Nothing happens when the reed switch is closed at the moment.

//Turn on LED while reed switch is closed

#define LED 13 //pin for the LED
#define SWITCH 7 //input for REED SWITCH

int val = 0; //used to store input value

void setup() {
pinMode(LED, OUTPUT); //tell arduino LED is an output
pinMode(SWITCH, INPUT); //SWITCH is input
}

void loop(){
val=digitalRead(SWITCH); //read input value and store it

//check whether input is HIGH (switch closed)
if (val==HIGH) {
digitalWrite(LED, HIGH); //turn LED on
} else{
digitalWrite(LED, LOW);
}
}

Jack, if you temporarly connect a wire across the reed switch can you control the led?

If so then the reed switch is not being closed by the magnet. try moving the magnet around different parts of the reed switch.

Are you using a pull up or pull down resistor with the switch ?

I'm using a pull-down resistor.

mem I don't understand exactly what you mean by "connect a wire accross the reed switch" and I don't want to damage any of my parts, but I have tried moving the magnet around the reed switch and still got nothing.

The most common reed switch has two contacts which close a circuit when near a magnet. I am suggesting you verify the wiring by simulating closing the circuit using a wire to connect the two contacts together.

Do you have a link to some more info on the switch you are using?

Ok, sorted the problem. I had been an idiot and connected the circuit wrong. Thanks for the help.

If you going to have a problem, the simple ones to fix are the best problems to have :wink:

Good to hear you have it sorted.

If you don't mind could you say how you connected it up wrong. I am sure you won't be the last and it is useful to know what mistakes beginners can make, in order to correct others.
It's many years since some of us were beginners :frowning:

I agree with Grumpy_Mike, it's good to know what was wrong, because it can help others and help to improve the documentation. We don't need an "air crash investigation", just a brief outline of the trouble!

Hello, I am doing the exact same thing using a reed switch to blink a LED. I am using an inspeed vortex wind sensor

I have the same script as above, with the reed switch in the wind sensor acting as my switch.

However, the light is just always on! it wont blink off when i spin the sensor.

any advice?

Hello, I am doing the exact same thing using a reed switch to blink a LED. I am using an inspeed vortex wind sensor

I have the same script as above, with the reed switch in the wind sensor acting as my switch.

However, the light is just always on! it wont blink off when i spin the sensor.

any advice?

Hello,
I am trying to use Arduino to log the frequency of pulses from a reed switch. The switch is inside a vortex wind sensor that actuates as the sensor spins --http://www.inspeed.com/anemometers/Vortex_Wind_Sensor.asp

here is my code:
#define LED 13 //pin for the LED indicator (green)
#define PULSESWITCH 1 //pin for the switch

int val=0; //sets variable "val" initially at zero "val" stores the state of the input pin
unsigned long time;

void setup(){
pinMode(LED,OUTPUT); //the LED is an output (light)
pinMode(PULSESWITCH,INPUT); //the switch is an input (current)
}

void loop(){
val=digitalRead(PULSESWITCH); //senses what the switch is doing
//check if the input is HIGH (switch on)
if (val==HIGH){
digitalWrite(LED, HIGH); //turns LED on if switch is on

}else{
digitalWrite(LED,LOW); //otherwise it turns the LED off

}
time=pulseIn(PULSESWITCH,HIGH,1);//takes the time between pulses at a sample rate of 1 sample/ms
Serial.print(time); // prints the time since program started
delay(1); // delay between prints (1 ms)
}

Here is how everything is laid out:

Pretty simple, but the LED is always on no matter what. I suspect a faulty reed switch since at one point i had it working.

Also, I am using the pulsein command to gather data of the pulses, but how can I export this into a readable format to be used in MATLAB?

Thank you

#define PULSESWITCH 1 //pin for the switch

Your diagram shows it wired to pin seven (7).

Oh, my mistake on the script. Yes I have defined it to pin 7.

But how do I verify if its even reading anything? where does the data get stored?

Were I in your shoes, I'd try something very simple...

#define LED 13 //pin for the LED indicator (green)
#define PULSESWITCH 7 //pin for the switch

int val=0;
           
void setup()
{
  pinMode(LED,OUTPUT);
  pinMode(PULSESWITCH,INPUT);
}

void loop()
{
  val=digitalRead(PULSESWITCH);

  if (val==HIGH)
  {
    digitalWrite(LED, HIGH);
  }
}

If the LED lights, you know the digital input went high at some point. Then I'd do this...

#define LED 13 //pin for the LED indicator (green)
#define PULSESWITCH 7 //pin for the switch

int val=0;
           
void setup()
{
  pinMode(LED,OUTPUT);
  pinMode(PULSESWITCH,INPUT);
}

void loop()
{
  val=digitalRead(PULSESWITCH);

  if (val==LOW)
  {
    digitalWrite(LED, HIGH);
  }
}

If the LED lights, you know the digital input went low at some point. If the first two Sketches work, it is very likely the hardware is correct and I'd move on to the software.

A DMM makes life a lot simpler, just put it on continuity mode across the sensor and spin it. Immediate verification one way or the other without wondering if you got the sketch and the wiring right........

I just want to do exactly what this guy did :

I tried getting his sketch but he didn't get back to me .
-how does he export the data like that?