I'm attempting to control a 24V solenoid using a Raspberry Pi and I'd like to know the best method of shielding the Pi from the circuit (so the GPIO doesn't get burnt out).

This is what I have so far:

Parts list:

Raspberry Pi 24V Solenoid circuit

  1. Do I need to add anything extra to ensure that the 24V circuit doesn't affect the Pi in any way?
  2. Based on this similar circuit, is it advisable to add a diode between the positive and negative connection on the solenoid to prevent a "push-back" of current when the solenoid snaps back? If so, how do I determine the correct direction of the diode seeing as both solenoid wires are black? mosfet circuit

2 Answers 2


See See http://elinux.org/RPi_GPIO_Interface_Circuits for background.

You do not need to do anything to protect the Pi, but it is always advisable to use a flyback diode in any inductive circuit. Here it is to protect the MOSFET if it doesn't already have one fitted.

I strongly recommend you DO NOT put the diode on the solenoid. If you plugged it in the wrong way you would cause major damage. Fit the diode on the driver board (it is to protect the MOSFET, so should be with it). The link above shows correct orientation if you are unsure.

To be specific is connected the wrong way you would have a forward biased diode as load, so a massive current would flow through the MOSFET, almost certainly disintegrating it and the diode.

  • Thanks for the clarification. Can you elaborate a bit on what "major damage" would be caused by fitting a diode between the solenoid terminals? The second circuit example I've shown does this, so I'm just wondering why it would be advisable not to?
    – Axel
    May 22, 2017 at 14:35

If you want to be sure you can include a optocoupler between the RPi and the driver. The optocoupler uses light to send a signal to the second circuit (fotodiode). In this way there is no electrical connection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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