The CircuitPython docs say that the pwmio module is available on the Gemma M0 microcontroller but when I try to import the module in my code.py, I get the following error:
Code: Select all
Traceback (most recent call last):
File "code.py", line 5, in <module>
ImportError: no module named 'pwmio'
Code: Select all
import time
import random
import analogio
import board
import pwmio
import adafruit_dotstar as dotstar
dot = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
sampleWindow = 0.033 # Sample window width (0.033 sec = 33 mS = ~30 Hz)
ledPin = board.D0 # Pin where LEDs are connected (PWM not avail on D1)
micPin = board.A1 # Microphone 'OUT' is connected here
mic = analogio.AnalogIn(micPin)
"""
pwm = pwmio.PWMOut(ledPin, frequency=1000, duty_cycle=0)
"""
def random_color():
return random.randrange(0, 7) * 32
def random_delay():
return random.uniform(0.1, 5.0)
n_dots = len(dot)
while True:
# Make sure Gemma is at least running code
dot.fill(0xADAF00)
time.sleep(0.5)
dot.fill(random_color())
time.sleep(random_delay())
print("random_color:", random_color())
dot[0] = (random_color(), random_color(), random_color())
time.sleep(0.5)
# Listen to mic for short interval, recording min & max signal
signalMin = 65535
signalMax = 0
startTime = time.monotonic()
while (time.monotonic() - startTime) < sampleWindow:
signal = mic.value
if signal < signalMin:
signalMin = signal
if signal > signalMax:
signalMax = signal
peakToPeak = signalMax - signalMin # Audio amplitude
n = (peakToPeak - 250) * 4 # Remove low-level noise, boost
if n > 65535:
n = 65535 # Limit to valid PWM range
elif n < 0:
n = 0
"""
pwm.duty_cycle = n # And send to LED as PWM level
"""
Thanks!