I have wasted many hours myself on several occasions chasing strange behaviour/errors only to discover the problem was just power so this is the first thing I would rule out as your problem.
I usually include a large smoothing capacitor on all my projects (in the order 3000mf) to avoid this sort of thing.
It may even be worth powering the esp32 and PIR from their own power supplies (remembering to link the GNDs) to make fully sure your problem is not power related.