Motor Pump Examples - Adafruit Circuit Playground Bluefruit¶
The following short Python programs demonstrate operation of DC motor driven air pumps using the Adafruit Circuit Playground Bluefruit (CPB) board. These assume the board has an external motor drive circuit and power battery circuit attached.
Each can be run by copying the program into code.py
on the CIRCUITPY drive offered by the board. The text can be pasted directly
from this page, or each file can be downloaded from the CircuitPython sample
code folder on this site.
Related Pages
Essential Documentation
Pulsing Pump¶
This sketch smoothly pulses the pump motor on and off.
Direct download: cpb_pump_control.py.
1# cpb_pump_control.py
2
3# Demonstrate control of an air pump based on a DC motor driven via a MOSFET
4# transistor driver circuit *or* a DRV8833 motor driver in a single-ended mode.
5#
6# 1. The driver PWM input connects to A2
7# 2. The battery ground line connects to GND
8
9# Related documentation:
10# https://circuitpython.readthedocs.io/en/latest/shared-bindings/pwmio/index.html#module-pwmio
11# https://circuitpython.readthedocs.io/projects/motor/en/latest/api.html#module-adafruit_motor.servo
12
13# ----------------------------------------------------------------
14# Import standard Python modules.
15import time, math
16
17# Import the board-specific input/output library.
18from adafruit_circuitplayground import cp
19
20# Import the low-level hardware libraries.
21import board
22import pwmio
23
24# ----------------------------------------------------------------
25# Initialize hardware.
26
27# Create a PWMOut object on pad A2 to generate control signals.
28pwm = pwmio.PWMOut(board.A2, duty_cycle=0, frequency=20000)
29
30# ----------------------------------------------------------------
31# Initialize global variables for the main loop.
32
33phase_angle = 0.0
34cycle_duration = 12 # seconds per cycle
35phase_rate = 2*math.pi / cycle_duration # radians/second
36next_pwm_update = time.monotonic_ns()
37next_status_update = time.monotonic_ns()
38
39# ----------------------------------------------------------------
40# Enter the main event loop.
41while True:
42 # Read the current integer clock.
43 now = time.monotonic_ns()
44
45 # If the time has arrived to update the servo command signal:
46 if now >= next_pwm_update:
47 next_pwm_update += 20000000 # 20 msec in nanoseconds (50 Hz update)
48 pwm_level = 0.5 + 0.5 * math.sin(phase_angle)
49
50 # convert a unit-range (0 to 1) pwm_level to a 16-bit integer representing a fraction
51 new_duty_cycle = min(max(int(pwm_level * 2**16), 0), 2**16-1)
52
53 # If the new value is less than a reasonable minimum, clamp to zero.
54 # The pump motor will stall if the PWM fraction is too low; this turns
55 # it off instead.
56 if new_duty_cycle < 48000:
57 pwm.duty_cycle = 0
58 else:
59 pwm.duty_cycle = new_duty_cycle
60
61 phase_angle = (phase_angle + phase_rate * 0.020) % (2 * math.pi)
62
63 # If either button is pressed, override the generated servo signal and run the motor.
64 if cp.button_a or cp.button_b:
65 pwm.duty_cycle = 2**16-1
66
67 # If the time has arrived to display the status:
68 if now >= next_status_update:
69 next_status_update += 500000000 # 0.5 sec in nanoseconds (2 Hz update)
70 print(pwm.duty_cycle)
71