Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Last active September 25, 2023 09:42
Show Gist options
  • Save kakopappa/bc5a7152c02e4e692ca9ae6708fe6814 to your computer and use it in GitHub Desktop.
Save kakopappa/bc5a7152c02e4e692ca9ae6708fe6814 to your computer and use it in GitHub Desktop.
#if defined(ESP8266)
#define SENSOR_PIN D1
#elif defined(ESP32)
#define SENSOR_PIN 26
#elif (ARDUINO_ARCH_RP2040)
#define SENSOR_PIN 5
#endif
int newState;
int oldState;
void setup() {
Serial.begin(9600);
pinMode(SENSOR_PIN, INPUT);
Serial.printf("Wait 10 secs for the PIR sensor to calibrate properly\r\n");
delay(1000 * 10);
oldState = digitalRead(SENSOR_PIN); // read the starting state
}
void loop() {
newState = digitalRead(SENSOR_PIN); // read current state
if(oldState != newState) {
if (newState == HIGH) {
Serial.println("Movement detected!");
} else {
Serial.println("Movement not detected!");
}
oldState = newState;
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment