MOSFET Motor Driver¶
The lab currently stocks IRLB8721PBF MOSFET transistors compatible with the Pico 3.3V logic, Lab Part 8721. Each of these can drive one DC motor in a single direction.
Sample MOSFET Motor Driver Circuit¶
Sample MOSFET driver circuit. Note that any GPIO pin may be used to control the IRLB8721PBF MOSFET transistor driver. The circuit is shown with a battery for motor power, but a an 5 or 12V adapter is also suitable if appropriate for the chosen motor. Please be careful with the power wiring, motor voltages can destroy the Pico. The IRLB8721PBF is specifically selected because it will operate with the Pico 3.3V output signals.¶
Pump Control Example¶
This self-contained example operates one DC motor in a single direction at variable speed. This is usually all that is needed for operating pumps or fans. For bidirectional control, please see the DRV8833 examples. The example uses GP22 for output, but any of the GPIO outputs can be programmed for pulse-width-modulation (PWM) output.
Direct download: pump_control.py.
1# pump_control.py
2
3# Demonstrate variable-speed single-directional control of a DC motor driven via
4# a MOSFET transistor driver circuit. For a reversible motion, please see the
5# DRV8833 example, but some applications (e.g. pumps and fans) only require a
6# single direction of rotation at variable speed.
7
8# Related documentation:
9# https://circuitpython.readthedocs.io/en/latest/shared-bindings/pwmio/index.html#module-pwmio
10
11# ----------------------------------------------------------------
12# Import standard Python modules.
13import time, math
14
15# Load the CircuitPython hardware definition module for pin definitions.
16import board
17
18# Load the CircuitPython pulse-width-modulation module for driving hardware.
19import pwmio
20
21# ----------------------------------------------------------------
22# Initialize hardware.
23
24# Create a PWMOut object on GP22. This will create a pulsing signal with
25# variable duty cycle. The switching rate of 20kHz is chosen to be
26# supra-audible.
27pwm = pwmio.PWMOut(board.GP22, duty_cycle=0, frequency=20000)
28
29# Utility function to calculate and apply a new duty cycle setting. Converts a
30# unit-range (0.0 to 1.0) pwm_level to a 16-bit integer representing a fraction
31# needed by the PWM driver.
32def set_motor_speed(pwm_device, pwm_level):
33 pwm_device.duty_cycle = min(max(int(pwm_level * 2**16), 0), 2**16-1)
34 print("Applied duty cycle of %d for pwm_level %f" % (pwm_device.duty_cycle, pwm_level))
35
36# ----------------------------------------------------------------
37# Enter the main event loop.
38while True:
39
40 # Turn the motor on halfway.
41 set_motor_speed(pwm, 0.5)
42 time.sleep(1.0)
43
44 # Turn the motor off again.
45 set_motor_speed(pwm, 0.0)
46 time.sleep(1.0)
47
48 # Turn the motor on a little faster.
49 set_motor_speed(pwm, 0.75)
50 time.sleep(1.0)
51
52 # Turn the motor off again.
53 set_motor_speed(pwm, 0.0)
54 time.sleep(1.0)
55
56 # Turn the motor full.
57 set_motor_speed(pwm, 1.0)
58 time.sleep(1.0)
59
60 # Turn the motor off again.
61 set_motor_speed(pwm, 0.0)
62 time.sleep(1.0)