The Pi supply is fully isolated and the 5V supply is likely to be so as well, there shouldn't be any problem joining the two 0V rails.The problem with your original wiring becomes clear if you consider the current flow through the sensor transistor. Pi 3v3 supply, pull up resistance, gpio pin, yellow wire, transistor, external supply ground then somehow it needs to find its way back to pi ground via 2 supplies and your mains wiring. A tortuous route with opportunity for mains hum.
Linking grounds may work but the two ends are at different potentials and could well induce a high alternating current which is best kept away from your pi. You avoid this if you power the sensor from the pi. A general rule is to keep grounds shorthand clean. You could power the lot from the pi of course and simplify your setup.
Phil
Statistics: Posted by pidd — Mon Jan 01, 2024 12:46 am