Motion Detector

Hello, i want to switch a light on via motion detector. After a few seconds, the light should go out. If i push a button the light should be always on, motion detector does not work.
But it does not work, can someone help me?

SimpleTimer BewegungsmelderTIMER;


void setup()

Blynk.begin(auth, ssid, pass);


BewegungsmelderTIMER.setInterval(1000L, GARTENLICHT);



if (param.asInt() == 0)
digitalWrite(16,LOW); //GPIO16
digitalWrite(16,HIGH); // GPIO16




int GARTENLICHT = digitalRead(4); // GPIO4
delay (1000);




void loop()



i’m on phone right now, not sure what is the problem in your code. but consider these:

  • you should learn a bit more about coding principles and program flow
  • try to organise your code a little bit
  • you call the timer function every 1000 millis, but you put an 1000 millis delay in the function you call. this is not corect
  • for the sake of “readability” it is general rule in programming to use english variable names and comments. it will help others who try to debug your code

ah, and the most important above all, please format your code properly when posting!

1 Like

Okay thank you, i will do that.

Everythin is working fine. Motion detector switches on the ligth, after the defined time the light switches of. When i push my button on the phone, the GPIO switches, but can not switch on the light over my relay. i do not understand why.

How you the time Timer and delay should be?

I ended up doing that for the OP, a few hours later :wink:

@Tom when posting code, please follow these guidelines (look back at your post above to see how i made the changes).

@Gunner Can you help me with my problem?

Do i need a Timer or not?

My Motion Detector detects and switches on my relay all is okay.

But why can i not switch on my relay with the output GPIO16?

Thanks in advance.

@Tom are you aware that GPIO 16 is a special pin and do you specifically need to use this pin?

@Costas Why is GPIO16 special?

Do you think my program runs when i use an other GPIO?

0, 1, 2, 3, 15 and 16 are all special in some way and should be avoided unless you have run out of regular pins. There are very special pins at 6 to 11 but most boards don’t give you access to these.

Try one of your remaining pins and let us know how you get on.

Are you referring to ANY ESP8266 based board, and are you referring to the Arduino reference or the NodeMCU reference?

For example, my Wemos D1 minis don’t seem to have GPIO designated pin 16… unless it is referring to the silk screened D0 which on the chart I have claims is the Arduino number 16.

And if going off the Arduino numbering, then pins 0 and 2 (NodeMCU pins 3 & 4) are marked as PWM capable, but without any other sub use… so what is “special” about them?

As new ESP user, I can testify that it is all very confusing at a glance :confused:

GPIO numbers are the official Espressif pin allocations, ignoring the screen printing used by “chip resellers”.

It’s not an Arduino thing and yes your D0 is GPIO 16.

Pins 0, 2 and 15 are used as part of the ESP’s boot sequence.

We had a “huge” number of PCB’s produced with GPIO 16 used as a regular pin and then realised it was a bad idea as we were then not able to use deepSleep with this board.

In our defence it wasn’t a “deepSleep” project and we were running short of regular pins but we like our boards to be as flexible as possible.

I know it isn’t an Arduino thing so much as a “use this number in the Arduino IDE” thing… but I am starting to see why Arduino boards are still well liked… less “land mines” to step on :stuck_out_tongue: But I am loving the OTA programming!!

@Tom You didn’t mention what Dev Board you are using, but on the Wemos (and presumably NodeMCU) D1 Mini boards, their “official” relay shields use D1 (GPIO 5).

GPIO 4 and 5 are my “go to” pins as the most regular ESP pins available but I did notice the OP is already using those in his sketch.

1 Like

@Tom having looked through your posts again I think this is where we are:

  1. I was thinking you were referring to a physical button to turn on the light via the relay but it appears you are referring to a Blynk button. This button looks to be on V15.

  2. By default Blynk buttons are set as momentary and not switch mode. You appear to want switch mode so you should ensure the widget is correctly set.

  3. A lot of relays are active (ON) LOW so you might have to switch HIGH and LOW for V15.

@Costas Yes Costas, i trigger with an virtual button from my smartphone.

Perhaps the problem is, that i have wired the two outputs on the input on the relay, but i do not know how i can change my prgram in this way that it runs.

if i connect only the button to the relay it works. If i disconnect the button and connect my motion sensor it works. But if i connect both items to the input of the relay it works not.

but why?

Hello Together,

one question, is it possible to wire two GPIOs on one Relay INPUT?

Thanks in advance

Of course it is possible, just take the wires and join them ;)… The question is WHY? There is absolutely no benefit whatsoever, and a wasted GPIO. Also, unless you are very careful in the code and pinmode setup one signal could countermand the other.

Question, Programm ia running with all function after upload my code…when I then disconnect shirt time the power for my ESP, my ESP did not get online again…why?

When I upload again, all I is running again, short stop power for the ESP again, ESP will get not online…

@Tom your symptoms are what you would get if you were short of power for an ESP-01 or an ESP without a USB port and you were doing the flash with a USB2TTL adaptor.

Precisely which ESP do you have and how are you powering it.

If it’s not a power issue the next probably cause is bad code. So we would need to see the minimum sketch, not 5000 lines of code, that demonstrates the problem including Serial Monitor output, app & library versions and make & model of Smartphone (with OS if it’s not obvious from the make & model).

Hi Costas, I know the problem. I be connected with a relay, and when I disconnect the relay during switch off and switch on my nodemcu all ia running, but why?

Further when I be connected to the relay when I want upload my code, o get a failure. When I disconnect the connection to my relay I can upload…but why?