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.