MAGNETIC DOOR SWITCH - Problem

Hey,

I really need your help.

I want to use the MAGNETIC DOOR SWITCH.

I’ve connected all the items but there is a problem - When I attach the magnet it makes the led turn on but when I disconnect it , it takes 5-10 seconds to turn off the led.

What can be the problem?

This is the code:

int led = 8;
const int switchPin = 7;
// the setup routine runs once when you press reset:
void setup() { **
** // initialize the digital pin as an output.

** pinMode(led, OUTPUT);**
** pinMode(switchPin, INPUT);**
}
// the loop routine runs over and over again forever:
void loop() {
** if(digitalRead(switchPin) == LOW){**
** digitalWrite(led, LOW);**
** }**

** else{**

** digitalWrite(led, HIGH);**
}
}

More precisely its turn off after 16 secons.. ):

Try changing the pinMode on the input to INPUT_PULLUP.
Also.

digitalWrite(led, digitalRead(switchPin));

Please remember to use code tags when posting code.

Please use code tags (read the forum rules).
Did you use a pull up (or pull down) resistor on the switch?

Try this sketch.
It uses the internal pull up on the switch pin, so you don't have to use an external pull up resistor.
You might have to use a 100n capacitor from switchpin to ground if your wiring is long (hash pickup).
Leo..

const byte switchPin = 7; // switch between pin7 and ground
const byte ledPin = 8;

void setup() {
  pinMode(switchPin, INPUT_PULLUP); // enable the internal pull up resistor
  pinMode(ledPin, OUTPUT);
}

void loop() {
  if (digitalRead(switchPin)) digitalWrite(ledPin, LOW);
  else digitalWrite(ledPin, HIGH);
  // swap HIGH and LOW if you want the LED to behave the opposite way
}

This is what I’ve done.

Should I change anything?

TNX!!

I see you have wired your doorswitch between pin and +5volt.
Then you MUST use a (10k) pull down resistor between pin and ground.
Otherwise the pin will be "floatiing" (undefined state) when the switch is open (no magnet near).

Use the sketch I posted, and wire the switch between pin and ground (read the comments).

Or, as AWOL already explained, you could replace everything in void loop() with his line.
That line simply writes the state of the switchpin to the ledpin.
Leo..

thanks to you all! but it's still not working..

this is the code , and I did not changed anything in the wires.

int led = 8;
const int switchPin = 7;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(switchPin, INPUT);

}

// the loop routine runs over and over again forever:
void loop() {

digitalWrite(led, digitalRead(switchPin));
}

If that's really all you want to do with that Arduino, you can replace it by a single transistor...

Anyway: if you don't change your wiring IT WILL NOT WORK. That advice was not given to you just because. Ignoring these things isn't helping - if you don't understand something don't ignore it, but try to figure out why those suggestions are given or ask about it.

Easiest (and best) way to wire a switch:
Connect it between your input pin and GND, then enable the internal pull-up resistor (using the pinMode(pin, INPUT_PULLUP) command). Don't let your pin float, or its state is undetermined and your LED may spontaneously switch on when the door is closed.

Then the switch becomes active LOW so you have to change your LED code to:

digitalWrite(led, !digitalRead(switchPin));

That exclamation mark is a NOT, meaning the signal becomes reversed.

I’ve tried to (if I undesrstand correct) but its still not working.

this is the code , and the picture.

int led = 8;
const int switchPin = 7;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(switchPin, INPUT);

}

// the loop routine runs over and over again forever:
void loop() {

digitalWrite(led, digitalRead(switchPin));
}

[img]file:///C:/Users/Anael/Downloads/WhatsApp_Image_2017-11-20_at_18_51_05.jpg[/img]

I can't see the resistor connected to pin 7.
I can see that you're not using the built-in pull-up resistor.

At least the switch seems to be connected between GND and the pin, that's progress.

Now enable the internal pull up resistor and reverse the logic (as per my reply #7) as your switch is going to be active LOW, and it should work.