Motor driver not working, 2 channel relay and current sensor

I’m having an issue with a motor driver I put together and was wondering if there was anything obvious from the schematic. Basically, I recreated a 2 channel relay and added a current sensor to the 24v motor line. An esp32 controls two optocouplers, a separate 5v line is then supposed to control the two relays to allow the direction of the motor to be switched. On the motor line there is a INA169 current monitor and shunt resistor.

A couple of issues, when controlling the two relays, I hear them click and the LEDs flash but only momentarily. I would think if everything is working properly the LED should be steady on? Also, the optocouplers both seemed to be closed all the time? Whichever optocoupler is getting the 1.2v to control it has 4v across the collector/emitter. The other one with 0v on the anode/cathode still has 5v across the collector/emitter.

Schematic: https://i.ibb.co/bjbfQ4ws/Screenshot-2025-09-16-070418.png

Thank you

Relevant parts (with datasheets):
Optocoupler: BPC-817 ( B BIN ) American Bright Optoelectronics Corporation | Isolators | DigiKey

5v relay: ALQ105 Panasonic Electric Works | Relays | DigiKey

IINA169 current monitor: INA169NA/250 Texas Instruments | Integrated Circuits (ICs) | DigiKey

Where does the +5V on your schematic come from?
What is the difference between GND and GND_ESP?

The 817s cannot handle the current required by the relay.
You will a transistor to drive the relay.
If you don't need optoisolation, then this is all you need

1 Like

Where does the +5V on your schematic come from?

What is the difference between GND and GND_ESP?

I have a different controller to drive two of these motors that consist of the esp32 and a buck converter to bring the 24v down to 5v. The 5v line powers the esp32 and also goes directly to this driver (via a CAT6 cable as seen in the schematic).

The difference between GND and ESP_GND was just an attempt at keeping the ground separate for optoisolation? One ground comes directly from the 5 volt buck converter and the others from the ESP. A little too technical for me I'm not sure if it's implemented correctly or not lol

The 817s cannot handle the current required by the relay.

You will a transistor to drive the relay.

If you don't need optoisolation, then this is all you need

Ah, more to research/learn. Thank you!

I see no need for optoisolation, so GND and GND_ESP can be the same.
The 24V is essentially isolated via the relay, I think that should be good enough.

1 Like

If you don't need optoisolation, then this is all you need

image

image559×465 5.24 KB

Ok, a couple days later I’m getting a handle on transistors. I see you added a 560 resistor there, which could just be a copy/paste but I was hoping to run the numbers I got past you.

From the datasheets:

ESP32 output voltage: V(oh) = VDD x 0.8 = 3.3 x 0.8 = 2.64V

Collector Current: I(c) = (Red LED) 40mA + (5v relay) 80mA = 120mA = I(c)

Idk if it matters but this is with a PN2222A transistor:

Beta = 10 as a number to use for saturation rather than Hfe = 100.

I(b) = I(c) / Beta = 120 / 10 = 12 mA = 0.012A

V(besat) = ~0.825 at 120mA

Resistor = (V(oh) - V(besat)) / I(b) = (2.64 - 0.825) = 1.815 / .012 = 151 ohms

Does that sound right?

I guess mostly I’m asking to confirm I add the Red LED’s 40 mA to the collector current? Does the resistor on it change anything?

Other than that it really looks like a simple swap to a transistor correct? Some schematics show an extra 10K resistor to ground on the ESP output pin to make sure it’s really off when low.

> (Red LED) 40mA

A typical red LED has a Vf of about 2V.
So the current would be (5V-2V)/2.2K = 0.9mA
You may want to change the resistor to 1K, with the 2.2K it might be rather dim.

Calculating the base resistor for a transistor as a switch isn't all theory and does involve some "rule of thumb" approximations. You want to make sure that the transistor is completely in saturation so a good rule of thumb for the collector current is to multiply what you calculated by 5. Some people say 10 some 2, I use 5.

80mA for relay + 3mA for LED = 83mA
83mA x 5 ~ 400mA

ESP32 output voltage: V(oh) = VDD x 0.8 = 3.3×0.8 = 2.64V

That is with Ioh = 40mA and you will not be sourcing that much current, so you can assume it will be much closer to 3.3V. A good guess maybe 3.1V to 3.2V

Idk if it matters but this is with a PN2222A transistor:

Yes it does, I chose the 547 because of the higher Hfe so less base current but a PN2222A will also work. The data sheet I have for the 2222 only has typical values for Hfe and for collector currents in the 100mA range I would estimate Hfe ~ 50.

So Ib = 400mA/50 = 8mA
If you use Vbe(sat) = 0.9V then R = (3.1V-0.9V)/8mA = 275Ω

1 Like

Damn, I thought I was a lot closer! But thank you for taking the time to work through it, good to be able to back over the reasoning and learn from it.

You were.
As I pointed out much of this is not solely based on theory but comes with practical experience.

1 Like

Just updating one last time, I got the motors running. The transistors work perfectly with the relays…

The bigger issue though was misreading the datasheet (and schematic footprint) and wiring the relays wrong. :face_with_peeking_eye:

Turns out pin 2 is common. I assumed the black pin on the opposite side was common, not realizing the arrows had more to do with it. Whoops. Anyway, a couple of jumpers and cut traces and everything is running properly.

The ESP32 is responding slowly changing the motor directions but hopefully I can figure that out in the software.. Unless that’s where the base pull down resistor comes in and I need to add one of those?