Why does controlling power with a transistor interfere with MHZ19B's serial communication? I've been trying to turn off an MHZ19B (datasheet and teardown photos: revspace.nl/MH-Z19B). I've used an Arduino, NPN, PNP transistors and mosfets as well as the diagram below for connecting the sensor to an ESP32. In all cases, PWM communication works great! I can power off the sensor, turn it back on, and values derived from PWM communication are as expected. However when I connect the Rx and Tx lines (to software serial Tx and Rx pins respectively on Uno pins A0 A1 or ESP32 pins 32 33) they spit out gibberish and the PWM signal also suffers hiccups.
Otherwise, without any kind of logic power control, the MHZ19B works great and can communicate with anything via UART, even my PC usb port via FTDI.
I added resistors to the Rx and Tx lines so the voltage on them doesn't exceed Vcc. I tried 1n4007 diodes (they're the only ones I have; no Schottky diodes either). I tried an NPN equivalent optocoupler. I also tried powering the sensor from my breadboard. I don't own an oscilloscope :(
My High School teacher said to just use PWM, but as I searched for a solution to this error, I noticed hundreds of others have this exact same problem with all sorts of different sensors and loads with ICs; no one has come up with a general solution.
I've been obsessing with this for weeks and if anyone can help or can recommend a book about sensor schematics I would really appreciate it. Thanks!
Just guessing but I suggest you use a level translator. Also when you power off the sensor the serial output should go to ground disabling that channel.  Check your voltages and put a bypass cap on the transistor for the sensor. You can use a simple P channel Logic Level fet,only a 50 ohm resistor in the gate to prevent oscillation.  Your output will now be active low instead of active high. If you stay with transistors make R2 &amp; R3 510 ohm so you have enough drive for the transistor and be sure it is NOT a darlington device, if it is you will not have enough voltage for the sensor.