I would like to create a lock that can be operated via Wi-Fi.

To do this I decided to connect the following 12V solenoid electric door lock to a Raspberry Pi.

However, I am not sure of the correct way to connect this to the Pi.

From the image I see two wires, so I assume the way that it operates is by simply putting current though it. However my concern is that it operates at 12V, while the Pi Zero operates at a lower voltage. I'm also unsure as to how much resistance I should add as well, or if there is any other issues that I'm not aware of.

So my question is, What is the correct way of connecting this device to the Raspberry Pi, (note I am using the Raspberry Pi Zero).

  • 1
    here is an example of connecting such a thing to an ESP8266 - same 3.3v considerations with that - so the schematic should help May 12, 2016 at 1:10
  • 1
    A technicality; we don't put current through things, things draw current from a power source. An important distinction though because a solenoid will try to draw a lot of current through a Raspberry Pi, sometimes more than it can safely provide :)
    – NoChecksum
    May 12, 2016 at 3:45

1 Answer 1


One does not simply connect something directly to the Pi's GPIO pins.

Your solenoid will draw too much current from the GPIO pins. So, no. Your solenoid won't even move and your Pi will be fried if you connect it directly to the GPIO pins.

As JaromandaX pointed out, you should use a MOSFET. Activating the MOSFET's gate pin will let power through. The Pi simply cannot power a solenoid directly. A better way would be to let the Pi control a switch that controls the solenoid, the switch being the MOSFET.

Oh, and don't forget the diode in the circuit, else your MOSFET will release the magic smoke or explode/catch on fire (the former being much more likely), possibly taking the Pi with it.

Take note that you're not limited to a MOSFET. You could also use a relay or something else.

  • Yeah, the sommat else could easily be a NPN bipolar semiconductor device also known as a transistor! A common-emitter configuration (though if it is a really high current solenoid you might want to consider a Darlington-pair in order to get the current gain wanted) is the thing to use, the reverse connected diode to give protection to those bits of silicon from the nasty voltage spike (can be hundreds of volts!) that you will get from the solenoid as a feature of its inductance when you decide to stop pulling current through it is still going to be needed....
    – SlySven
    May 16, 2016 at 13:47
  • @SlySven The problem with a transistor is that the gate generally needs current, not voltage. MOSFETs only need voltage. The Pi's GPIO pins are okay with providing voltage but can only supply a limited current (per pin and overall). You are correct with the diode part. The term for it is a flyback diode, clamp diode, etc. It needs a really high voltage rating (bigger inductor = higher voltage)
    – Aloha
    May 16, 2016 at 14:05
  • Um, for gate you mean base don't you? 8-) - and the current gain requirement needed is why I suggested a Darlington-pair arrangement would be best - with a (large) signal current gain of say 100 per transistor that configuration will square the individual gain. For example a GPIO pin sourcing current via say a 2.2K base-series resistor into that pair the maximum base current will be around ( 3.3 - 2 * 0.7 ) V ÷ 2200 Ω = 0.8 mA and multiplied by 100² that can switch over 8 Amps...
    – SlySven
    May 16, 2016 at 22:24
  • Also consider adding an LED and accompanying resistor to indicate the GPIO pin state - I am building this sort of functionality into my Home Automation project but as I have a Winkhause AV2E bluematic lock that needs a "volt-free" 12V switched input I'm using a relay that I can switch to the 12V supply I have (a 13.8V lead-acid battery connected to a permanently connected suitable battery charge - feeding a pair of 3.5A adjustable buck converters: one to provide a 5.2 volt output to an RPi UPS unit feeding the RPi and associated bits; the other the 12V 2A the door lock needs and my relay)...
    – SlySven
    May 16, 2016 at 22:42
  • ... for security and to prevent RPi lock-ups unlocking the door unexpectedly I am actually using two GPIO lines which must BOTH be driven to opposite states and to the opposite voltages that they assume by default on booting (GPIO 4 and 10) to activate the relay. A dual LED with both Red and Green diodes is also wired so that Red is shown when the relay is not active and Green when it is - so I always have clear indication of the supply being present and what state the lock is in.
    – SlySven
    May 16, 2016 at 22:48

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.