ESP32 smart plant box - AC light interference with ADC readings

Hello!

I built a DIY smart plant growbox based on an ESP32 composed of:

  • 230V LED lamp, controlled by a 5V/230V relay
  • DHT22 temperature/humidity sensor (not shown in figure)
  • Analog Capacitative Moisture sensor (v2.0, no brand) for measuring pot soil moisture
  • 230V/12V 2A (24W) power supply for LED strips to power the ESP32 and sensors

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.

Any help is of course really appreciated!

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.

Hey, thank you very much for your answer.

Actually the relay is a full module with its driver included similar to this one https://www.circuits-diy.com/wp-content/uploads/2023/04/How-to-use-a-5V-Relay-with-Arduino.png

I tried a 12DC to 5DC stepper down to supply the relay from a different line rather than directly from ESP’s VCC pin, but same result.

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?

Sorry, you are right, my bad for the over-simplified scheme that is more “logical” than realistic.

The relay is this one ARCELI 5PCS KY-019 5V One Channel Relay Module Board Shield For PIC AVR DSP ARM for arduino Relay : Amazon.co.uk: Business, Industry & Science

The relay module on your link is not compatible with esp (or any 3.3V board).

The ESP32 board should be able to supply 5V https://cdn.shopify.com/s/files/1/1509/1638/files/AZ280_A_20-6_EN_B08BV3L92G_0d23998e-8e81-4521-bc8a-08e9c049db70.pdf?v=1721128784 but of course no digital pin can erogate 5V to switch the relay.

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.