How to protect GPIO from back current? Diode?
Question
The OP is learning how to use Rpi to control a 12V DC motor using a 5V relay without any optical isolation.  To adjust motor speed and direction, He uses a PWM controller and a DPDT reverse polarity relay to switch the 12V power supply.
So far so looking good, but he has already fried two Rpi's!
Now the OP has two questions:
(1) That it might be the &quot;back EMF&quot; flash back current that fries his two poor Rpi's, and what sort of fly back diodes should be used to prevent frying his third Rpi?
(2) Or using an optocoupler might help to isolate any noises or spikes from going back to Rpi and cause trouble?

Short Answer



Note: The schematic v0.3, after a wild guess.
WARNING: I am just a friendly hobbyist. No guarantee no nothing won't melt down or blow up.

Flyback Diode Notes

/ to continue, ...

Long Answer

/ to continue, ...

References
(1) AOD4184A 40V N-Channel MOSFET datasheet - Alpha and Omega
(2) IRLR7843PbF IRLU7843PbF HEXFET (30V, 3.3mΩ, 34nC) Power MOSFET - IR (tlfong01's remark: doesn't seen like a Schotky flyback diode)
(3) AliExpress High Power 15A 400W MOS FET Trigger PWM Drive Module Control Panel Controller - US$0.50 tlfong01's remark: Just one &quot;channel&quot; unidirectional high current switch, nothing to do with PWM, though you can use 20kHz (Electromechanical relays can only do 10Hz) PWM signal to do the switching!
(4) IRLR7843PbF IRLU7843PbF HEXFET (30V, 3.3mΩ, 34nC) Power MOSFET - IR (tlfong01's remark: doesn't seen like a Schotky flyback diode)
(5) Flyback Diode Selection Guide 1/2 - tlfong01, rpi.org.forum, 2019mar06
(6) Schottky Diode Selection Guide 2/2 - tlfong01, rpi.org.forum, 2019mar06
(7) How to protect GPIO from back current? Diode? - that-ben 2020jul20
(8) AliExpress High Power 15A 400W MOS FET Trigger PWM Drive Module Control Panel Controller US$0.50
(9) How to wire a PPTC polyswitch + blown indicator LED to protect a bi-directional DC motor from stalling? - that-ben, Rpi SE 2018may01
(10) Rpi GPIO pin's High level signal possibly not High enough to switch off the 5V relay problem - that-ben, EE SE 2018may01
(11) How to properly use a relay module with JD-VCC from Arduino/Raspberry? - Carlos Garcia, EE SE 2020jun13

/ to continue, ...