code.py
 1  # SPDX-FileCopyrightText: 2021 jedgarpark for Adafruit Industries
 2  # SPDX-License-Identifier: MIT
 3  
 4  # This example uses an MOSFET transistor circuit to drive a solenoid from a
 5  # Pico RP2040 digitalio pin
 6  # Hardware setup:
 7  #   Button on GP3 to gnd (uses internal pull up)
 8  #   MOSFET driving solenoid on GP14 w protection diode and 47uF capacitor across power rails
 9  #   external power source should be proper voltage and current for solenoid, not USB power
10  
11  import time
12  import board
13  from digitalio import DigitalInOut, Direction, Pull
14  
15  print("*** Solenoid Demo ***")
16  
17  led = DigitalInOut(board.LED)  # onboard LED setup
18  led.direction = Direction.OUTPUT
19  led.value = True
20  
21  
22  def blink(times):
23      for _ in range(times):
24          led.value = False
25          time.sleep(0.1)
26          led.value = True
27          time.sleep(0.1)
28  
29  
30  # Mode button setup
31  button = DigitalInOut(board.GP3)
32  button.direction = Direction.INPUT
33  button.pull = Pull.UP
34  
35  # Solenoid setup
36  solenoid = DigitalInOut(board.GP14)  #  pin drives a MOSFET
37  solenoid.direction = Direction.OUTPUT
38  solenoid.value = False
39  
40  strike_time = 0.05  # coil on time range ~0.05 - ? beware heat/power drain beyond a few seconds)
41  recover_time = 0.20  # adjust for coil off time/pause between strikes
42  
43  
44  def solenoid_strike(loops):  # solenoid strike function
45      print("solenoid test")
46      for _ in range(loops):
47          solenoid.value = True
48          time.sleep(strike_time)
49          solenoid.value = False
50          time.sleep(recover_time)
51          time.sleep(0.1)
52  
53  
54  while True:
55      if not button.value:
56          blink(1)
57          solenoid_strike(4)