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:
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 \$.
UPDATE
I am adding the full schematic for future reference.
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.