Demonstrated here is an easy way to control a 12 Volt Solenoid and a 5V Relay using a GPIO Pin on a Raspberry Pi.

Transcript

Hey gang, Tim here at Core Electronics and today we're controlling a solenoid using a Raspberry Pi. Solenoids are an immensely useful way to turn electrical energy into a linear motion.

Solenoids come in a variety of different specs with 5 volts and 12 volts being very common for makers to experience. So today we're going to activate the stronger 12 volt solenoid using a Raspberry Pi GPIO pin with a relay module in between the two.

On the table before me is everything you need to make it work. A 12 volt solenoid, I'm using a latch solenoid in this case, a 5 volt single channel relay module at 10 amps. While marked as 5 volts for input signals, we found this to work fine at 3.3 volts and this is good as it's the max voltage that a Raspberry Pi GPIO pin can output.

A Raspberry Pi with a microSD card flashed with the Raspberry Pi OS. I'm using a Raspberry Pi 4 1 gigabyte but this will definitely work with earlier models of Raspberry Pi. You'll also need a power supply for the Raspberry Pi, in this case I'm using the official Raspberry Pi power supply which operates at 5 volts and a 12 volt 2 amp power supply for the solenoid.

Solenoids of 12 volts usually need around 600 milliamps to drive them so if you're using multiple solenoids simultaneously you may need a higher current from your supply. You'll also need jumper cables both female to female and male to male and a DC power jack so that we can separate the ground from the positive easily from this power supply. You're also going to need a screwdriver.

So to start, connect the ground pin of the Raspberry Pi to the ground pin on the relay. Connect the VCC volt power of the relay to the 3.3 volt pin on the Raspberry Pi. For this I will use the GPIO18 which is pin number.12 as the data pin telling the relay either to open or close. So I will connect that GPIO pin 18 to the in on the relay.

Moving attention to the solenoid, I attach the ground wire to the ground connection on the DC power jack by screwing it into place with a screwdriver, then attach the solenoid positive to the middle connection on the relay and the positive side of the power jack to the pin on the top of the relay by screwing them down.

The top of the board is determined by following the orientation of the words printed on top of the blue box which conceals the actual mechanical mechanism of the relay. The pin on the bottom is going to be left empty. This arrangement means the solenoid will be unpowered unless the relay is actuated by the Raspberry Pi. There are some nice symbols on the bottom of this relay to help you figure out which ones to connect to.

So this arrangement means that the solenoid is going to remain unpowered unless the relay is actuated by the Raspberry Pi. If we had instead of connected the top wire on the relay to the bottom, it would power the solenoid until the relay was actuated. Keep in mind, solenoids end up getting very hot if you keep them activated for a long time.

Now before we add power to the system by connecting the two power supplies, let's get the code into the Raspberry Pi running. There are a lot of ways you can get a Raspberry Pi to activate this solenoid. I will do the most intuitive by connecting this Raspberry Pi up to a monitor with a keyboard and mouse and directly writing the code to it.

I'm going to open up a Python interface and I'm going to copy and paste the following code into it. You're going to be able to find this exact code in my article page. Nice.

So let's save.And run that code here and we'll plug in the power for the solenoid and see what happens.

We can already hear the relay turning itself on and off. Nice. It's working.

So now it's working. Let's take a quick look at that code. The first two lines set up all the necessary information the script needs to import functionality. It then removes GPIO warnings and then determines the way we refer to each GIO pin, in this case by the number found after the GPIO.

Next, we make the GPIO18 pin be an output pin. Then it starts an infinite loop using a while true statement. The loop starts by setting the output GPIO pin 18 to high, thus sending out the max voltage. The max voltage this GPIO pin can send out is approximately 3.3 volts. This voltage will not trigger the relay and thus the solenoid will remain deactivated.

Next, the code will wait for a second. After that wait, the GPIO pin 18 is turned to low, thus it sends out the min voltage. This will send out the lowest voltage an output pin can, which is approximately 0 volts. This will trigger the relay to switch and thus activating the solenoid. The Raspberry Pi will then wait for a second. This process is then repeated, opening and closing the solenoid.

The code here you can alter it to make whatever the GPIO pin you want to cause the solenoid actuation for any manner of reasons or lengths of time. Altering code here is where the real creativity can happen.

Another good way of activating these GPIO remotely is by using your phone and a free app called Raspberry Controller. Link to that guide showing you how in the description.

So that is it for today. You can now control a 12-volt solenoid from a Raspberry Pi using a relay. In this case, I used one for a latch and lock, butThere are more traditional plunger solenoids and also very cool valve solenoids which you can use to control the flow of liquid.

It is also true you can control solenoids more directly using a transistor, resistor and diode. Check out another guide linked down below on how you can do that with a 5 volt solenoid and an Arduino.

So with this solenoid button away, until next time, stay cozy.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.