Maybe good to inform you about the truth of PIR sensors that help to eliminate the problems that are commonly known with these small units.
To start, the PIR unit basically consist of two critical elements: the piezo electric sensor that senses infrared energy and second electronics circruitry that enables differential detection of infrared energy changes followed by either simple or advanced evaluation circuitry to judge detection or not.
The sensitivity of the sensor is such that only just microvolts of amplitude variation will be passed to the circuitry that is designed to judge “movement detection” or not. If you look at the module from pure technical point of view, it isn’t detecting movement but it detects “changes in the infrared light spectre”. Sudden changes are usually caused by persons that dissipate “heat” from body temperature which is noted when passing a sensor due to the fact that it causes a sudden change in this infrared energy in the detection area.
However, due to the technology, not only persons will be detected. In the case where sudden temperature changes (1 tot 2 degrees will do already) happen, a sensor circuitry may already give so named positives. The “intelligence” of the differential evaluation often is a prime selection criterium for the right sensor in your application. In all cases, it is advised to position the sensor such that no sudden temperature changes are to be expected such as a window or drafty position.
Another point to give serious attention is EM field sensitivity. As written above, the sensor element reports changes in the infrared spectrum with utmost small voltage variations that are in the microvolt range of which the evaluation circuitry needs to decide whether it is a detection or not. Many (cheap) PIR units are positioned close to RF radiating elements (such as wifi) or use long power / data leads that easily pickup electromagnetic energy that cause small voltage variations near the sensor element or circuitry as well. If sudden changes here are notified by the PIR unit (such as a wifi databurst), it may cause false detection. In PIR technology, often “noise” is named as cause for bad detection quality. This noise is not only caused by limited filtering of the evaluation circuit, but also EM disturbances. Actually, there are quite some aspects that influence the total behavior of a PIR unit of course. A further cause of noise is often a microcontroller, causing small switching voltages in IO lines that may already influence a PIR and last but not least the power supply noise may be of influence.
I have studied several PIR units and learned that almost none of them take care of EM filtering in an adequate manner. This is not a big problem when no EM field disturbances are to be expected. When the PIR units is meters away from the (in this case ESP) RF unit, it will work reasonably well. When this is not possible, measures have to be taken. For example, the HC-SR505 PIR unit is great due to small size, but it brings not only very simple evaluation circuitry, but lacks any kind of EM filtering. The 8-pin chip, for example, outputs the detection line via the output pin only via a 1kOhm resistor. There is no filtering whatever that eliminates EM radiation. In cicruitry that evaluates micovolt variations, this is killing.
A third thing to be aware of is light on/off switching. Since an artificial light also radiates infrared energy, the area will undergo a sudden change when light is switched. This is normal and cannot be prevented. Only very advanced PIR units with DSP / asic evaluation circuitry may “recognise” the pattern that light switching causes in the infrared field, which basically differs from a person moving. Simple circuits that we use can normally not differentiate between various types of changes in infrared energy, so don’t waste precious time in trying to eliminate that, you have to take that for granted.
EM radiation can be eliminated very well, but knowledge of RF technology is of importance. Moreover, EM filtering potentially requires additional circuitry which is contrary to the desire to keep things small.
Usually the test setup in your ESPhome isn’t ideal as the unit is subject to all kinds of sudden events that cause false triggering. Main items to keep in mind will be temp variations (just blow to the circuit to test it) and EM susceptibility. Now, what to do to have a reasonably fair PIR element:
- Limit temp variations
As said before, position it in a place where sudden temperature variations (within a couple of seconds) are not to be expected. Slow variations are fine as the circuit measures differential voltages in a window comparator or digital equivalent of that.
- Minimize EM field sensitivity:
- The easiest one is to position the PIR unit as far away from an ESP or other RF device as possible, duh.
- position ferrites on the three wires to accomplish a basic RF filtering
- You may create RF shielding arount the PIR unit by encapsulating it into a metal box. Most important here is that the grounding of the metal box with the ground of the PCB is handled adequate. If done wrong, the metal case may act as antenna rather than RF block.
- You may further filter the power supply and detect output wires with a so calles Lowpass (Pi) filter as close to the circuit as possible, consisting of at least a capacitor of a couple of Pf’s between the power supply / detection output lead and GND as well as an inductor in series with it. I would start to use 100pF and 220nH as this has a cut-off frequency of about 80MHz, which should eliminate problems enough.
- minimize voltage ripple
Minimize voltage / data ripple on the leads. This can be done in its simplest form by eventually adding a ~100 of 220uF electrolytic capacitor over the voltage line and GND, again as close to the circuit as possible.
If handled well, the measures of above will significantly improve the performance. I suggest to test these suggestions one by one, starting with the first. Most of times, you don’t need to implement all to have a reasonable behavior.
Good luck!