Driving relay with ESP32 DevKit V1

I’m working on a project to drive a 12V 40A automotive relay using an ESP32 DevKit V1 through a PSMN4R3-30PL MOSFET but it’s not activating and I’m not sure where I went wrong.

In lieu of a schematic, I’ve got:

  • +12V going into one side of the relay coil
  • +12V to the VIN on the board
  • GND on the board to GND
  • gate to one of the output pins of the ESP32 board with a 10k resistor to GND
  • drain to the other side of the relay coil
  • source to GND

The intent is that when the ESP’s output pin is pulled high the relay is activated. Instead, the relay is activating as soon as power is supplied to the circuit no matter which way the ESP pulls the pin.

Is there something I’m missing? I’m very new to building circuits and couldn’t find a ton of helpful documentation online so please forgive any ignorance

A schematic would definitely help here. Make sure your MOSFET isn’t facing the wrong way, i.e. the drain and source aren’t swapped.

The threshold voltage of the MOSFET and the connections seem approximately correct in my head but a drawing would help make sure you’re not misinterpreting something.

2 Likes

Sounds reasonable, but difficult to be sure without a sketch.
You also need to put a diode across the relay coil. Make it so it doesn’t conduct when the relay is on. It will prevent the energy stored in the coil current from frying your mosfet!

2 Likes

Here’s a rough sketch of the circuit. I believe I’ve got the polarity correct on the MOSFET connections.

I can certainly put a diode on the relay side. This would be called a “fly back” diode right? How do I determine what the ratings of the diode should be in this case?

1 Like

+1 to the recommendation from @Weldingrod1 to add a diode, say a 1N4001, with the cathode (banded) end connected to the end of the relay coil that is connected to +12V. It is best if the connections between the diode and coil are relatively short.

What is the part number for the MOSFET you are using? You should find the manufacturer’s datasheet to get the proper pin assignments. If you bought it from DigiKey, their webpage for the part will have a link to the manufacturer’s webpage or datasheet.

Are you showing the physical arrangement of your connections to the MOSFET? All the 3 pin board mounted power MOSFETs I have used have the pin assignment G D S. There is a chance the MOSFET is damaged though most should survive having 12V applied to their Gate.

1 Like

Thanks for the diode recommendation. I’ll pick up some of those and add them on the relay side.

The MOSFET is a PSMN4R3-30PL (datasheet) and it is configured G D S. For simplicity’s sake my sketch showed D G S but I am certainly not applying +12V to the gate hahaha.

Almost any normal diode will work there. A 1 Amp diode rated for at least 30 Volts will do it.

2 Likes

The flyback diode must be rated for at least 12V. Your bog-standard leaded 1N4001 is rated for 50V and should work fine.

1 Like

Your circuit should work. It is possible that the MOSFET is damaged. If you have a DVM you can do the following tests:

  • Set the DVM to the diode test setting.
  • Connect the red lead to the Drain and the black lead to the Source. The DVM should read open circuit.
  • Connect the black lead to the Drain and the red lead to the Source. The DVM should read somewhere between 0.5V and 1V.
  • Connect the red lead to the Gate and the black lead to the Source. The DVM should read open circuit.

If you have 2 DVM’s, you can do the following tests:

  • Disconnect the MOSFET from the circuit.
  • Set both DVM’s to measure resistance.
  • Connect the red lead of one DVM to the Drain and the black lead to the Source. The first DVM should read open circuit.
  • Connect the black lead of the second DVM to the Source and the red lead to the Gate. The second DVM should read open circuit and the first DVM should change to read some very low resistance, below a few Ohms.
  • Connect the red lead of the second DVM to the Source and the black lead to the Gate. The second DVM should read open circuit and the first DVM should change to read open circuit.
4 Likes

Im a little bit confused about whats happening on the left side of your circuit, mainly how the drain of your circuit connects to your device and 12V. I believe your trying to make a level shifter? Have you tried simulating it in LT Spice yet?

The coil/spiral-line-switch thing on the left side of the sketch is the symbol for a relay. OP wants to use the GPIO pin of the ESP32 microcontroller to drive a MOSFET to drive a relay to drive whatever device. The two connections on the top connected to the switch-part of the symbol, “+12V” and “to 12V device”, would be the common and normally-open/-closed of the relay, and should only really be able to interact with the rest of the circuit by causing issues with the power supply.

@mrEkko I don’t think I saw you mention it, have you tried testing each part of the circuit separately? philso gave a fairly detailed walkthrough for testing the MOSFET. For the relay, it should be as connecting the coil directly to the power supply and ground and verifying that the connectivity is changing between the common and normally-open/normally-closed terminals (and probably also an audible click). For the ESP32, you could either measure the output of the pin with a voltmeter/multimeter or just connect an LED (and appropriate resistor). You also mentioned a 10k resistor from the MOSFET gate to ground, but it is not present in the sketch. As described, I don’t think it would cause any problems, but it might be good to verify that on the sketch.

If you’ll allow me to wax educational a bit, I have some other suggestions/information. Even if it isn’t directly helpful, it might benefit someone stumbling across this thread later.

You might also want to add a resistor in series between the ESP32 pin and the gate of the MOSFET. A MOSFET gate acts like a small capacitor, and the current immediately after the ESP32 pin is turned on may be quite a bit higher than is really safe for a microcontroller if directly connected. I think something like 300-1000 ohms looks right for this application (3.3 V / max 12 mA GPIO current). Going higher is safe too, I think even something like 100k there is still on the order of a few milliseconds of switching time on the MOSFET, discounting the other pulldown resistor. During switching is generally the worst time for heat generation, but you are only driving a small load, a relay coil, and you are not switching rapidly/repeatedly, like for a PWM driver, so I don’t think it’s really a concern at all.

Regarding the diode: the relay coil, like any coil of wire, will have some inductance. Basically, it really wants to keep the current running through it constant, and will produce whatever voltage necessary to try to resist that. Now, in the real world, there are a lot of effects limiting that, like internal resistances and non-zero switching times, and the amount of voltage corresponds to how quickly the current changes, so the actual timeframe where you might have problematic voltages is very short (but possibly still long enough to do damage).

Let’s pretend for a moment that we have an ideal circuit with just a voltage source/battery, inductor, resistor, and a perfect, no-delay switch all in series. We start with the switch open and no current running through the inductor. When we close the switch, at that exact instant, the inductor only needs to produce a voltage equal and opposite to the battery to hold on to that zero-current situation. In producing that voltage though, it lets the current change over time, and eventually the current in the circuit will level off to just the battery voltage and resistor, with the inductor acting like any old piece of wire. When you open the switch is where things get really crazy though. The inductor wants to keep that same level of current flowing, but the resistance in the circuit is now equal to the resistor plus the air gap of the switch (also a bunch of other messy real-world stuff going on, but close enough for demonstration). The battery certainly isn’t going to be pushing the same amount of current through an open switch, so just for an infinitesimal moment, the inductor may be producing thousands of times as much voltage, or even more, to make that happen. Obviously, that may be bad for some other electronics that might be connected in a more functional circuit. Here is a really nice interactive simulation of this effect with actual values.

As it turns out, one easy way to protect the rest of the circuit is just to add a single diode, “backward” and in parallel with the inductor. When the switch is closed, there is no current trying to flow forward through the diode; the voltage on the battery-positive side of the inductor is higher than the battery-negative side (inductor voltage opposes battery voltage). When the switch is opened, the voltage on the inductor changes direction, but now the voltage on the “input” anode side of the diode is greater than on the “output” cathode and current will flow through it. Since there is this “easy” way for current to continue flowing through the inductor (while decreasing, since there is a voltage difference required to go through the diode), there is no reason to try to get current through the “hard” way and no huge voltage spike. Here is the same circuit with a freewheeling/flyback diode added. The most that the diode should need to survive is a forward current equal to the relay coil current and a reverse/breakdown voltage equal to the supply voltage, neither of which should be hard to do.

I hope this helped. Please let me know if I should explain something differently or expand on anything.

2 Likes

I don’t have the testing tools required to carry out all the steps, but I swapped out the MOSFET I was using on the breadboard with another one and it all magically worked.

So, problem solved!

This was incredibly helpful and taught me why the flyback diode is necessary. I will also be taking your advice, I’ll add a resistor between the ESP32 pin and the MOSFET gate.

Thank you everyone for the replies and assistance!

The first set of tests I described verifies that the body diode in the MOSFET is working. The MOSFET could still be bad. If this test fails, the MOSFET is virtually guaranteed to be bad.

The second set of tests I described actually turns the MOSFET on and off using the second DVM. You may want to try it on the damaged part and any spares you have.

The datasheet for the MOSFET you are using shows some switching characteristics measured with an external series gate resistor RG = 4.7 Ohms. The manufacturers I have spoken with generally recommend not using an external series gate resistor much over 10x the value shown on the datasheet.

If the series gate resistance value is too high, the Miller Capacitance (between the drain and gate) can cause the MOSFET to oscillate. Sometimes, this can lead to destruction of the MOSFET.

It isn’t necessary to worry about the switching losses in applications such as yours since the MOSFET changes state so infrequently. One only has to worry about switching losses when the MOSFET is changing state in the kHz range. Using series gate resistance values as high as has been suggested will actually cause more heating due to switching losses, especially if the MOSFET oscillates for some time after each change of state. In general, one should be using the lowest series gate resistance value. I have fixed many circuits where my predecessors used values that were too high.

2 Likes

Mosfet gates are incredibly easy to fry… it’s literally part of how they work :wink:
A gate resistor is 99% likely to be a good thing. Even 100 Ohm helps… plus, it makes a handy jumper wire

1 Like

It looks like updated versions of the ESP32 datasheet list a maximum pin source current of 40 mA, though with an output voltage of only 2.64 V. In order to get that, you have to make sure you are using a digital pin in the VDD3P3_CPU or VDD3P3_RTC power domains, which are listed in the IO_MUX table on page 62 of the datasheet. You will also have to reconfigure the output drive strength of the pin above the default value of 20 mA, though I don’t know how that configuration is actually implemented physically. Taking that max current and nominal output voltage would give 82.5 ohm, while assuming the minimum specified output voltage at that current would give 66 ohm. 2.64 volts is above the maximum threshold voltage of the MOSFET, but it is below the typical plateau voltage, and I don’t know how that might affect the dynamics. I do not find any information on higher acceptable pulse currents for the ESP32’s GPIO pins, nor any voltage/current curves. Were this a project I was making, I don’t know that I would want to go for less than 150-180 ohm between the ESP32 and a MOSFET gate. Ultimately, an ESP32 is not a gate driver, and these two parts may not be ideally suited to work together in this configuration. A smaller MOSFET, a BJT, a Darlington transistor, adding an optoisolator, or something else might be a better fit. I’m not sure which value “as high as has been suggested” was referring to, but 100k was not a practical suggestion, but rather just seeing how an extreme value might affect some things.

1 Like

The OP has a 10 kOhm from the gate to ground which will draw in the order of 300 uA when the output is in the Hi state. Any logic output that is working properly will drive this low load current virtually to the supply rail voltage.

The gate of a MOSFET looks like a capacitor to the outside world. When the digital signal driving it changes state, there will be an initial current spike. The current then decays exponentially to near zero. This decal will be very quick since the gate capacitance is only 2.4 nF and the gate charge is only 8 nC. Due to the current dependent output resistance of the output MOSFETs in the logic gate driving the MOSFET gate, peak amplitude of the current spike is never what one expects. While the MOSFET is on, the gate current is just a leakage current that most people do not have the ability to measure. Thus the average current out of the logic gate is very small.

A logic output is perfectly able to drive the gate of the MOSFET the OP is using. They were designed for such applications and do not require extra driver transistors, MOSFETs or opto-isolators for the power levels the OP is working with.

My coworkers and I have used circuits like this in the circuit boards I design in our day jobs successfully for years for applications just like what the OP is trying to make.

2 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.