ESP8266 NodeMCU with PIR Motion Sensor
To use a PIR (Passive Infrared) Motion Sensor with an ESP8266 NodeMCU and control an LED based on motion detection, follow these steps:
Components Needed:
ESP8266 NodeMCU
PIR Motion Sensor
LED
Resistor (220-330 ohms)
Jumper wires
Breadboard
Wiring:
Connect the components as follows:
Connect the PIR sensor:
Connect the sensor's VCC pin to 5V on the NodeMCU.
Connect the sensor's GND pin to GND on the NodeMCU.
Connect the sensor's OUT pin to a digital pin on the NodeMCU (e.g., D2).
Connect the LED:
Connect the LED's anode (longer lead) to a digital pin on the NodeMCU (e.g., D3) through a resistor.
Connect the LED's cathode (shorter lead) to GND.
Arduino Code:
#define timeSeconds 10
// Set GPIOs for LED and PIR Motion Sensor
const int led = 12;
const int motionSensor = 14;
// Timer: Auxiliary variables
unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;
// Checks if motion was detected, sets LED HIGH and starts a timer
ICACHE_RAM_ATTR void detectsMovement() {
Serial.println("MOTION DETECTED!!!");
digitalWrite(led, HIGH);
startTimer = true;
lastTrigger = millis();
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
// PIR Motion Sensor mode INPUT_PULLUP
pinMode(motionSensor, INPUT_PULLUP);
// Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
// Set LED to LOW
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
// Current time
now = millis();
// Turn off the LED after the number of seconds defined in the timeSeconds variable
if(startTimer && (now - lastTrigger > (timeSeconds*1000))) {
Serial.println("Motion stopped...");
digitalWrite(led, LOW);
startTimer = false;
}
}
Instructions:
Copy the code into the Arduino IDE.
Upload the code to your ESP8266 NodeMCU.
Open the Serial Monitor to view the motion detection status.
Observe the LED turning on when motion is detected and turning off when there's no motion.
This example reads the output of the PIR Motion Sensor and controls an LED based on motion detection. Adjust the pin configuration as needed for your specific setup. The LED turns on when motion is detected and turns off when there's no motion.
motion sensor,pir motion sensor,nodemcu pir motion sensor,motion sensor with esp32,pir sensor,esp32 with pir motion sensor,esp32 detecting motion with pir sensor,internet connected motion sensor,pir sensor with arduino and relay,arduino internet connected motion sensor,esp8266 motion sensor,sensor,esp8266 pir motion sensor,motion sensor ic,motion sensor security,esp motion sensor,iot motion sensor,pir motion sensor module,kube motion sensor