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.
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.
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).
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.
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.
strike_time = 0.05 recover_time = 0.20
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.
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!
while True: if not button.value: blink(1) solenoid_strike(4)