/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-motion-sensor
*/
const int PIN_TO_SENSOR = 19; // GPIO19 pin connected to OUTPUT pin of sensor
int pinStateCurrent = LOW; // current state of pin
int pinStatePrevious = LOW; // previous state of pin
void setup() {
Serial.begin(9600); // initialize serial
pinMode(PIN_TO_SENSOR, INPUT); // set ESP32 pin to input mode to read value from OUTPUT pin of sensor
}
void loop() {
pinStatePrevious = pinStateCurrent; // store old state
pinStateCurrent = digitalRead(PIN_TO_SENSOR); // read new state
//Serial.println(pinStateCurrent);
if (pinStatePrevious == LOW && pinStateCurrent == HIGH) { // pin state change: LOW -> HIGH
Serial.println("Motion detected!");
// TODO: turn on alarm, light or activate a device ... here
}
else
if (pinStatePrevious == HIGH && pinStateCurrent == LOW) { // pin state change: HIGH -> LOW
Serial.println("Motion stopped!");
// TODO: turn off alarm, light or deactivate a device ... here
}
delay(10);
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND
Loading
ds18b20
ds18b20
pir1:VCC
pir1:OUT
pir1:GND
ntc1:GND
ntc1:VCC
ntc1:OUT
imu1:INT
imu1:AD0
imu1:XCL
imu1:XDA
imu1:SDA
imu1:SCL
imu1:GND
imu1:VCC
rtc1:GND
rtc1:5V
rtc1:SDA
rtc1:SCL
rtc1:SQW
ldr2:VCC
ldr2:GND
ldr2:DO
ldr2:AO
relay1:VCC
relay1:GND
relay1:IN
relay1:NC
relay1:COM
relay1:NO
led1:A
led1:C
r1:1
r1:2