Random Relay and PIR sensor

Hello. I am making a project which involves a motor. What I have it programmed to do now is when it is plugged in the motor turns on and off randomly. What I would like to do is attach a PIR sensor to this to be able to turn the program on. This is an art project that will be in a gallery so I don't constantly want the motor to be doing its random loop. I only want it to be doing its loop when people are in the gallery. The relay is a 5VDC SPDT. I am using an arduino UNO. and the PIR sensor is a sparkfun SE 10 I think. I have tried to just plug in the PIR to see what kind of readings I get but it says that motion is always detected even if there is none.ANy help with programming or advice would be helpful. I have very little experience with this programming, just one class a few semesters ago. Here is my code for my relay/motor.

int ledPin = 2; // Relay connected to digital pin 2

// The setup() method runs once, when the sketch starts

void setup() { // initialize the digital pin as an output: pinMode(ledPin, OUTPUT); }

// the loop() method runs over and over again, // as long as the Arduino has power

void loop() { digitalWrite(ledPin, HIGH); // set the LED on delay(random(1000)); // wait for a second digitalWrite(ledPin, LOW); // set the LED off delay(random(1000)); // wait for a second }

Hey Girl,

Take a look at this in the ArduinoInfo.Info WIKI: http://arduino-info.wikispaces.com/Brick-Pushbuttons&OtherSwitches

Scroll down to the Motion Detector. There is example code there also that you can cut/paste to try. If you can get that working, you can add the motor code in instead of the "Alarm"..

Maybe read the whole page, if you are just starting out with Arduino.

Also maybe see: http://arduino-info.wikispaces.com/YourDuinoStarter

Thank you!

How would I plug in my motor code for the alarm/buzzer?

The blurb on the Sparkfun site say the data pin is open collector that means you need to hold the line high and it pulses low when motion is detected.
Connect the power plus wire to 5v on arduino, ground wire to GND on arduino and signal wire to say pin 3 on arduino then try this sketch

int ledPin =  13;    // Built in LED
int signalPin = 3;  // PIR signal pin

void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(signalPin, INPUT_PULLUP);     // Make signal pin input and turn on internal pullup resistor
}

void loop() {
    if (digitalRead(signalPin) == LOW){
        digitalWrite(ledPin, HIGH);         // set the LED on
        delay(500);                         // wait for half a second
    }
    else {
        digitalWrite(ledPin, LOW);              // set the LED off
    }
}

If it’s all connected properly then hopefully the on board LED will light when motion is detected.

I don't need to use a 10K resistor on the power wire? Will not using a 10K resistor ruin my arduino?

greengirl55: I don't need to use a 10K resistor on the power wire? Will not using a 10K resistor ruin my arduino?

If your referring to the 10K resistor as seen in the bildr page http://bildr.org/2011/06/pir_arduino/ then no you don't need it. It is not used as a current limiting resistor but as a pullup resistor on the signal wire. The line pinMode(signalPin, INPUT_PULLUP); is setting the pin as input and turning on the built in pullup resistors on that pin.

Ok. Thanks.

I plugged in the code but this popped up

sketch_nov01a.cpp: In function 'void setup()': sketch_nov01a:5: error: 'INPUT_PULLUP' was not declared in this scope

All I want to do when my PIR detects motion, it will run the code that controls my relay so that the motor will turn on and off randomly during the time it detects motion. When there is no motion, nothing would be on. Isn't there a simpler way to do this? I am not very good at coding so the coding needs to be pretty straight forward and simple for me to understand. here is my current code for my motor/relay

int ledPin = 2; // Relay connected to digital pin 2

// The setup() method runs once, when the sketch starts

void setup() { // initialize the digital pin as an output: pinMode(ledPin, OUTPUT); }

// the loop() method runs over and over again, // as long as the Arduino has power

void loop() { digitalWrite(ledPin, HIGH); // set the motor on delay(random(1000)); // wait for a second digitalWrite(ledPin, LOW); // set the motor off delay(random(1000)); // wait for a second }

greengirl55: Ok. Thanks.

I plugged in the code but this popped up

sketch_nov01a.cpp: In function 'void setup()': sketch_nov01a:5: error: 'INPUT_PULLUP' was not declared in this scope

What version of the Arduino IDE are you using as it compiled fine on my v1.0.1?

Try doing the long way by replacing pinMode(signalPin, INPUT_PULLUP); // Make signal pin input and turn on internal pullup resistor with pinMode(signalPin, INPUT); // Make signal pin input pinMode(signalPin, HIGH); // Turn on internal pullup resistor

greengirl55: All I want to do when my PIR detects motion, it will run the code that controls my relay so that the motor will turn on and off randomly during the time it detects motion. When there is no motion, nothing would be on. Isn't there a simpler way to do this? I am not very good at coding so the coding needs to be pretty straight forward and simple for me to understand. here is my current code for my motor/relay

Once your sure the PIR is working okay then adding a few lines to your code should do the trick

int ledPin =  2;    // Relay connected to digital pin 2
int signalPin = 3;  // PIR signal pin

// The setup() method runs once, when the sketch starts

void setup() {
    // initialize the digital pin as an output:
    pinMode(ledPin, OUTPUT);
    pinMode(signalPin, INPUT);      // Make signal pin input
    pinMode(signalPin, HIGH);       // Turn on internal pullup resistor
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop() {
    if (digitalRead(signalPin) == LOW){
        digitalWrite(ledPin, HIGH);   // set the motor on
        delay(random(1000));          // wait for a second
        digitalWrite(ledPin, LOW);    // set the motor off
        delay(random(1000));          // wait for a second
    }
}

alright, I'll try that out. I have the arduino UNO not sure what version. It came in the sparkfun electronics inventors kit. Thanks for the help. Ill try it out tonight and let you know!

greengirl55: alright, I'll try that out. I have the arduino UNO not sure what version. It came in the sparkfun electronics inventors kit. Thanks for the help. Ill try it out tonight and let you know!

It's the Arduino software version that Riva is referring to, not the version of the Uno. If you got the software on a CD that came with the kit, or if you have an version older than 1.01 for any other reason, download the latest version of the Arduino software from the main site.

Oh ok. I downloaded it last Spring. It says 1.0

I tried the code with the PED pin and my PIR to test it and my light is continuosly on, even when I do not move. What am I doing wrong?

greengirl55: I tried the code with the PED pin and my PIR to test it and my light is continuosly on, even when I do not move. What am I doing wrong?

There are two little LED's, one is power that should be on all the time the device is powered and the other should turn on/off with movement. If you just connect power (5V & GND) and use a 10K resistor between the sensor pin and 5V then does the little LED on the PIR board go on/off with movement. If the onboard LED is not reacting then it's probably a PIR problem. This could be just the way it's positioned (picking up house lighting) or maybe it's faulty