My ESP8266 program sends motion notifications once it start while there is no movement

hey im using ESP8266, i've been struggling with this issue and i don't know how to fix this, so my program once it start it always sends motion detection notifications and start the alarm, after the alarm stop and the "alarm deactivated" message sent, then after that my device is working properly, i want to fix the first issue above but i couldn't figure out the problem can anyone help me fix my issue
here is my code

#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>

#ifdef ESP32
  #include <WiFi.h>
#else
  #include <ESP8266WiFi.h>
#endif


const char* ssid = "**";//i did put my ssid correctly
const char* password = "**";//i did put my wifi password correctly


#define BOTtoken "****"// i did put my bot token correctly   


#define CHAT_ID "****" // i did put my chat id correctly

#ifdef ESP8266
  X509List cert(TELEGRAM_CERTIFICATE_ROOT);
#endif

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

const int motionSensor = 14;
const int motionSensor1 = 5;
bool motionDetected = false;
int val = 0;
int val1 = 0;
const int buzzer = 12;
unsigned long lastMotionTime = 0; 
const unsigned long alarmDuration = 60000; 
bool alarmOn = false;


void ICACHE_RAM_ATTR detectsMovement0() {
  motionDetected = true; 
}

void setup() {
  Serial.begin(115200);

  #ifdef ESP8266
    configTime(0, 0, "pool.ntp.org");      
    client.setTrustAnchors(&cert); 
  #endif

  #ifdef ESP32
    client.setCACert(TELEGRAM_CERTIFICATE_ROOT); 
  #endif
  
  pinMode(motionSensor, INPUT_PULLUP);
  pinMode(motionSensor1, INPUT_PULLUP);

 
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  bot.sendMessage(CHAT_ID, "Bot started up", "");

  lastMotionTime = millis();

  pinMode(buzzer, OUTPUT);
}

void loop() {
  val = digitalRead(motionSensor);  
  val1 = digitalRead(motionSensor1); 
  
  if (val == HIGH && !alarmOn) {
    alarmOn = true;
    lastMotionTime = millis(); 
    digitalWrite(buzzer, HIGH); 
    bot.sendMessage(CHAT_ID, "Motion detected at sensor 1, alarm activated", "");
  }

  if (val1 == HIGH && !alarmOn) {
    alarmOn = true;
    lastMotionTime = millis(); 
    digitalWrite(buzzer, HIGH); 
    bot.sendMessage(CHAT_ID, "Motion detected at sensor 2, alarm activated", "");
  }

  
  if (alarmOn && millis() - lastMotionTime >= alarmDuration) {
    alarmOn = false;
    digitalWrite(buzzer, LOW);
    bot.sendMessage(CHAT_ID, "Alarm deactivated", "");
  }

  
  if (val == HIGH && alarmOn) {
    lastMotionTime = millis(); 
    digitalWrite(buzzer, HIGH); 
    bot.sendMessage(CHAT_ID, "Additional motion detected during alarm period at sensor 1 ", "");
  }

  if (val1 == HIGH && alarmOn) {
    lastMotionTime = millis(); 
    digitalWrite(buzzer, HIGH);
    bot.sendMessage(CHAT_ID, "Additional motion detected during alarm period at sensor 2 ", "");
  }
}

What type of sensor are you using? Since you configured the pins as INPUT_PULLUP, they normally would be HIGH and probably LOW when motion is detected? Can't tell without the sensor datasheet. When you first power up, maybe it takes some time before the sensor settles down and outputs a valid signal? Again, can't tell without the datasheet so these are just guesses.

im using HC-SR501 PIR sensor module for the sensors, is this information would help?

I don't know if your sensor is exactly like the one from Adafruit, but this article will give you a lot of useful information about triggering

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.