I am currently using a Raspberry Pi to control the speed of a 12V DC motor and it works perfectly fine right now like this:

enter image description here

This works: I can start, stop and ramp up the DC motor speed. But obviously, it only goes in 1 direction and it cannot spin in the other direction unless I physically reverse the red/black wires at the output of the PWM module. I tried it and it works, the motor can spin in the other direction with -12V instead of +12V.

Then, I ordered a DPDT 8 pins relay (LY2NJ) because I saw the following YT video and that is exactly what I want to do: https://www.youtube.com/watch?v=xTGzcN8JrUk

This is how I connected everything, but I think something is off in the connections I made, because there was smoke when I connected the battery, so I immediately unhooked everything and I'm now asking for your expertise to help me find what's wrong. Note that the bottom relay enables the RPi 3.3V GPIO pin to trigger the 12V DPDT relay.

enter image description here

What's wrong with the connections I made? (DO NOT REPRODUCE THE ABOVE)

EDIT: WELP, after frying my older Raspberry Pi (It's now RIP instead of RPI, get it? Yeah it's not funny) I have followed @brhans advice to power the DPDT relay AFTER the PWM and also I'm freaking out about grounding to the RPi ever again, so I grounded all the relays to the -12V instead of to the RPi:

enter image description here

Do you think this will fry my second RPi or should it be good like that??

  • 1
    \$\begingroup\$ You should have connected the relay between the PWM module and the motor. \$\endgroup\$ – brhans Feb 16 at 23:07
  • \$\begingroup\$ Why? And also, if it doesn't work when the relay is before the PWM, why would it work when it is after the PWM? \$\endgroup\$ – that-ben Feb 16 at 23:09
  • 1
    \$\begingroup\$ Because your PWM module expects and needs a particular polarity on the supply you feed it - you reversed the polarity, so the smoke came out. It doesn't care what you do between the module and the motor - that's the same as just swapping the connections to the motor. \$\endgroup\$ – brhans Feb 16 at 23:15
  • \$\begingroup\$ The PWM board may require a certain polarity power input. With the relay where you show it, the relay reverses the polarity to the PWM board. If you put the relay between the PWM board and the motor, you are only reversing the polarity to the motor, and not affecting the PWM board. \$\endgroup\$ – Peter Bennett Feb 16 at 23:16
  • 2
    \$\begingroup\$ If the smoke got out it's probably dead. There's only a certain amount built in to each circuit and letting even a little out can cause it to fail. \$\endgroup\$ – Transistor Feb 16 at 23:27


simulate this circuit – Schematic created using CircuitLab

It was a leap of faith to add PWM to the video you saw which was incorrect for unipolar PWM.

Shielded twisted pairs will help reduce the likely EMI from the motor to RPi.

The motor must be stopped before changing directions and PWM set to 0.

A full-bridge with PWM is a better way with a CNC bridge for Arduino or RPi.

This is just a conceptual solution and motor DCR and power ratings must be considered with load inertia, speed, & acceleration needs.

| improve this answer | |
  • 1
    \$\begingroup\$ In an ideal world, I would build such a thing, but let's hypothetically think about making it "work" for now with what I have pictured in my Q (not ideal, I know!) and yes, thank you for pointing out the EMI and indirect factors to consider. I fried the first Raspberry Pi probably because the PWM was grounded onto the RPi. I have now grounded every PWM, relay modules and the motor's DPDT relay to -12V instead of onto the RPi. I think when the RPi fried, the PWM also fried, because no matter what I do, it never outputs any voltage :( I'll order another one. \$\endgroup\$ – that-ben Feb 17 at 2:25
  • 1
    \$\begingroup\$ The PWM must be grounded to the RPi unless you have optical isolation, as it is the control return path as I ilustrated in my schematic. \$\endgroup\$ – Tony Stewart Sunnyskyguy EE75 Feb 17 at 2:27
  • 1
    \$\begingroup\$ There PWM WAS grounded to the RPi and it fried itself and the RPi. Wouldn't grounding it to the -12V battery terminal be better? \$\endgroup\$ – that-ben Feb 17 at 2:55
  • 1
    \$\begingroup\$ All +3 MUST share ground as the relay reverses V+ and gnd(V-). The motor current must not share logic return paths to battery. Sorry I did not show that clearly. \$\endgroup\$ – Tony Stewart Sunnyskyguy EE75 Feb 17 at 3:06

OK I was drunk yesterday. Thank god my 25Ah (yes, you read that correctly) lithium battery did not explode. I wired the relay wrong! That is why the RPi fried.

Today, I unsoldered the mess I did yesterday and rewired the relay as it should have been to make it a reverse polarity switch. For any future noob (hopefully not as much as I am) coming here to look up the way to wire a LY2N-J DPDT relay for reversing polarity, THIS is the correct way AND NO SMOKE THIS TIME :)

enter image description here

I also tuned some other things as well, while I was at it, such as replace the relay for a true 3.3v one, which is less prone to sticking. I also connected every single 12V relay, PWM and DC motor to the battery's -12V "ground" instead of the RPi's ground, because I'm freaking out due to what happened yesterday. I still would feel better protecting the PWM signal pin with a diode, which I don't have right now.

BONUS: Anybody knows what extremely fast inverse polarity recovery diode I could order to protect the GPIO port "just in case" the PWM decides to short to the signal pin?


| improve this answer | |
  • 1
    \$\begingroup\$ You need to connect the Pi's Ground to the 12 V Ground, to provide a return/reference for the signals from the Pi to the relay and PWM boards. \$\endgroup\$ – Peter Bennett Feb 17 at 21:44
  • \$\begingroup\$ I kind of can't right now, because I don't have any diodes on hand yet. So at the moment, I keep all the logic (RPi) grounds together (many many more 3.3v sensors are not shown above) and all the 12V related modules grounds together JUST IN CASE, because I wouldn't want any other RPi to fry. I will order some diodes, but I'm evaluating what my diode options are for that project right now. I need some extremely fast recovery diodes and ones that don't drop voltage much, since many of those diodes will be used to protect 3.3v GPIO ports... 500mA seems like a huge voltage drop IMO! \$\endgroup\$ – that-ben Feb 17 at 21:56


How come my advanced motor circuit keeps frying Rpis?

pwm motor


(1) @tlfong01 is casually refining and drawing the circuit without prior approval by @that-ben.*

(2) The Rpi's PWM signal with duty cycle 0% might actually cut of the 12V DC motor power. In other works, the first stage 5V relay switching on/off 12V motor power supply might not be necessary.


Well, there are many causes frying the Rpi's.

Perhaps we can redraw the circuit diagram showing the power, signal, and ground lines.

One ounce of prevention is better than one pound of cure. You might like to try the following prevention tricks:

(1) Tidy up the 12V motor power ground and Rpi signal ground lines so that they don't stick or share common segments. Add ground stubs freely.

(2) Use optical isolation and better still "Total Optical Isolation" to "completely" separate signal and power grounds.

/to continue, ...

Discussion Recommendation

(1) Use H-bridges instead of DPDT relays. Apopular choice is L298N, heavy current H-bridge include BTS7960B.


Part 1

/ to continue, ...

Part 2

(1) BTS 7960B High Current PN Half Bridge Product Brief - Infineon

(2) BTS 7960B Half Bridge Datasheet - Infinion

(3) AliExpress H-Bridge Drivers

(4) AliExpress Dual BTS7960 43A H-bridge Driver Module - US$4

(5) TaoBao Dual BTS7960B H-bridge 43A 25kHz PWM DC/BLDC/Stepper Motor Driver Module - ¥22

(6) BTS7960 43A Motor Driver Arduino Instructable - Mohannad Rawashdeh, 157,992 Views

/ to continue, ...


Appendix A - Old design notes

hardware pics

old design

flyback diodes

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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