Motion detector mw2401tr

I am using an Arduino UNO and a MW2401TR. The output of the sensor is 3.3 V, connected to Pin2 when there is notion. I want to turn on a LED etc when there is motion, for a time set by me and off for a time set by me. If there are more triggers when they must ignored. I have tried using the fallowing code and can not make it work

  • LED attached from pin 13 to ground

  • pushbutton attached to pin 2 from +5V

  • 10K resistor attached to pin 2 from ground

  • Note: on most Arduinos there is already an LED on the board
    attached to pin 13.

created 2005
by DojoDave http://www.0j0.org
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Button
*/

// constants won’t change. They’re used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}

delay(1000);

}

Do you have a current limit resistor ( 330 to 680 Ohms) in series with the LED?

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
digitalWrite(ledPin,digitalRead(buttonPin));
delay(1000);
}

Your post is confusing because it includes notes about connecting a button to 5v, including a pulldown resistor, etc., which appear to have nothing to do with your sensor. You should clean that up. And get rid of the pulldown, if you have one (unless the datasheet for your particular unit says you need one).

Also, consider doing an analogRead of the sensor output in every iteration of loop() and printing the result to the monitor. That may be a better initial test than lighting an LED.

Thanks

I do have current limiting resistor. The sketch does work, but I need to block inputs from Pin 2 while the LED is on, so it does not stay on with multipool inputs.

Thanks for your comments. I am new at all this and I am not a programmer. The sketch that I tried came from the list of examples in IDE, called BUTTON so it had a lot of things that do not apply. Sorry I do not know how to get rid of the extra stuff. As far as the LED goes it is just an indicator. What I will be using it a transistor driving a relay. Not sure about using analogRead.

Thanks for your time.

Ok, forget about using analogRead. I suggested that because I thought you were having trouble with your sensor and that may have helped figure out what was going on. But it seems your main issue is programming, about which questions are more appropriately asked in the… Programming …section of the forum.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.