253Views11Replies

Author Options:

Help with Arduino? Answered

Hey everyone,
         I'm doing a project with the arduino and i just want an led to light if a PIR senses motion.
I am using this sensor:     http://www.sparkfun.com/commerce/product_info.php?products_id=8630
I know I have everything hooked up right, my code is just wrong.(Yes, MOTOR is used for the LED) Can someone tell me what to do? Here is the code:

//Motion Detecting Box-Second Attempt

#define MOTOR 13 //Motor connected to pin 13

#define PIR 2   //Motion Sensor is connected to pin 2

void setup()

{
  pinMode(MOTOR, OUTPUT); //sets the digital pin as output
 
  pinMode(PIR, INPUT);    //sets the digital pin as input
 
}

void loop() {
 digitalRead(PIR); //checks pin 2
 
  if (PIR==LOW) {
 
  digitalWrite(MOTOR, HIGH); //turn on Motor
}
 else {
  digitalWrite(MOTOR, LOW);
}
}

11 Replies

user
Willard2.0Best Answer (author)2010-10-24


You need to store your read value from the digitalRead(PIR) statement in a variable. All it's doing right now is reading the sensor and not doing anything with the value.  Define a variable, like pirState and include it in your void loop() like this:

void loop() {
pirState = digitalRead(PIR); //checks pin 2

if (pirState==LOW) {
digitalWrite(MOTOR, HIGH); //turn on Motor
}
else {
digitalWrite(MOTOR, LOW);
}
}

Select as Best AnswerUndo Best Answer

user
TerryKing (author)2011-06-01

Here's info on this with example Arduino code:
http://arduino-info.wikispaces.com/Brick-Pushbuttons%26OtherSwitches#Motion

Select as Best AnswerUndo Best Answer

user
Adum24 (author)2010-10-25

So, in the top where I have #define I also need to include PIRState? And then just add in your code? How exactly do I do that?

Select as Best AnswerUndo Best Answer

user
Adum24 (author)Adum242010-10-25

Never mind! I got it! Thnx a lot! That really helped! Man, im excited! Thank you so much!

Select as Best AnswerUndo Best Answer

user
Willard2.0 (author)Adum242010-10-25

Glad I could help. Thanks for the patch.

Select as Best AnswerUndo Best Answer

user
Adum24 (author)Willard2.02010-11-01

Hey, another question, sry. UM, are my motion sensors like really sensitive? because nothings moving and it goes off randomly. Can u tell me wut to do?

Select as Best AnswerUndo Best Answer

user
Willard2.0 (author)Adum242010-11-01

I don't have experience motion sensors in particular, but I know that some sensors can only be checked so many times a second.  Right now I'd say you're checking it more than 1000 times a second.  Lets try lowering that number to something like 10-20 times per second.

Also, I just read the description and you have to let the sensor stay still for 2 seconds for it to get a 'snapshot' of the room and then start reading for movement.

My changes are in bold.  Try it out and see if it works.

//Motion Detecting Box-Second Attempt

#define MOTOR 13 //Motor connected to pin 13

#define PIR 2   //Motion Sensor is connected to pin 2

int pirState = 0;  //initiates the variable for the PIR sensor

void setup()

{

  pinMode(MOTOR, OUTPUT); //sets the digital pin as output
 
  pinMode(PIR, INPUT);    //sets the digital pin as input

  digitalWrite(MOTOR, HIGH); //turns on LED until initial PIR sensor gets snapshot of the room

  delay(2000);  //let the PIR sensor stay still for 2 seconds

  digitalWrite(MOTOR, LOW);  // turns the LED off
 
  delay(100);  //wait 1/10second before taking readings
}

void loop() {
pirState = digitalRead(PIR); //checks pin 2 and saves value in pirState

if (pirState==LOW) {
digitalWrite(MOTOR, HIGH); //turn on Motor
}
else {
digitalWrite(MOTOR, LOW);
}
delay(50);  //waits 1/20second before taking next reading
}

Select as Best AnswerUndo Best Answer

user
Adum24 (author)Willard2.02010-11-03

All right, this works for an LED, but the motor just has a field day, spinning super fast and not stopping. As soon as I remove connections to the motor it goes back to normal.(I can tell by the built in LED on the arduino.

Select as Best AnswerUndo Best Answer

user
Adum24 (author)Willard2.02010-11-03

K, thnx. I'll try it and get back to you!

Select as Best AnswerUndo Best Answer

user
Willard2.0 (author)Adum242010-10-25

Yup, just add in the line: int pirState = 0;  
That initiates the variable that is used in my code. You can't use #define for any value that will change as the program runs.
So your entire code would look something like this:

//Motion Detecting Box-Second Attempt

#define MOTOR 13 //Motor connected to pin 13

#define PIR 2   //Motion Sensor is connected to pin 2

int pirState = 0;  //initiates the variable for the PIR sensor

void setup()

{
  pinMode(MOTOR, OUTPUT); //sets the digital pin as output
 
  pinMode(PIR, INPUT);    //sets the digital pin as input
 
}

void loop() {
pirState = digitalRead(PIR); //checks pin 2 and saves value in pirState

if (pirState==LOW) {
digitalWrite(MOTOR, HIGH); //turn on Motor
}
else {
digitalWrite(MOTOR, LOW);
}
}

Select as Best AnswerUndo Best Answer