Esphome and mosfet switching

Just curious,
I have an ESP32 driven circuit that currently works using a 5v relay to switch power to a sensor. I am using a pinout that sends a high signal that switches this relay. I then have a deep sleep timer so when the deep sleep occurs the pinout signal goes low and no power is sent to sensor (saving power for a battery setup)
I thought maybe i could replace this relay with an n channel mosfet (P16NF06L) so that the relay led’s aren’t sucking power and I might save myself about 100-200 mA power.
I connected it all up and the circuit works.
However the deep sleep on the ESP32 has not sent a 0 voltage message to the pinout with voltage running through the mosfet continously once activated.
What code do I need to insert to have the pin voltage go to zero and close the gate?

yaml, logs and circuit diagram please?

The circuit diagram I have followed is this from Hummingbear. I know it is only a pic but essentially this circuit works great but I attempted to put a mosfet in place of the relay.

yaml is

  name: "maintankv2"
  platform: esp32
  board: nodemcu-32s
# Send power to sensor by enabling relay on boot
      - output.turn_on: water_sensor_power
      - delay: 6s

# Deep sleep
  run_duration: 4min
  sleep_duration: 2min
# Enable logging

  - platform: gpio
    pin: 19
    id: water_sensor_power
    inverted: false
# Enable Home Assistant API

  sda: GPIO21
  scl: GPIO22
  scan: true
  id: bus_a

- address: 0x48
  i2c_id: bus_a

- name: maintank
  platform: ads1115
  multiplexer: 'A0_GND'
  update_interval: 6s
  gain: 4.096
  unit_of_measurement: "V"
  icon: "mdi:gauge"
  accuracy_decimals: 3
  - median:
      window_size: 11
      send_every: 09
      send_first_at: 04
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
    ssid: "Esphome-Web-178238"
    password: "R4e6qBGLoo7Z"


I don’t have any logs that show anything to do with the gate remaining open. They just show the readings - but only when the esp32 is not in depp sleep. I have just measured the voltage over the terminals.
Using the relay switch the voltage is 0
Using the mosfet the voltage remains at the input voltage for the sensor - 15v. I expected it to drop to 0 when the esp32 went into deep sleep - the same as the relay.

The more I read about using mosfets the more confused I get. Maybe i should just stick with my relay that works and expect to use that extra 50mA in power. I have an expected run time of about 50 days on a 3.7v lipo battery. If I tie this with a solar charger then I reckon it would be an asteroid impact that stops my battery being recharged regularly.
The mosfet discovery tour can occur later. Don’t waste your time on this. Mosfets at the moment are a bit like smoke and mirrors. I know I’ve already got the smoke part with a wrong connection lead quickly turning into this.

Did you put a pull-down resistor to mosfet’s gate? If not this can happen. When ESP goes to sleep it’s pins are more or less “floating”. Mosfets with floating (or not connected) gate tend to remain it’s previous state. Try to use a resistor, say 10k (possibly more, less…try) from G of mosfet to GND


Yeah I put a 10k resistor between gate and source. Maybe I need to go higher?

Best is to try and see. If resistor will be to large then mosfet will stay (maybe only partially) open. Even better would be to use some mosfet driver. Running mosfet on 3.3V can cause mosfet not being fully open, thus heating will occur… if your mosfet will become too hot it means it’s not fully open.

1 Like