HELP! my coding for the h-sr501 pir motion sensor did not detect any motion / doesn't display any reading

This is my coding:

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

#define trigPin D7         // Pin for sensor trigger
#define echoPin D8            // Pin for sensor echo
#define motionPin D3          // Pin for motion sensor
#define RECEIVER_IP IPAddress(192, 168, 4, 1) // IP address of the receiver
#define PORT 1234              // UDP port number
#define SOUND_VELOCITY 0.034
#define CM_TO_INCH 0.393701

const char* ssid = "mukhriz";     // Enter your WiFi SSID
const char* password = "b4e7f435c0"; // Enter your WiFi password
const IPAddress serverIP(192, 168, 4, 1); // IP address of the access point
const int serverPort = 1234; 

WiFiUDP udp;
// defines variables
long duration;
int distance;
float distanceCm;
float motionCm;
int measureDistance;
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(motionPin, INPUT);     // declare sensor as input

  Serial.begin(115200);
  Serial.println();
  Serial.println("Connecting to Access Point...");
  WiFi.begin("mukhriz", "b4e7f435c0"); // Replace with your Access Point SSID and password
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {

  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance= duration*0.034/2;

   distanceCm = duration * SOUND_VELOCITY/2;

     // Prints the distance on the Serial Monitor
  Serial.print("Distance (cm): ");
  Serial.println(distanceCm);
  delay(1000);

  // Check if distance is above 10cm and below 200cm
  if (distanceCm > 10 && distanceCm < 200) {
    sendTrigger();
    delay(1000); // Delay to prevent multiple triggers
  } else {
    Serial.println("Distance detected by HC-SR04 is not within the desired range, no trigger sent.");
  }

int motionState = digitalRead(motionPin);
     // Prints the distance on the Serial Monitor
  Serial.print("Motion (cm): ");
  Serial.println(motionCm);
  delay(1000);

    val = digitalRead(motionPin);  // read input value
  
  if (val == motionCm > 10 && motionCm < 200) {	// check if the input is HIGH            
    sendTrigger();
    delay(1000);
  } else
    {Serial.println("Motion Distance detected by HC-SR501 is not within the desired range, no trigger sent.");
  } 
  
}

void connectToWiFi() {
  Serial.println("Connecting to WiFi");
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void sendTrigger() {
  Serial.println("Sending trigger to access point");
  
  // Send trigger data packet to the access point
  udp.beginPacket(serverIP, serverPort);
  udp.write("trigger");
  udp.endPacket();
}

The other coding works fine but only for the PIR motion sensor coding can't read anything

Have you tried any pir motion sensor simple example ?

what is this?

Variable for reading pin status

It should be like this: ... if (val == motionCm && motionCm < 200) {

or like this: ... if (val > 10 && motionCm < 200) {

or: if ( motionCm > 10 && motionCm < 200) {

1 Like

I think you should get rid of these delay(1000); in your code.
You can use timestamps and check the elapsed time rather than blocking the main thread making the subsequent input readings unreliable.

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