SR 501 unstable results

Hello All,

Like many, I am having some reliability issues with my SR-501 PIR motion sensor. I realize I am not alone and that there are many online who describe similar issues. What I cannot find however, is a consistent solution. So, in an attempt to find a solution, I am posting here to look for suggestions.

My setup:
I have a raspberry pi zero W, with a small webcam and a PIR sensor plugged in. I am happy to consider or try a micro-controller (in place of the pi), but the zeroW is pretty cheap around me and the built in wifi+native python sold me right away. My goal is to have the PIR sense motion and publish status to my MQTT Broker/HomeAssistant hub. Right now this is all working just fine. I also would like to add funcitonality such that when motion is detected, python uses the webcam to snap a couple photos (something I already have the code for).

The problem, is that I cannot get a consistent read on the sensor. I have tweaked the sensitivity, and the delay potentiometers, as well as swapped the jumper. I see that some people claim its an RF issue with the wifi, or that adding a capacitor to pins on the motion detector chip is necessary, or that its a power source thing. I have only tried changing sensors, changing PIs and adding longer header wires to connect everything. Also, its worth noting that I have also tested without the camera connected just in case.

  1. Can anyone offer any solutions?
  2. Just in case I am missing something, would anyone be willing to share their code?

I had to shield the SR501 from the NodeMCU when I installed it all in a box. I just surrounded it with two angled metal brackets stuck together with tape, which was enough to prevent false triggers.

I knew the triggers were false because the system worked fine for weeks before I tried to put it in a box, when the SR-501 was slightly further away from the NodeMCU.

thanks for the advice, I just tried that but so far, it has not helped!

Does anyone else have any suggestions?

I am about to give up on these cheap little things. I can’t understand why my readings are so noisey. I see tons of tutorials showing how to interface them with the Pi or Arduino, does anyone bother to actually use them beyond a quick little demo?

My five have been working for over six months (1 with a NodeMCU, 1 with a Wemos D1 mini and 3 with MySensors nodes).

Would you care to check if I am doing anything wrong? here is my code for my NodeMCU D1 mini (which I just tested). I am getting cyclical “alarm”, “safe” almost every few seconds.

I have my PIR hooked up to the 5V and G on the D1 mini, the signal is connected to D3. Connection to the MQTT gateway is perfect…I can see the results in Home Assistant just fine.

> #include <ESP8266WiFi.h>
> #include <Wire.h>
> #include <PubSubClient.h>
> 
> #define wifi_ssid "SSID"
> #define wifi_password "PASS"
> 
> #define mqtt_server "192.168.1.15"
> #define mqtt_user "your_username"
> #define mqtt_password "your_password"
> 
> #define motion_topic "home-assistant/motion/hallway"
> 
> WiFiClient espClient;
> PubSubClient client(espClient);
> int inputPin = 0;
> int pirState = LOW;
> int val = 0;
> 
> void setup() {
>   Serial.begin(9600);
>   setup_wifi();
>   client.setServer(mqtt_server, 1883);
>   pinMode(LED_BUILTIN, OUTPUT);
>   pinMode(inputPin, INPUT);
>   
> }
> 
> void setup_wifi() {
>   delay(10);
>   // We start by connecting to a WiFi network
>   Serial.println();
>   Serial.print("Connecting to ");
>   Serial.println(wifi_ssid);
> 
>   WiFi.begin(wifi_ssid, wifi_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 reconnect() {
>   // Loop until we're reconnected
>   while (!client.connected()) {
>     Serial.print("Attempting MQTT connection...");
>     // Attempt to connect
>     // If you do not want to use a username and password, change next line to
>     if (client.connect("ESP8266Client")) {
>     // if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
>       Serial.println("connected");
>     } else {
>       Serial.print("failed, rc=");
>       Serial.print(client.state());
>       Serial.println(" try again in 5 seconds");
>       // Wait 5 seconds before retrying
>       delay(5000);
>     }
>   }
> }
> 
> void loop() {
>   if (!client.connected()) {
>     reconnect();
>   }
>   client.loop();
> 
>   val = digitalRead(inputPin);  // read input value
>   if (val == HIGH) {            // check if the input is HIGH
>     digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on (Note that LOW is the voltage level but actually the LED is on; this is because it is active low on the ESP-01)
>     if (pirState == LOW) {
>       Serial.print("ALARM");
>       client.publish(motion_topic, "ALARM");
>       pirState = HIGH;
>     }
>   } else{
>     digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH
>     if (pirState == HIGH){
>       client.publish(motion_topic, "SAFE");
>       Serial.print("SAFE");
>       pirState = LOW;
>     } 
>   }
>   delay(500);
> }

Small update:

I had first tried using a raspberry pi zero w (built in WiFi), but received lots of false calls.

Recently I switched to an ESP8266, and noticed the same noise. However, my previous code (posted above) had used the input pin 0 which I later found is held high. I may have broken the PIR sensor with that setup, as it doesn’t seem to read at all now. However, if I use a new PIR connected to D2 and my little homemade shield, I am getting much better results.

1 Like

Of course I spoke too soon. the shield did seem to help, but it did not fix the problem. I have tried shielding a n SR501 from an ESP8266 and also in a separate instance from a raspberryu pi zero w. The ESP8266 produced better results, but both showed false alerts within a matter of hours.

My shield was an Aluminum foil wrapped in plastic and grounded to the negative DC power lead.

I did a little more experimenting. I am convinced that the issue is related to RF, but the shield doesnt block everything. I have now tried wrapping the entire PCB of the sensor with paper-aluminim-paper sandwhich with holes only for the sensor and leads coming out the back. The aluminum was grounded to the negative line on the sensor.

Much of the noise did go away, but it is not completely gone. Attached is a photo of my attempt.

Whats interesting is that I have a second one setup (different location) that is hard wired to a raspberry pi which is hardwired to the router. This one does not have any false calls, but is fairly close to a second, active, wireless router.

I am wondering how exactly the RF noise is being introduced…because it must come over the wires.

UPDATE: If I move the wireless unit right next to the other unit, then the wired unit DOES receive a ton of noise. Shielding helps, but I am not shielding all of it. Also, I tried powering them both from different power supplies…which showed no change.

I thought I would show my shielding. It is not grounded, but it is quite thick steel, from shelving brackets, doubled up next to the SR-501 and wrapped in tape. Its crude, but has been effective.

Thanks for the photo.

Today I tried making a similar setup to what you have shown there. I used thicker steel and grounded it. Unfortunately, I still cannot get a stable read. It literally flickers with false calls every 5-10 seconds.

I am about to give up on these sensors :frowning:

Quick question. I have read that changing the esp power and mode(wireless g) can help. Do you know how to do this? Are you currently?

Would you care to share your code?

The arduino esp8266 library documentation is here http://arduino-esp8266.readthedocs.io/en/latest/index.html

But I haven’t seen anything that could change the power output. I think you probably need to change the configuration of your router to change the channel.

My code is based on an early version of what became the OpenMQTTGateway. I have added my own motion and light sensor, and a notification to simple push if it detects the broker has gone down. But the wifi setup is all the standard stuff you can find all over the internet.

/*
  433toMQTTto433  - ESP8266 program for home automation 
   Act as a wifi gateway between your 433mhz nodes and a MQTT broker 
   Send and receiving command by MQTT
 
  This program enables to:
 - receive MQTT data from a topic and send RF 433Mhz signal corresponding to the received MQTT data
 - publish MQTT data to a different topic related to received 433Mhz signal
  Contributor:
  - 1technophile
  Based on:
  - MQTT library (https://github.com/knolleary)
  - RCSwitch (https://github.com/sui77/rc-switch)
  - ESP8266Wifi
  
  Project home: https://github.com/1technophile/433toMQTTto433_ESP8266
  Blog, tutorial: http://1technophile.blogspot.com/2016/09/433tomqttto433-bidirectional-esp8266.html
Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
and associated documentation files (the "Software"), to deal in the Software without restriction, 
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Some usefull commands to test gateway with mosquitto:
Subscribe to the subject for data receiption from RF signal
mosquitto_sub -t home/433toMQTT
Send data by MQTT to convert it on RF signal
mosquitto_pub -t home/MQTTto433/ -m 1315153
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <RCSwitch.h> // library for controling Radio frequency switch
#include <ESP8266mDNS.h>
#include <Simplepush.h>
#include "secrets.h"

RCSwitch mySwitch = RCSwitch();
Simplepush simple;

//Do we want to see trace for debugging purposes
#define TRACE 1  // 0= trace off 1 = trace on

// Update these with values suitable for your network.

//variables to avoid duplicates
#define time_avoid_duplicate 1500 // if you want to avoid duplicate mqtt message received set this to > 0, the value is the time in milliseconds during which we don't publish duplicates
// array to store previous received RFs codes and their timestamps
long ReceivedRF[10][2] ={{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}};

#define subjectMQTTtoX "home/commands/#"
//RF MQTT Subjects
#define subject433toMQTT "home/433toMQTT"
#define subjectMQTTto433 "home/MQTTto433/"
#define SUBJECTSIMPLEPUSHKEY "simplepush/key"

//adding this to bypass to problem of the arduino builder issue 50
void callback(char*topic, byte* payload,unsigned int length);
WiFiClient wifiClient;

// mqttClient parameters
PubSubClient mqttClient(wifiClient);

//MQTT last attemps reconnection number
unsigned long lastReconnectAttempt = 0;
unsigned long reconnectStart = 0;
unsigned long lastDarkMessage = 0;
bool noticeSent = false;
bool firsttime = true;
char simplepush_key[128];
char simplepush_title[128] = "MQTT Broker Inaccessible";
char simplepush_message[128] = "Cannot connect to MQTT broker";
char simplepush_event[128] = "Failure";

// Motion Sensor Pin
const int MOTIONSENSORPIN = D1;  // D5 on NodeMCU is GPIO14 on 202
boolean motionDetected = false;
const int LIGHT_LEVEL_PIN = D8;
boolean lightlevel = false;

// Callback function, when the gateway receive an MQTT value on the topics subscribed this function is called
void callback(char* topic, byte* payload, unsigned int length) {
  // In order to republish this payload, a copy must be made
  // as the orignal payload buffer will be overwritten whilst
  // constructing the PUBLISH packet.
  trc("Hey I got a callback ");

  if (memcmp(topic, SUBJECTSIMPLEPUSHKEY, strlen(SUBJECTSIMPLEPUSHKEY)) == 0) {
    // Update push key with new value
    int tocopy = std::min((unsigned int)127, length);
    memcpy(simplepush_key, payload, tocopy);
    simplepush_key[tocopy] = '\0';
    trc("Simplepush key is now ");
    trc(simplepush_key);
    return;
  }

  // Allocate the correct amount of memory for the payload copy
  byte* p = (byte*)malloc(length);
  // Copy the payload to the new buffer
  memcpy(p,payload,length);
  
  // Conversion to a printable string
  p[length] = '\0';
  String callbackstring = String((char *) p);
  String topicNameRec = String((char*) topic);
  
  //launch the function to treat received data
  receivingMQTT(topicNameRec,callbackstring);

  // Free the memory
  free(p);
}

void setup()
{
  //Launch serial for debugging purposes
  Serial.begin(115200);
  //Begining wifi connection
  setup_wifi();
  delay(1500);
  lastReconnectAttempt = 0;
  strncpy(simplepush_key, DEFAULT_SIMPLEPUSH_KEY, 128);
  
  //mySwitch.enableTransmit(4); // RF Transmitter is connected to Pin D2 
  //mySwitch.setRepeatTransmit(20); //increase transmit repeat to avoid lost of rf sendings
  mySwitch.enableReceive(digitalPinToInterrupt(D2));  // Receiver on NodeMCU pin D2 -- GPIO 5 on 202

  pinMode(MOTIONSENSORPIN, INPUT);

  mqttClient.setCallback(callback);

  if (!MDNS.begin("MQTTto433")) {
    trc("Error setting up MDNS responder");
  }

}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  trc("Connecting to ");
  trc(wifi_ssid);

  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    trc(".");
  }
  trc("WiFi connected");
}

boolean reconnect() {

  // Loop until we're reconnected
  if (!mqttClient.connected()) {
    firsttime = true;
    if (MDNS.queryService("mqtt", "tcp") > 0) {
      mqttClient.setServer(MDNS.IP(0), MDNS.port(0)); 

      trc("Attempting MQTT connection...");
      // Attempt to connect
      if (mqttClient.connect("433toMQTTto433", "sensor/433/status", 1, true,
                             "offline")) {
        // Once connected, publish an announcement...
        mqttClient.publish("sensor/433/status","online", true);
        
        trc("connected");
        //Subscribing to topic(s)
        //subscribing(subjectMQTTtoX);
        if (!mqttClient.subscribe(SUBJECTSIMPLEPUSHKEY)) {
          trc("Couldn't subscribe to simplepush_key");
        }
      } else {
        trc("failed, rc=");
        trc(String(mqttClient.state()));
      }
    } else {
      trc("No MQTT services found");
    }
  }
  return mqttClient.connected();
}

void loop()
{
  unsigned long now = millis();

  //MQTT mqttClient connexion management
  if (!mqttClient.connected()) {
    if (now - lastReconnectAttempt > 5000) {
      lastReconnectAttempt = now;
      if (!noticeSent && reconnectStart == 0) {
        reconnectStart = millis();
      }
      trc("mqttClient mqtt not connected, trying to connect");
      // Attempt to reconnect
      if (reconnect()) {
        lastReconnectAttempt = 0;
        reconnectStart = 0;
        noticeSent = false;
      } else if (!noticeSent && ((now - reconnectStart) > 60000)) {
        trc("Sending alert");
        simple.send(simplepush_key, simplepush_title,
            simplepush_message, simplepush_event);
        noticeSent = true;

      }
    }
  } else {
    // MQTT loop
    mqttClient.loop();
  }

  // Receive loop, if data received by RF433 send it by MQTT to subject433toMQTT
  if (mySwitch.available()) {
    // Topic on which we will send data
    trc("Receiving 433Mhz signal");
    unsigned long MQTTvalue;
    MQTTvalue=mySwitch.getReceivedValue();  
    mySwitch.resetAvailable();
    if (mqttClient.connected()) {
      if (!isAduplicate(MQTTvalue)) {// conditions to avoid duplications of RF -->MQTT
        //trc("Sending 433Mhz signal to MQTT");
        //trc(String(MQTTvalue));
        switch (MQTTvalue) {
          case 4000398:  // rc button 1 off 
            sendMQTT("/energenie/ENER002/444102/2", "OFF", true);
            break;
          case 4000399:  // rc button 1 on
            sendMQTT("/energenie/ENER002/444102/2", "ON", true);
            break;
          case 4000390:  // rc button 2 off 
            sendMQTT("/energenie/ENER002/444102/3", "OFF", true);
            break;
          case 4000391:  // rc button 2 on
            sendMQTT("/energenie/ENER002/444102/3", "ON", true);
            break;
          case 4000394:  // rc button 3 off 
            sendMQTT("/energenie/ENER002/444102/4", "OFF", true);
            break;
          case 4000395:  // rc button 3 on
            sendMQTT("/energenie/ENER002/444102/4", "ON", true);
            break;
          case 4000386:  // rc button 4 off 
            break;
          case 4000387:  // rc button 4 on
            break;
          case 4000396:  // rc button all off 
            sendMQTT("/energenie/ENER002/444102/2", "OFF", true);
            sendMQTT("/energenie/ENER002/444102/3", "OFF", true);
            sendMQTT("/energenie/ENER002/444102/4", "OFF", true);
            break;
          case 4000397:  // rc button 4 on
            sendMQTT("/energenie/ENER002/444102/2", "ON", true);
            sendMQTT("/energenie/ENER002/444102/3", "ON", true);
            sendMQTT("/energenie/ENER002/444102/4", "ON", true);
            break;            

          default:
            sendMQTT(subject433toMQTT,String(MQTTvalue), false);
        }
        storeValue(MQTTvalue);
      }
    } else {
      if (reconnect()) {
        trc("Sending 433Mhz signal to MQTT after reconnect");
        trc(String(MQTTvalue));
        sendMQTT(subject433toMQTT,String(MQTTvalue), false);
        storeValue(MQTTvalue);
        lastReconnectAttempt = 0;
      }
    }
  }

  if (mqttClient.connected()) {
    //Light level
    // Only send once a second to avoid spamming when light 
    // level is on the limit
    if (now < lastDarkMessage // Check for wrap around
        ||
        now > (lastDarkMessage + 1000)
        || firsttime == true) {
      boolean currentLight = (digitalRead(LIGHT_LEVEL_PIN) == HIGH);
      if (lightlevel && !currentLight) {
        sendMQTT("sensor/433/dark", "OFF", false);
        lightlevel = currentLight;
        lastDarkMessage = now;
      } else if (!lightlevel && currentLight) {
        sendMQTT("sensor/433/dark", "ON", false);
        lightlevel = currentLight;
        lastDarkMessage = now;
      } else if (firsttime == true) {
        sendMQTT("sensor/433/dark", currentLight?"ON":"OFF", false);
        lightlevel = currentLight;
        lastDarkMessage = now;
      }
    }

  // Moton Detection
    boolean currentMotion = (digitalRead(MOTIONSENSORPIN) == HIGH);
    if (motionDetected && !currentMotion) {
      sendMQTT("sensor/433/motion", "OFF", false);
      motionDetected = currentMotion;
    } else if (!motionDetected && currentMotion) {
      sendMQTT("sensor/433/motion", "ON", false);
      motionDetected = currentMotion;
    } else if (firsttime == true) {
      sendMQTT("sensor/433/motion", currentMotion?"ON":"OFF", false);
      motionDetected = currentMotion;
    }
    firsttime = false;
  }
}

void storeValue(long MQTTvalue){
    long now = millis();
    // find oldest value of the buffer
    int o = getMin();
    trc("Minimum index: " + String(o));
    // replace it by the new one
    ReceivedRF[o][0] = MQTTvalue;
    ReceivedRF[o][1] = now;
    trc("send this code :" + String(ReceivedRF[o][0])+"/"+String(ReceivedRF[o][1]));
    trc("Col: value/timestamp");
    for (int i = 0; i < 10; i++)
    {
      trc(String(i) + ":" + String(ReceivedRF[i][0])+"/"+String(ReceivedRF[i][1]));
    }
}

int getMin()
{
  int minimum = ReceivedRF[0][1];
  int minindex=0;
  for (int i = 0; i < 10; i++)
  {
    if (ReceivedRF[i][1] < minimum) {
      minimum = ReceivedRF[i][1];
      minindex = i;
    }
  }
  return minindex;
}

boolean isAduplicate(long value){
  trc("isAduplicate");
  // check if the value has been already sent during the last "time_avoid_duplicate"
  for (int i=0; i<10;i++){
    if (ReceivedRF[i][0] == value){
      long now = millis();
      if (now - ReceivedRF[i][1] < time_avoid_duplicate){
        trc("don't send this code :" + String(ReceivedRF[i][0])+"/"+String(ReceivedRF[i][1]));
        return true;
      }
    }
  }
  return false;
}

void subscribing(String topicNameRec){ // MQTT subscribing to topic
  char topicStrRec[26];
  topicNameRec.toCharArray(topicStrRec,26);
  // subscription to topic for receiving data
  boolean pubresult = mqttClient.subscribe(topicStrRec);
  if (pubresult) {
    trc("subscription OK to");
    trc(topicNameRec);
  }
}

void receivingMQTT(String topicNameRec, String callbackstring) {
  trc("Receiving data by MQTT");
  trc(topicNameRec);
  char topicOri[26] = "";
  char topicStrAck[26] = "";
  char datacallback[26] = "";

  // Below you send RF signal following data value received by MQTT 
  callbackstring.toCharArray(datacallback,26);
  trc(datacallback);
  long int data = atol(datacallback);
  trc("Send received data by RF 433");
  trc(String(data));
  //send received MQTT value by RF signal (example of signal sent data = 5264660)
  mySwitch.send(data, 24);
}

//send MQTT data dataStr to topic topicNameSend
void sendMQTT(String topicNameSend, String dataStr, boolean retain){

  char topicStrSend[100];
  topicNameSend.toCharArray(topicStrSend,100);
  char dataStrSend[200];
  dataStr.toCharArray(dataStrSend,200);
  boolean pubresult = mqttClient.publish(topicStrSend,dataStrSend, retain);
  trc("sending ");
  trc(dataStr);
  trc("to ");
  trc(topicNameSend);
}

//trace
void trc(String msg){
  if (TRACE) {
    Serial.println(msg);
  }
}

/* vim: set expandtab shiftwidth=2 tabstop=2: */

I also had a lot of false positives with the SR-501 connected to raspi or esp8266.

This sensor is very sensitive to RF interference and will generate false positives.

I had a lot of success following this video:

I rarely get false positives now.