ESP32 goes crazy when the pump is turned on

Hi everyone, I’ve been playing with ESP32s for a short while, but I’ve learned in a short time.
I’m designing a small irrigation system for my plants, before assembling it I’m trying everything on a BreadBoard, and everything seems to be going well, except when I turn on the pump, in that case the ESP32 goes crazy randomly turning the relay and LEDs on and off, and I find the message in the log: “[dht:174]: Requesting data from DHT failed!”
The whole system is powered by 5v with a MeanWell DR-15-5 power supply, the voltages are stable and perfect.
If I try with a motor like this → Mabuchi Motor RF-300 then the problem doesn’t occur, even with 3 of these turned on at the same time.
I can’t understand why it happens and how to solve it.

This is my esphome yaml:

esphome:
  name: "irrigatore"
  friendly_name: Irrigatore
  area: Home

  on_boot: 
    priority: 800
    then:
      - switch.turn_off: Pompa
  
  on_shutdown:
    then:
      - switch.turn_off: Pompa

esp32:
  board: esp32dev
  framework:
    type: esp-idf

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "NB+M8OzBcEpB0MA2Tl7jhNjD/qzlPlc77z5nkjjCH2k="

ota:
  - platform: esphome
    password: "c06bf487b779e1626a91b3d5a5b279df"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  enable_btm: true
  enable_rrm: true

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Irrigatore - Fallback Hotspot"
    password: "Qhckalnovka3"

captive_portal:

time:
  - platform: sntp
    id: my_time
    timezone: Europe/Rome

binary_sensor:
  - platform: status
    name: "Irrigazione - Stato Rete"

#Pump button
  - platform: gpio
    pin:
      number: GPIO33
      inverted: true
      mode:
        input: true
        pullup: true
    name: "Pulsante Pompa manuale"
    on_press: 
      then:
        - switch.toggle: Pompa
        - light.toggle: led_on
        - light.toggle: led_off
        - logger.log: Pompa in modalita' manuale
  
  - platform: gpio
    pin:
      number: GPIO17
      inverted: false
      mode:
        input: true
        pullup: true
    name: "Recipiente acqua"
    on_press: 
      then:
        - light.turn_on: led_on
        - light.turn_on: led_off
        - logger.log: Bidone dell'acqua vuoto

status_led:
  pin: GPIO2

#Pump Relay
switch:
  - platform: gpio
    name: "Pompa"
    pin:
      number: GPIO32
    inverted: true
    icon: "mdi:sprinkler"
    id: Pompa
    restore_mode: always_off

#Pump LED
output:
  - id: ledon
    platform: gpio
    pin: GPIO16
  
  - id: ledoff
    platform: gpio
    pin: GPIO4

light:
  - platform: binary
    id: led_on
    name: "LED Pompa ON"
    output: ledon
    icon: "mdi:led-on"
    restore_mode: always_off
  
  - platform: binary
    id: led_off
    name: "LED Pompa OFF"
    output: ledoff
    icon: "mdi:led-off"
    restore_mode: always_on

sensor:
  - platform: wifi_signal
    name: "Irrigazione WiFi Signal"
    update_interval: 30s
    id: wifi_signal_db

  - platform: copy
    source_id: wifi_signal_db
    name: "WiFi Signal Percent"
    filters: 
      - lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
    unit_of_measurement: "%"
    entity_category: "diagnostic"
    device_class: ""
  
  - platform: uptime
    name: "Irrigazione Uptime"
    id: uptime_sensor
    update_interval: 60s
  
  - platform: internal_temperature
    name: "Temperatura del sistema"
    icon: "mdi:thermometer"

#External temp sensor
  - platform: dht
    pin: GPIO25
    temperature: 
      name: "Irrigazione Temperature"
      icon: "mdi:thermometer"
    humidity: 
      name: "Irrigazione Humidity"
      icon: "mdi:water-percent"
    update_interval: 30s

#Soil humidity sensor  
  - platform: adc
    pin: GPIO35
    name: "Soil Humidity Sensor"
    id: soil_humidity
    accuracy_decimals: 0
    update_interval: 5s
    icon: "mdi:flower"
    device_class: "moisture"
    unit_of_measurement: "%"
    attenuation: 12db
    filters: 
      - calibrate_linear:
          - 2.25 -> 0
          - 0.97 -> 100
      - lambda: |
          if (x < 0) return 0; 
              else if (x > 100) return 100;
                  else return (x);
text_sensor:
  - platform: version
    name: "Irrigatore version"

  - platform: wifi_info
    ip_address: 
      name: "Irrigatore IP"
      update_interval: 60s
    bssid: 
      name: "Irrigatore BSSID"
      update_interval: 60s
    ssid: 
      name: "Irrigatore SSID"
      update_interval: 60s
  
button:
  - platform: factory_reset
    name: "Restart with Factory Default Settings"
  
  - platform: restart
    name: "Irrigatore Riavvia"
  
  - platform: shutdown
    name: "Irrigatore Spegni"
    icon: "mdi:power"
  
  - platform: safe_mode
    name: "Irrigatore Safe Mode"

Which relays are you using?
How do you connect them?
Is it possible that the pump motor disturb your WiFi?

I’m using a relay like this → Arduino Relay
The relay is powered directly from the power supply, the pump is connected to the NC contact of the relay and the COM is connected to the power supply.
I also tried powering the pump directly from the power supply, without using the relay, and the result is the same, so I thought that the motor emits some kind of noise, but I’m not sure, I also tried with another ESP32 that I’m using elsewhere, temporarily flashing the firmware of this one, and the result is the same.

From what power supply 5V or 3.3V?
If 5V do you protect the esp from the 5V input?

Try to separate the power supply from the pump ans esp more and put the esp further from the motor.
I use a WT32-eth001 for my pool pump with the same relays but power supply from the WT32 is 30m away and I split the VCC and the JD-VCC
JD-VCC => 5V power supply
VCC 3,3V from the ESP

My guess (from doing pretty much the same with solenoids) is that you need a “flyback diode” (google it) across the pump terminals.

1 Like

Flyback diode and small capacitor across pump terminals. I would use separate psu for pump and for esp32. No common ground.
Take care of correct wiring of your relay module, 5v to jd-vcc and 3.3v to vcc, jumper removed.

The relay is powered by 5v directly from the psu terminals, only the signal wire to the GPIO is connected to the ESP32.
I just tried to run the motor using a slightly longer cable, same problem.
Using a USB power supply to power the ESP separately with the sensors and relay worked, but I want to use only the MeanWell to make it more efficient.

Don’t do that, you have now +/-5V on your ESP!
Remove the jumper as we already mentioned.

See:

So I can power the relay like this.
JD-VCC → +5 PSU
VCC → +3.3 ESP
Does VCC still work at 3.3v even though it should theoretically work at 5v?
To solve the pump problem, the diode seems like a good solution, but I have no idea which one exactly to use or how to connect it.

I measured the voltage between GND and IN and it is 2.30v
Shouldn’t it be safe?

That protects the relay module. Not the ESP.

Without a SCHEMATIC of how you have connected everything, we are all just guessing. But your original question screams: power.

The schematic doesn’t have to be elaborate, pencil and paper is just fine. Just make sure you label all of the connections.

Not if everything is powered from the same power supply, as stated in the original post.

Vcc is sinking to esp gpio trough optocoupler. Only 3.3v to vcc and gpio to INx is required on Esp side. Jumper removed.
Whatever setup other than this is incorrect.

On the other side (relay coil) 5v to jd-vcc and gnd to gnd.

1 Like

This is the scheme, now modified as in the image in the next post.

So it should be like this, right?

Looks OK for me now.

1 Like

Now there remains the problem of the pump that drives the ESP32 crazy, it is as if the pump is sending interference through its cables.
Would the 1N4001 diode in parallel with the motor be okay?

As mentioned above by me and others, with that setup your pump requires a flyback diode.

1 Like