pwmio Import Error - Gemma M0

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
masonmontero
 
Posts: 9
Joined: Mon Nov 22, 2021 7:08 pm

pwmio Import Error - Gemma M0

Post by masonmontero »

Hi,

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'
This is my code.py currently with the references to pwmio commented out:

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
    """
Do I need to download the pwmio module separately? I'm using the recommended Mu IDE and not sure how to do that?

Thanks!

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: pwmio Import Error - Gemma M0

Post by dastels »

Yes, pwmio is stated to be included in the Gemma M0 build.

No, it isn't available as a separate module since it's implemented internal to CircuitPython in C++.

Are you using the latest version of CircuitPython (7.0.0 currently)?

Dave

User avatar
masonmontero
 
Posts: 9
Joined: Mon Nov 22, 2021 7:08 pm

Re: pwmio Import Error - Gemma M0

Post by masonmontero »

Hi Dave,

I just tried to update to CIrcuitPython 7 following this guide (https://learn.adafruit.com/welcome-to-c ... cuitpython) but now my board isn't showing up on my mac?

I'm able to enter into safe-mode and can see the DotStar LED pulse yellow 3 times every 5 seconds but when I connect to my laptop over USB, the board doesn't show up in my file system? I've tried multiple USB cables but still nothing?

The board was showing up before I tried to install the latest CircuitPython so I'm not sure what's going on.

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: pwmio Import Error - Gemma M0

Post by dastels »

Check through the drive related issues on the troubleshooting page: https://learn.adafruit.com/welcome-to-c ... leshooting

Dave

User avatar
masonmontero
 
Posts: 9
Joined: Mon Nov 22, 2021 7:08 pm

Re: pwmio Import Error - Gemma M0

Post by masonmontero »

Got it working! I installed/updated to CircuitPython 7 and now the import is working.

Unfortunately I fried my Gemma somehow and it won't even turn on anymore :(

User avatar
dastels
 
Posts: 15656
Joined: Tue Oct 20, 2015 3:22 pm

Re: pwmio Import Error - Gemma M0

Post by dastels »

Make sure there's nothing stuck to the board shorting something.

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”