Motion activated lamp - help for a school project

Hey guys. I am working on making a motion activated lamp with the arduino uno, breadboard, a Sparkfun PIR motion sensor and a relay. I hooked up the motion sensor and the relay to the arduino (the power wire for both is connected to the breadboard).

So basicly, the moition sensor reacts to InfraRed Radiation and sends volatge to the arduino and then the arduino sends voltage to the relay (which is hooked up to my lamp) via a wire attached to pin 13.

I am not fully aware on how to know when the motion sensor works and how to program everything. I already used a sketch code but it makes the lamp constantly turn on and off (blinking) and it won't turn off completly. So I would really apreciate any advice, escpecially on the code.

Here is my code:

 int motion_1 = 2;
 int light_1 = 13;
 void setup(){
   pinMode (2,INPUT);
   digitalWrite(2, HIGH);
   
   pinMode (motion_1,INPUT);
   pinMode (light_1, OUTPUT);
 }

void loop (){
   digitalWrite (light_1,LOW);
   delay(1000); //this delay is to let the sensor settle down before taking a reading
   int sensor_1 = digitalRead(motion_1);
   if (sensor_1 == LOW){
     digitalWrite(light_1,HIGH);
     delay(500);
     digitalWrite(light_1,LOW);
     delay(500);

  }
 }
   delay(1000); //this delay is to let the sensor settle down before taking a reading

No, it isn't. It's crap. The sensor doesn't need "time to settle down".

   pinMode (2,INPUT);
   digitalWrite(2, HIGH);
   
   pinMode (motion_1,INPUT);

You gave the pin a name. Why are you then addressing it by number, too. What do you think is happening to the pullup resistor when this bit of code executes? Restructure it properly to be SURE.

Assuming you want to check each second for movement, and turn the lamp on if it is detected, i'd do something like this:

int motion_1 = 2;
 int light_1 = 13;
 void setup(){
   // use pin names, since you've set them
   pinMode (motion_1,INPUT); 
   
   pinMode (light_1, OUTPUT);
   digitalWrite(light_1, LOW); // turn the lamp off initially
 }

void loop (){
   int sensor_1 = digitalRead(motion_1); //read PIR
   if (sensor_1 == LOW){ // if movement is detected
     digitalWrite(light_1,HIGH); // signal the relay to open
   }
   else {
     digitalWrite(light_1,LOW); // dont signal the relay to open
  }
  delay(1000); //this makes more sense saying it's giving some time before turning the lamp off, like waiting for the person to leave, not allowing the sensor to settle :\
 }

Your if clause was turning the lamp briefly on when movement was present and turning it off immediately afterwards. Also, at beginning of each loop you turned the lamp off before a 1s delay, resulting in a total of 500ms of ON time and 1500ms of OFF time.

ladansedesdamnes , Thanks so much for the code. It is now working better than mine - the lamp is not blinking all the time.

However, it is not performing as it was supposed to. It turns on for a second (not sure if motion activated as it happens even when I am not moving), then turns off for a much longer time. Even when I wave my hand in front of it, it wouldn't turn the lamp on. I can't see a stable pattern.

Just wondering - did you enable the built-in pullup resistor in your code because my sensor requires it to output the signal.

Thanks again

It seems to me you have 2 choices:

  • let someone else write the code for you, in which case you will probably learn nothing
  • go back to basics and build your sketch up slowly so it eventually does everything you expect of it

Given that this is for a school project, I suggest the option 2 should be the one you choose.

I would start by removing everything to do with the relay and lamp and get the motion sensor working first - output sensor_1 to the Serial Monitor so you can see what sort of responses you are getting. Once this is working move onto the next step of doing something when the motion sensor is activated i.e. activate the relay.

Good luck

You might also want to have a look at the product page https://www.sparkfun.com/products/8630.

There are some interesting comments towards the bottom and this looks to be a good link http://itp.nyu.edu/physcomp/sensors/Reports/PIRMotionSensor.

Magicj: Given that this is for a school project, I suggest the option 2 should be the one you choose.

I would start by removing everything to do with the relay and lamp and get the motion sensor working first - output sensor_1 to the Serial Monitor so you can see what sort of responses you are getting. Once this is working move onto the next step of doing something when the motion sensor is activated i.e. activate the relay.

Good luck

Thanks Magicj. I will gladly learn more about the theory but now I am really in a rush to submit my project. I basically no nothing about coding ...

As to getting the motion sensor works - can you please give me some more details? What exactly do I need to do? What is a Serial Monitor? (sorry for the question ...)

Hi, can you post a CAD or picture of your circuit diagram please.

Have you connected the arduino output directly to the relay coil, or do you have mosfet or transistor doing it for you?

If you have the relay powered directly off the arduino output, you will probably be overloading the arduino output.

Do you have back emf diode on the relay coil?

Also keep the relay and output wiring well away from the motion sensor wiring.

Tom.... :)