Electronic – How to wire a Rpi, a relay, a DPDT and a PWM module to a DC motor, allowing to control its direction and speed

dpdtrelay

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??

Best Answer

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?

Thanks!