# Relay control using ESP32 via a transistor

I have an ESP32 powered by a DC 5V 1A power supply. I want to control a Honga HF32FV-G relay using the ESP32 via a J3Y S8050 transistor.

I have connected everything and it works as expected, but I want to know what the correct value of R1 should be or how to calculate it. I have tried values ranging from 470 ohms to 5.1K, and all seem to work fine, but I want to know the calculation formula.

P.S. - I am following schematics and tutorials from the internet. I am not an expert by any means

When you bias the transistor's base with a Current, you basically tell the transistor how much current it allows to pass from its collector to its emmitter. You need to know the $$\ h_{FE} \$$ or $$\ β \$$ or current gain of the transistor as its called to calculate these. It is usually ~10 or ~100, but you can check this out in the transistor's datasheet.

In your case, I assume the "Relay" pin biases with 5V the R1, then The current flowing from R1 to GND is $$\ \frac{5V}{4k7}= 1mA\$$. And in your case the transistor $$\ β \$$ is around ~300 based on the plot(s) and table in the datasheet:

So the maximum alloable current from collector to emitter through the transistor will be $$\ 1mA * 300 = 300mA \$$. Of course, if the relay's resistance from pin 1 to pin 2 has such a large resistance that wont allow 300mA to pass, then lower current will flow.

To calculate the resistor R1, you should look at the datasheet of the Relay, check how much current it needs to pass from pin 1 to pin 2 in order for it to turn on. Check for Coil data:

Check for coil infomation/data in purchase options:

You should buy the 5V type since you want to power the coil from 5V, And it will draw $$\ \frac{5V}{55Ω}=90mA \$$ at 5V. So the 4.7k should be more than enough, but you should check that after you solder the final PCB. Check that the transistor is not overheating, that the current that passes through the transistor is indeed around 90mA.

If you choose a really low value resistor (say you even use 0 Ohms for R1, this will short the "Relay" pin to GND -with a 0.7V drop in Vbe-), so you definitely want a resistor value there that wont draw much current from your ESP. And you dont want to use a really large resistor that will block the least current needed (~90mA) to turn on the Relay.

• Thank you very much for the detailed answer. Yes, I am using a 5V relay only, and I apologize for not providing much information about the relay. Yes, I have kept it on for a while, and it doesn't heat up. However, I haven't checked the current. By the way, which value would you choose? Nov 9, 2023 at 20:20
• I would choose a value that would draw from my MCU some current, about 2mA. (R=5V/2mA=2k5R). This will ensure that 1) the resistor is not so low that it will draw too much current from the MCU. 2) the resistor is low enough that it will always let enough current for the coil to flow. 3) Using large resistor values will make the Vb weak and it will be sensitive to Electromagnetic noise, and that way the relay might turn on/off on its own, in the presence of EMI. Nov 9, 2023 at 20:25
• Thanks, i'll go with a 2k7R then. Nov 17, 2023 at 22:04