/* Rui Santos Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-rcwl-0516-arduino/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. */ // If you're using the built-in LED for testing, remember that it works with inverted logic // (HIGH=LOW, and LOW=HIGH) int led = 2; // the pin that the LED is attached to int sensor = 12; // the pin that the sensor is attached to int state = LOW; // by default, no motion detected int val = 0; // variable to store the sensor status (value) void setup() { pinMode(led, OUTPUT); // initalize LED as an output pinMode(sensor, INPUT); // initialize sensor as an input Serial.begin(115200); // initialize serial } void loop(){ val = digitalRead(sensor); // read sensor value if (val == HIGH) { // check if the sensor is HIGH digitalWrite(led, HIGH); // turn LED ON if (state == LOW) { Serial.println("Motion detected!"); state = HIGH; // update variable state to HIGH } } else { digitalWrite(led, LOW); // turn LED OFF if (state == HIGH){ Serial.println("Motion stopped!"); state = LOW; // update variable state to LOW } } }