I have just finished soldering my "custom designed" ESP32 PCB board that I'd like to use for management of 6 x 24 Vac irrigation valves.

Given my limited electronics knowledge, for controlling the irrigation valves I wanted to "emulate" (if not copy) the schematics of one of those "5V relay boards" available for Arduino/ESP, such as this. In particular, I wanted to obtain opto-isolation, even though I have surely made a mistake as the GND of "ESP32 portion of board" and that of the "relay portion of board" is the same, so there is not opto-isolation in reality...nonetheless I got my PCB printed and delivered before noticing the mistake.

This is the schematic of the control circuit for each relay:

enter image description here

The components I have used are the following; on the "3.3 V ESP32 side":

  • 6 x PC817 opto-isolator
  • 6 x red LEDs
  • one OLED screen and a pushbutton (not represented in the schematic)

While on the "5 V relay side":

  • 6 x 2N3904 NPN BJT
  • 6 x SRD-05VDC-SL-C relays
  • 6 x 1N4007 flyback diodes

The resistors are as per schematic:

  • \$R_1 = 100 \Omega\$
  • \$R_2 = 220 \Omega\$

Each PC817 opto-isolator is controlled by a GPIO of ESP32. The ESP32 is powered at 3.3 V while the relay circuit is powered at 5 V. The board is powered by an external 5V power adapter with nominal output of 2A; 5V to 3.3V conversion is done on the board by a MP1584EN step-down converter such as this one.

I have tried breadboarding this circuit (actually for just 1 relay) before printing the PCB, and it worked. Nonetheless, now that I have soldered all the components to the custom PCB, no relay is switching.

I have a feeling I must have mis-sized one of the components, but I am at the limit of my electronics knowdledge. I would appreciate your ideas/suggestions on what I could try to make this work as expected.

Regarding the choice of resistors:

For the 3.3V side I have sized \$R_1 = 100 \Omega \$ so to get a forward current on the PC817 of \$I_f = 20 mA\$, which should lead to a voltage drop of around 1.3 V between pins 1 and 2 of PC817 according to its datasheet (this one). This leaves me with around 2 V to power the LEDs with 20 mA of current.

Regarding the 5V side: I consider that the SRD-05VDC-SL-C relay requires circa 90 mA of current, according to its datasheet (this one). This is basically the \$I_c\$ collector current I need on the 2N3904 NPN BJT. Since the BJT should act as a switch, I consider that the \$V_{ce}\$ of the NPN should be around 200 mV - having a look at Figure 16 of the datasheet of 2N3904 (this one) for a collector current of around \$ I_c = 100 mA\$, I assume base current \$I_b = 15 mA\$, which leaves me with a \$\beta = Ic/Ib = 90/15 mA = 6\$.

At pin 3 of PC817 I have measured a voltage of 4.5 V, according to 2N3904 the \$V_{be,sat} \approx 0.9 V\$, therefore

\$ R_2 = \frac{4.5 V - 0.9 V}{0.015 A} = 240 \Omega \$

which I have approximated to \$ 220 \Omega \$.


I am adding the full schematic for future reference. Full schematic As CL. and Justme pointed out (thanks again!), adding an external red LED in series with the opto-isolator LED is not a good idea, as available voltage is limited (only 3.3 V with ESP32) given the required forward current on PC817. Silly me to not take the voltage drop on \$R_1\$ into account in the first place... Replacing that red LED with a \$0 \Omega\$ resistor immediately got the circuit to work, even though there are other things that need improvement, as per CL. answer.

  • \$\begingroup\$ The relay itself will provide some degree of isolation, though they aren't always rated for it. The coil of a relay is isolated from its contacts. You should never rely on isolation that isn't rated, however, so if you need isolation for safety purposes adding additional isolation via an optocoupler or other isolator is recommended. Or using a relay with an isolation rating. (an aside: There is a difference between isolation and insulation, and you appear to be saying the latter while talking about the former.) \$\endgroup\$
    – Hearth
    Commented Oct 2, 2022 at 16:34
  • \$\begingroup\$ Thanks for your feedback, indeed I am not sure isolation is really that needed given my application but I was happy to learn something new. Also thanks for pointing out my grammar mistake, I have corrected "insulation" to "isolation" as I definitely meant the latter. \$\endgroup\$
    – mrcsrplln
    Commented Oct 2, 2022 at 16:44
  • 2
    \$\begingroup\$ Since it worked when you breadboarded it and doesn’t work when on the pcb you made, let’s get the pcb design added to this question as I expect that’s where the problem shall be \$\endgroup\$
    – Bryan
    Commented Oct 2, 2022 at 16:47
  • 2
    \$\begingroup\$ What do you measure with a multimeter at the GPIO output pin? Or voltage over the resistor? To me, it simply looks like a bad idea to put opto and red LED in series, driven from 3.3V. The output of ESP won't go down to 0V at 20mA as it is not infinitely strong, and components may have a lot of tolerance how much they need forward voltage to conduct. \$\endgroup\$
    – Justme
    Commented Oct 2, 2022 at 16:50

1 Answer 1

  1. The calculation of R2 is correct. But a typical β used for satured switching is 10 or even 20.
  2. The PC817's CTR (current transfer ratio) is somewhere between 50% and 600%. (Use a ranked device to get a narrower range.) So in the worst case, to get 15 mA through the output, you need to drive the input with 30 mA.
  3. With a current of 20 mA, the voltage drop over R1 would be 2 V. Together with the 1.3 V of the optocoupler's LED and the 2 V of the red LED, you'd have a total voltage drop of 5.3 V. In other words, the ESP32 will not be able to drive both LEDs.

If you remove the red LED, it will probably work. But you could just as well rip out the optocoupler and drive the transistor directly from the GPIO.

  • \$\begingroup\$ #3 made it! I am so silly, I didn't take into account the R1 voltage drop in my equation! I still remain puzzled as to why it works on the breadboard...? As for your other points, many thanks for your contribution, your suggestions will help me improve my design! \$\endgroup\$
    – mrcsrplln
    Commented Oct 2, 2022 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.