HC-SR501 PIR sensor problem in multi-sensor unit, Blynk

I’m working on a multi-sensor unit using ESP 8266 and HC-SR501 PIR sensor, along w other sensors. All sensors working fine except the HC-SR501 PIR sensor. I’m not getting any reading in Blynk. Tried both “push” and other seconds in Blynk and also tried the 2 adjustment screws. Also tried 3 other HC-SR501 PIR sensor units but my guess is it’s in the code somewhere. Code is below minus person info. Thank you all!

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>


/************************* Pin Definition *********************************/

//Relays for switching appliances
#define Relay1            D6
#define Relay2            D2
#define Relay3            D1
#define Relay4            D5

//DHT11 for reading temperature and humidity value
#define DHTPIN            D7

//buzzer to use it for alert
#define buzzer            D0

//Selection pins for multiplexer module to switch between different sensors and give data on a single analog pin.
#define S0                D3
#define S1                D4

//Analog pin to read the incoming analog value from different sensors.
#define analogpin         A0

int proximity;
int light;
int gas;
int motion;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "Blynk_Token";

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301



DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "SSID";
char pass[] = "PASS";
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V0, t);
  Blynk.virtualWrite(V1, h);
  Blynk.virtualWrite(V2, proximity);
  Blynk.virtualWrite(V3, gas);
  Blynk.virtualWrite(V4, motion);
  Blynk.virtualWrite(V5, light);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  dht.begin();
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(A0, INPUT);
  pinMode(buzzer, OUTPUT);

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}

void loop()
{
  // Now we can publish stuff!
  // Address 00
  digitalWrite(S0, LOW);
  digitalWrite(S1, LOW);
  gas = analogRead(analogpin);
  Serial.print("Gas - "); Serial.println(gas);

// Address 10
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);
  motion = analogRead(analogpin);
  if (motion > 512)
  {
    motion = 1;

  }
  else
  {
    motion = 0;

  }
// Address 01
  digitalWrite(S0, LOW);
  digitalWrite(S1, HIGH);
  proximity = analogRead(analogpin);
  Serial.print("Proximity - "); Serial.println(proximity);
  if (proximity < 512)
  {
    proximity = 1;
    digitalWrite(buzzer, HIGH);
  }
  else
  {
    proximity = 0;
    digitalWrite(buzzer, LOW); 
  }



// Address 11
  digitalWrite(S0, HIGH);
  digitalWrite(S1, HIGH);
  int raw_light = analogRead(analogpin);
  light = map(raw_light, 1024, 0, 0, 100);
  Serial.print("Light - "); Serial.println(light);

  Blynk.run();
  timer.run();
}

How are these PIR sensors connected and powered?

Why do you have all this code on your void loop?

Pete.

They are powered from a 9 volt power supply going through a L7805 voltage regulator powered down to 5 volts. The positive for sensors is coming from the “vin” pin on the Esp8266. This is the actual project.

I did not write the code. Thank you!

I don’t understand why anyone would use an analogue multiplexer for monitoring the output from digital devices.

Did you do this on the custom PCB, or have you wired it up yourself?

Pete.

I wired it myself. I’m not using the custom PCM. I am not using the relays with the lights. I have the temp/humidity, gas and motion sensor only but all but the motion seem to be working.

I’d check the wiring, especially ensuring that you have a common ground from the sensors to the multiplexer and back to the MCU.
I’d also add some serial print statements to see what (if any) values you are getting from the PIRs.

This is a badly designed circuit, with badly written code, so you may be better taking a different approach, maybe with an ESP32 and no multiplexer.

Pete.

I’m actually an inventor and this is part of a bigger project and my experience is not in coding. I’m a novice at this but I have done other projects. I thought the code looked sketchy from the beginning but I could not find any other code that used 4 sensors and integrated w Blynk. If you know of another resource, it would be greatly appreciated. Thank you.