Solenoids are excellent for pushing and pulling small things. They are electromagnets that, when on, will push or pull a small metal slug a short distance. A built in spring is used to return the slug to its resting position when off. 

Solenoids use greater amount of current than a microcontroller can supply, so a MOSFET transistor will handle the power duties.

When the MOSFET gate pin receives signal from the Pico GP14 output, the MOSFET will allow the 5V to head through the solenoid to ground, thus energizing the solenoid's electromagnet and moving its slug.

When the Pico GP14 output goes low, the MOSFET gate will close and the solenoid will become de-energized, returning the slug to its resting position.

Follow the diagram above to breadboard the circuit. These are the connections you'll make:

  • Pico pin 19 (GP14) to MOSFET gate
  • Pico GND to common ground rail
  • Pico pin 5 (GP3) to mode button to GND
  • Pico pin 30 (RESET) to reset button to GND
  • Power supply 5V+ to 47uF capacitor to GND
  • 5V rail to solenoid pin 2
  • MOSFET source to GND
  • MOSFET drain to solenoid pin 1
  • Diode to shunt 5V from drain and solenoid pin 1

Code

Copy the code from the element below, and paste it into a fresh file in Mu. Save the file to your Pico's CIRCUITPY drive as code.py It will automatically run the code!

In this program, all you need to do is press the mode button, and then the solenoid will strike four times.

# SPDX-FileCopyrightText: 2021 jedgarpark for Adafruit Industries
# SPDX-License-Identifier: MIT

# This example uses an MOSFET transistor circuit to drive a solenoid from a
# Pico RP2040 digitalio pin
# Hardware setup:
#   Button on GP3 to gnd (uses internal pull up)
#   MOSFET driving solenoid on GP14 w protection diode and 47uF capacitor across power rails
#   external power source should be proper voltage and current for solenoid, not USB power

import time
import board
from digitalio import DigitalInOut, Direction, Pull

print("*** Solenoid Demo ***")

led = DigitalInOut(board.LED)  # onboard LED setup
led.direction = Direction.OUTPUT
led.value = True


def blink(times):
    for _ in range(times):
        led.value = False
        time.sleep(0.1)
        led.value = True
        time.sleep(0.1)


# Mode button setup
button = DigitalInOut(board.GP3)
button.direction = Direction.INPUT
button.pull = Pull.UP

# Solenoid setup
solenoid = DigitalInOut(board.GP14)  #  pin drives a MOSFET
solenoid.direction = Direction.OUTPUT
solenoid.value = False

strike_time = 0.05  # coil on time range ~0.05 - ? beware heat/power drain beyond a few seconds)
recover_time = 0.20  # adjust for coil off time/pause between strikes


def solenoid_strike(loops):  # solenoid strike function
    print("solenoid test")
    for _ in range(loops):
        solenoid.value = True
        time.sleep(strike_time)
        solenoid.value = False
        time.sleep(recover_time)
        time.sleep(0.1)


while True:
    if not button.value:
        blink(1)
        solenoid_strike(4)

How It Works

Libraries

First, the libraries are imported for time so it can pause, board for pin definitions, and digitalio for lighting the onboard LED, using the mode button, and sending signals to the MOSFET to fire the solenoid.

Download: file
import time
import board
from digitalio import DigitalInOut, Direction, Pull

LED

The on-board LED is set up and turned on to show that the code is running, and will blink when the button is pressed.

Download: file
led = DigitalInOut(board.LED)  # onboard LED setup
led.direction = Direction.OUTPUT
led.value = True


def blink(times):
    for _ in range(times):
        led.value = False
        time.sleep(0.1)
        led.value = True
        time.sleep(0.1)

Button

The button is set up on pin GP3 as an input with internal pull up resistor. When the button is pressed the solenoid will strike four times (you can change this number later).

Download: file
button = DigitalInOut(board.GP3)
button.direction = Direction.INPUT
button.pull = Pull.UP

Solenoid Setup

The solenoid will be controlled by setting a GPIO output pin to high. Pin GP14 on the Pico will be used for this -- since it can be anything from a PWM pin, to an I2C pin, to a digital input or output and more, we must first set the pin as a digital IO output pin.

Its value will be set low or False by default.

Download: file
solenoid = DigitalInOut(board.GP14)  #  pin drives a MOSFET
solenoid.direction = Direction.OUTPUT
solenoid.value = False

Time Variables

It takes a fraction of a second to energize and then release the solenoid, as low as ~0.05 seconds depending on the power supply. A variable called strike_time is set to define this value. This determines if it'll be a very quick strike, or if the slug will get thrown and stay in that position for a while. You can hold it out for a second or more, but beware the power drain and possible over-heating of the solenoid coil if it is energized for too long without release.

The amount of time spent recovering in a non-energized state will be defined with the recover_time variable. This will determine the tempo of consecutive strikes.

Download: file
strike_time = 0.05 
recover_time = 0.20
Note how this adds up to a quarter of a second -- a pretty nice tempo for musical quarter notes in a 60 BPM (beats per minute) pattern. Just in case you're thinking of drumming with your solenoids.

Solenoid Strike Function

The solenoid_strike(loops) function is set up to make it easy to loop through a number of solenoid strikes.

It takes an loops input value and then uses the strike_time and recover_time to repeat a pattern of strikes.

Download: file
def solenoid_strike(loops):  # solenoid strike function
    print("solenoid test")
    for _ in range(loops):
        solenoid.value = True
        time.sleep(strike_time)
        solenoid.value = False
        time.sleep(recover_time)
        time.sleep(0.1)

Main Loop

The main loop waits for a button press, then blinks the onboard LED and strikes the solenoid four times (this number can be changed to whatever you like.

Press the button again and it repeats the process. Happy Pico-driven solenoiding!

Download: file
while True:
    if not button.value:
        blink(1)
        solenoid_strike(4)

This guide was first published on Feb 17, 2021. It was last updated on 2021-02-17 14:48:05 -0500.

This page (Solenoids) was last updated on Apr 10, 2021.

Text editor powered by tinymce.