Motor Running Even Though Duty Cycle is 0

Anishwar~ I’m still struggling to solve the problems from class the other day. The motor pump is still constantly pumping air; additionally, I am having trouble with getting the non-blocking version of the sound part of the code working. However, one success was getting the program to run only when there isn’t any light. While, the motor pump was still running because of the aforementioned issue the duty cycle was 0 and no sound played.

Motor pump under new fabric

Elise~ I found new fabric that stretches well with the pneumatic. I had two versions that worked but I think a bigger inflatable makes more movement. Also tested code: It turned green when in brighter light and back to blue when in darker light. The tones changed with the light but no motor pump output.

Code tested on my end
# cpb_pump_control.py

# Demonstrate control of an air pump based on a DC motor driven via a MOSFET
# transistor driver circuit *or* a DRV8833 motor driver in a single-ended mode.
#
#  1. The driver PWM input           connects to A2
#  2. The battery ground line        connects to GND

# Related documentation:
# https://circuitpython.readthedocs.io/en/latest/shared-bindings/pwmio/index.html#module-pwmio
# https://circuitpython.readthedocs.io/projects/motor/en/latest/api.html#module-adafruit_motor.servo

# ----------------------------------------------------------------
# Import standard Python modules.
import time, math

# Import the board-specific input/output library.
from adafruit_circuitplayground import cp

# Import the low-level hardware libraries.
import board
import pwmio

# ----------------------------------------------------------------
# Initialize hardware.

# Create a PWMOut object on pad A2 to generate control signals.
pwm = pwmio.PWMOut(board.A3, duty_cycle=0, frequency=20000)

# ----------------------------------------------------------------
# Initialize global variables for the main loop.

phase_angle = 0.0
cycle_duration = 12                       # seconds per cycle
phase_rate  = 2*math.pi / cycle_duration  # radians/second
next_pwm_update = time.monotonic_ns()
next_status_update = time.monotonic_ns()
toneCheck = True
nextSound = time.monotonic_ns()

# ----------------------------------------------------------------
# Enter the main event loop.
while True:
    # Read the current integer clock.
    now = time.monotonic_ns()
    soundNow = time.monotonic_ns()


    # If the time has arrived to update the servo command signal:
    if cp.light < 81:
        if now &gt;= next_pwm_update:
            next_pwm_update += 20000000  # 20 msec in nanoseconds (50 Hz update)
            pwm_level = 0.5 + 0.5 * math.sin(phase_angle)

            # convert a unit-range (0 to 1) pwm_level to a 16-bit integer representing a fraction
            new_duty_cycle = min(max(int(pwm_level * 2**16), 0), 2**16-1)

            # If the new value is less than a reasonable minimum, clamp to zero.
            # The pump motor will stall if the PWM fraction is too low; this turns
            # it off instead.
            if new_duty_cycle < 48000:
                pwm.duty_cycle = 0
            else:
                pwm.duty_cycle = new_duty_cycle

            phase_angle = (phase_angle + phase_rate * 0.020) % (2 * math.pi)

        # If either button is pressed, override the generated servo signal and run the motor.
        if cp.button_a or cp.button_b:
            pwm.duty_cycle = 2**16-1

        # If the time has arrived to display the status:
        if now &gt;= next_status_update:
            next_status_update += 500000000  # 0.5 sec in nanoseconds (2 Hz update)
            print(pwm.duty_cycle)

        if cp.light < 81 and cp.light &gt;= 36:
            cp.play_tone(329,.5)
            cp.play_tone(493,.5)
            cp.pixels.fill((0,255,0))
            
        elif cp.light < 36:
            cp.play_tone(293,.5)
            cp.play_tone(466,.5)
            cp.pixels.fill((0,0,255))
    else:
        pwm.duty_cycle = 0
        print("ok")



Below is my attempt to try making the sounds play using a non-blocking code; however, it doesn’t seem to be working and I need to continue messing with it.

Elise~ tested and needed A2 to change to A3 like previous code. Motor and light combination has some issues moving smoothly.

Testing second code set
# cpb_pump_control.py

# Demonstrate control of an air pump based on a DC motor driven via a MOSFET
# transistor driver circuit *or* a DRV8833 motor driver in a single-ended mode.
#
#  1. The driver PWM input           connects to A2
#  2. The battery ground line        connects to GND

# Related documentation:
# https://circuitpython.readthedocs.io/en/latest/shared-bindings/pwmio/index.html#module-pwmio
# https://circuitpython.readthedocs.io/projects/motor/en/latest/api.html#module-adafruit_motor.servo

# ----------------------------------------------------------------
# Import standard Python modules.
import time, math

# Import the board-specific input/output library.
from adafruit_circuitplayground import cp

# Import the low-level hardware libraries.
import board
import pwmio

# ----------------------------------------------------------------
# Initialize hardware.

# Create a PWMOut object on pad A2 to generate control signals.
pwm = pwmio.PWMOut(board.A2, duty_cycle=0, frequency=20000)

# ----------------------------------------------------------------
# Initialize global variables for the main loop.

phase_angle = 0.0
cycle_duration = 12                       # seconds per cycle
phase_rate  = 2*math.pi / cycle_duration  # radians/second
next_pwm_update = time.monotonic_ns()
next_status_update = time.monotonic_ns()
toneCheck = True
nextSound = time.monotonic_ns()

# ----------------------------------------------------------------
# Enter the main event loop.
while True:
    # Read the current integer clock.
    now = time.monotonic_ns()
    soundNow = time.monotonic_ns()


    # If the time has arrived to update the servo command signal:
    if now &gt;= next_pwm_update:
        next_pwm_update += 20000000  # 20 msec in nanoseconds (50 Hz update)
        pwm_level = 0.5 + 0.5 * math.sin(phase_angle)

        # convert a unit-range (0 to 1) pwm_level to a 16-bit integer representing a fraction
        new_duty_cycle = min(max(int(pwm_level * 2**16), 0), 2**16-1)

        # If the new value is less than a reasonable minimum, clamp to zero.
        # The pump motor will stall if the PWM fraction is too low; this turns
        # it off instead.
        if new_duty_cycle < 48000:
            pwm.duty_cycle = 0
        else:
            pwm.duty_cycle = new_duty_cycle

        phase_angle = (phase_angle + phase_rate * 0.020) % (2 * math.pi)

    # If either button is pressed, override the generated servo signal and run the motor.
    if cp.button_a or cp.button_b:
        pwm.duty_cycle = 2**16-1

    # If the time has arrived to display the status:
    if now &gt;= next_status_update:
        next_status_update += 500000000  # 0.5 sec in nanoseconds (2 Hz update)
        print(pwm.duty_cycle)

    if cp.light < 81 and cp.light &gt;= 36:
        print(soundNow)
        print(nextSound)
        if soundNow &gt;= nextSound:
            print("something")
            cp.start_tone(329)
            nextSound = soundNow + 500000000
            cp.stop_tone()
        else:
            print("cool")
            cp.start_tone(493)
            cp.stop_tone()
    elif cp.light < 36:
        cp.play_tone(293,.5)
        cp.play_tone(466,.5)
    else:
        cp.stop_tone()