Turn on the light with pir

Hi guys, for my project, I have 1 pir SR-501 and a classic relay connected to other relays.
I would like to light the lamp in the room when the pir identifies the movement and turns it off when the pir does not see the movements after x seconds. Specific to turn on the lamp I use the push mode due to the relay. Can you give me an idea on how to proceed? I thought of a time counter: millis ()
Thankyou all

What do you mean by you have a relay connected to other relays?

And are you wanting to control with Blynk also or is this just a timer project?

Yes, now to turn on my lamp i use a classic relay. I connect my phisical button another relay, i have create in my blynk project, a Virtual Button mode push, and works: 1 pression for turn on and another pression for turn off. Now i want connect the pir to ligh the lamp for lighting automatic. This is a simple to manage the pir but in switch mode:

void setup () {
rele = 5; //pin rele
pir = 6: // pin pir

pinMode(rele, OUTPUT);
pinMode(pir, INPUT);
timer.setInterval(1000L, light); }

void light() {
val = digitalRead(pir);
if (val == HIGH) {
   digitalWrite(rele, HIGH);    
else
   digitalWrite(rele, LOW);  
}

void loop() {
   Blynk.run();
   timer.run();
}

How do i fix if i want push mode? Can the pir timer conflict with the timer function?
I hope I was clear. TY

Unfortunately not - at least for me!

The term “classic relay” obviously means something to you, but not to me. Can you explain?

PIR devices vary enormously.
If you buy one that I’d designed for use with an alarm system then they usually have a built-in relay that can be wired as normally open or normally closed. This type often needs a 12v power supply. If you’re using this type then simply wire one side of the relay to one of the GPIO pins on your MCU and the other side to either 3.3v or GND, depending on how you want to monitor the GPIO pin.

If you’re using a more modern PIR module then it will probably have a logic pin that is pulled HIGH or LOW when the PIR triggers and this can be connected directly to a GPIO pin on your MCU.

If you want more advice then you’d need to share details of your PIR device, MCU and relay board, along with wiring schematics.

Pete.

You are right, by classic relay I mean a 220V relay finder, as i wrote on, the pir is HC-SR501, the mcu is arduino mega with lan shield.
Now the button to turn on the light is connected to another relay arduino as this

Schematic?

Pete.

Schema

Thank you for the patience, this is the schematic:
You see Phase in black and Neutral in Blue, step relay and push button, in parallel relay arduino. The pir and relay arduino are connected with 12v, and pins to arduino to control

I’m not really interested in your mains wiring, it’s the interconnections between PIR, Arduino, relay(s) etc that are needed.
Your current schematic is a very small image, when you post the one for the Arduino etc it needs to be a larger file.

Pete.


I have done everything possible, I hope it is clear, the second relay is connected to 220v

Okay, I now understand your wiring, I’m not 100% clear about what you want to achieve, but I think I have an idea.
Here are my comments…

First of all, I wouldn’t have a physical switch attached to the output terminals of the relay. By doing this, no matter what the PIR or anything else connected to the Arduino tries to do, it won’t be able to override the physical switch. In my opinion, it would be better to have the physical switch connected to one of the Arduino GPIO pins so that you can fully control the logic of the lighting.

Secondly, the PIR will only keep the light on for as long as it stays triggered. I realise that you can probably adjust the trigger time, but it might be better to do this within your Arduino code for more control. You’d need to use a timeout timer to achieve this. The 1000ms sample time of your timer might be a bit long. It could mean that the PIR isn’t responsive enough. There is actually a better way to do this, which is to use an interrupt. When you attach an interrupt to a pin it will automatically run a function within your code when the pin meets the interrupt criteria. Interrupt types are RISING, FALLING and CHANGE. If you used a RISING interrupt then the function would be triggered when the pin goes from LOW to HIGH.
Not all Arduino GPIO pins can be used as interrupts, but the Mega has more available than the Uno.

I think your original question was about how you configure the Switch widget in the Blynk app. The best way is to attach the Switch widget to a virtual pin. When the widget changes state it will automatically trigger a BLYNK_WRITE(VPin) function in your code, which can be used to change the state of your relay pin. This is a bit like the function of the interrupts I described earlier, but for virtual pins rather than hardware triggers.

If I’ve misunderstood your question then please provide more information.

Pete.

ok, ignoring the button for a moment, if I had a PIR connected to a step by step relay, what could be a sketch sketch? this comes to mind, but I know it’s wrong

void setup () {
rele = 5; //pin rele
pir = 6: // pin pir

pinMode(rele, OUTPUT);
pinMode(pir, INPUT);
timer.setInterval(1000L, light); }

void light() {
val = digitalRead(pir);

 if (val == HIGH) {
  digitalWrite(rele, HIGH);    
 delay(10);
 digitalWrite(rele, LOW); } // beacuse relay is step by step
else {
 digitalWrite(rele, HIGH); 
 delay(10);
 digitalWrite(rele, LOW);   // beacuse relay is step by step
 }
} 

void loop() {
Blynk.run();
timer.run();
}

but I have 2 counters: hardware (output timing as image) and “timer.setInterval(1000L, light);”

the timer in hardware is 5 sec to 5 min. set the hardware to 5 sec then playing with your sketch.

ok, but can i use delay() ?

No, use a Blynk Timer.

Pete.

1 Like