Once you've finished setting up your QT Py RP2040 with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.
To do this, click on the Download Project Bundle button in the window below. It will download as a zipped folder.
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries # SPDX-License-Identifier: MIT import time import board import pwmio import simpleio from adafruit_motor import motor from analogio import AnalogIn from digitalio import DigitalInOut, Direction, Pull # button setup warble_switch = DigitalInOut(board.A0) warble_switch.direction = Direction.INPUT warble_switch.pull = Pull.UP # potentiometer setup pot = AnalogIn(board.A1) # PWM pins for L9110 PWM_PIN_A = board.A3 # pick any pwm pins on their own channels PWM_PIN_B = board.A2 # PWM setup pwm_a = pwmio.PWMOut(PWM_PIN_A, frequency=50) pwm_b = pwmio.PWMOut(PWM_PIN_B, frequency=50) # motor setup cassette = motor.DCMotor(pwm_a, pwm_b) # variables for warble switch i = 0.4 last_i = 0.4 pos = False neg = False while True: # map range of pot to range of motor speed # all the way to the left will run the motor in reverse full speed # all the way to the right will run the motor forward full speed mapped_speed = simpleio.map_range(pot.value, 0, 65535, -1.0, 1.0) # if you press the button... # creates a ramping effect if not warble_switch.value: # checks current pot reading # if it's positive... if mapped_speed > 0: # sets starting speed i = 0.4 # sets last value to loop last_i = i # notes that it's positive pos = True # if it's negative... else: # sets starting speed i = -0.4 # sets last value to loop last_i = i # notes that it's negative neg = True # loop 8 times for z in range(8): # if it's positive... if pos: # increase speed i += 0.06 # if it's negative else: # decrease speed i -= 0.06 # send value to motor cassette.throttle = i time.sleep(0.1) # loop the value while button is held down i = last_i pos = False neg = False # run motor at mapped speed from the pot cassette.throttle = mapped_speed
Upload the Code and Libraries to the QT Py RP2040
After downloading the Project Bundle, plug your QT Py RP2040 into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the QT Py RP2040's CIRCUITPY drive.
- lib folder
- code.py
Your QT Py RP2040 CIRCUITPY drive should look like this after copying the lib folder and the code.py file.
The L9110 motor controller can be used with the adafruit_motor
CircuitPython library. It just requires two PWM pins.
# PWM pins for L9110 PWM_PIN_A = board.A3 # pick any pwm pins on their own channels PWM_PIN_B = board.A2 # PWM setup pwm_a = pwmio.PWMOut(PWM_PIN_A, frequency=50) pwm_b = pwmio.PWMOut(PWM_PIN_B, frequency=50) # motor setup cassette = motor.DCMotor(pwm_a, pwm_b)
In the loop, the potentiometer's value range is mapped to the motor's throttle range. When the pot's value is 0
, the motor will throttle full speed in reverse. When the pot's value is 65535
, the motor will throttle full speed forward.
mapped_speed = simpleio.map_range(pot.value, 0, 65535, -1.0, 1.0) cassette.throttle = mapped_speed
The button acts as a revved warble switch. When it's pressed, it incrementally increases the motor's throttle in increments of 0.06
to create a warbling sound. The direction of the warble is affected by the pot's position.
# if you press the button... # creates a ramping effect if not warble_switch.value: # checks current pot reading # if it's positive... if mapped_speed > 0: # sets starting speed i = 0.4 # sets last value to loop last_i = i # notes that it's positive pos = True # if it's negative... else: # sets starting speed i = -0.4 # sets last value to loop last_i = i # notes that it's negative neg = True # loop 8 times for z in range(8): # if it's positive... if pos: # increase speed i += 0.06 # if it's negative else: # decrease speed i -= 0.06 # send value to motor cassette.throttle = i time.sleep(0.1) # loop the value while button is held down i = last_i pos = False neg = False
Text editor powered by tinymce.