Wifi interference with HC-SR501

I have had numerous false positive triggers of HC-SR501 on an ESP32, unfortunately my research has led me to some weird and wonderful solutions. From running it on 3.3V, soldering items to it, and reducing the Wifi band and tx power. On the latter, I am testing a build with some changes to do just that.

Research - HC-SR501 PIR - stop false positives? - Page 1 - Let's Control It
I solved this with the next solutions:
A: Used a 47uF and 102pF power decompling consensator soldered over the pir power connections at the ESP8266 side.
B: Put the ESP in 802.11G mode instead of 802.11N mode. I think the Rf spectrum will be less in G mode.
C: Put the ESP in wifi STA mode only.
D: Lower the ESP wifi output power to 10dBm.

In main.ino

  //Reduce WiFi interference when using ESP32 using custom WiFi mode and tx power
#  ifdef ESP32
  // https://github.com/jnogues/ESP32-Long-Range-WiFi/issues/4
  // Reset wifi protocol
  if (esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N | WIFI_PROTOCOL_LR) != ESP_OK) {
          Log.error(F("Failed to reset WifiMode." CR));
  }
  //https://github.com/espressif/arduino-esp32/search?q=WIFI_PROTOCOL_11G
  //https://www.letscontrolit.com/forum/viewtopic.php?t=671&start=20
  #ifdef WifiGMode
    if (esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G) != ESP_OK) {
      Log.error(F("Failed to change WifiMode." CR));
    }
  #endif

  #ifdef WifiPower
    WiFi.setTxPower(WifiPower);
  #endif
#  endif

In environments.ini

build_flags =
  ${env:esp32doitv1-aithinker-r01-sx1278.build_flags}
  '-DHCSR501_LED_NOTIFY_GPIO=LED_BUILTIN'
  '-DWifiGMode=true'
  '-DWifiPower=WIFI_POWER_11dBm'
  '-DZsensorHCSR501="HCSR501"'
  '-DHCSR501_GPIO=13'

Anybody else with similar PIR problems and resolutions? I am now also logging the triggers to see if the code made any difference.

1 Like

Been 12 hours with no False triggers, and I continue to monitor.

Not a single false trigger in the past 36 hours :slight_smile:

1 Like

@diepeterpan You are very determined with that motion sensor, mine went in the scrap pile years ago as the internet consensus was that the AM312 was a lot more reliable.

One of many threads - HC-SR501 pir sensor gives false triggers - #5 by martinst - Hardware - Home Assistant Community

So far I am very impressed at the stability since changing the WiFi to G instead of N and lowering the tx power. Not a single false trigger in 60 hours.