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);
}
}
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?
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
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!
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?
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........