So far, so good. The problem is that when the relay and the lamp are turned on, the ADC reads a consistent +0.1V offset from the moisture sensor compared to the measure when the light is off, as visible in figure.
Cables connecting the sensor to the ESP are ~1/1.2 meters long and travel relatively close to the lamp and the entire system, so I would expect some power line noise, but I cannot figure out the origin of the consistent offset when the lamp is on. Also because the 230V powerline is constantely connected to the ESP via the 12V power supply to keep it alive. The only difference is the AC line traveling across the relay to turn on the light, and of course EM interference from the lamp. I also tried with several different LED lamps, same effect.
I tried also to connect the sensor to a completely separated ESP powered by a USB 5V charger and I don’t see this behavior (no offset with on/off light), but I tested with short (20cm) cables. I still need to test the circuit replacing the moisture sensor with a fixed resistor to confirm/exclude if the issue is due to the cheap capacitative sensor that interferes with the AC light.
I was wondering if a better external ADC, such as the ADS1115, could mitigate the issue or if it would be a waste of time and money.
I thought about the relay draining too much current and messing up with ADC reading, but without the lamp connected I cannot see the offset.
You can’t power the relay coil from esp32 gpio pin. Typical 5V power relay needs more than double the current esp can provide (and the voltage is incorrect as well). I’m very surprised that the relay even work. Your approach cause esp misbehaviour and eventually damage.
So how can anyone help here, if your circuit diagram presents something else.
Also, “similar” is not enough, post what you actually have. The relay module on your link is not compatible with esp (or any 3.3V board). What gpio you use for analog? Are you on esphome?
As I said, I tried to power the 5V for the relay with a stepper down from 12V to 5V, decoupling it from the ESP. The only “real” connection between the ESP and the relay is the signal pin of the relay module connected to one among all digital pins (I tried several, being caution with bootstrap pins).
So perhaps I need to use a transistor also for the control pin. I was naive because I use the very same relay module for a thermostat with which I had no issue in piloting the module.
What gpio you use for analog?
I tried all analog pins showed in the board datasheet (GPIO02, 04, 34, 35, 36, 39). Indeed I have three different sensors, all same behavior even trying one at the time).
Are you on esphome?
Yes.
Note: when the analog pins are floating (no sensor attached) I don’t see anything like the plot
As last naive thought: if the relay drains too much current, should I see a drop in voltage, instead of an increase, shouldn’t I? Or maybe I’m getting it wrong, maybe since the voltage reference drops, everything measured by the adc ‘seems’ higher when the relay starts drawing current?
Since you are using relay module, the current draw is likely not an issue (except if your power supply is underrated).
Your amazon link presents images of two different modules, the one with JD-VCC jumper is ok, the one without is only good for 5V board like arduino. You should use transistor to drive it with Esp.
But to verify what’s going on, disconnect the relay from esp and turn the light on/off manually to see how your analog readings behave.
Use only gpios 32-39 (adc1) for analog.