adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
Other
4.04k stars 1.19k forks source link

Itsy Bitsy M0 Express Audio Noise when accelerometer connects #2746

Open brettm2 opened 4 years ago

brettm2 commented 4 years ago

I've had noise issues with the Itsy Bitsy M0 Express and have boiled it done to attaching the MSA301 Accelerometer. I can attach neopixels, buttons, etc. but as soon as I attach the accelerometer I get random loud noise. I've also tried different speakers and audio boards with no change in the noise. The noise can occur right when I turn it on or it may wait 3-4 sec before starting.

Here is the simplified code:

# Import libraries
import board
from digitalio import DigitalInOut, Direction, Pull
import busio
import adafruit_msa301
import audioio
import audiobusio

# initialize audio
audio = audiobusio.I2SOut(board.D1, board.D0, board.D9)

# Accelerometer
i2c = busio.I2C(board.SCL, board.SDA)
msa = adafruit_msa301.MSA301(i2c)
msa.range = adafruit_msa301.Range.RANGE_16_G

#Initialize button
button1 = DigitalInOut(board.D10)
button1.direction = Direction.INPUT
button1.pull = Pull.UP

def play_wav(name, loop=False, sample_rate=2000):
    try:
        wave_file = open('sounds/' + name + '.wav', 'rb')
        wave = audioio.WaveFile(wave_file)
        audio.play(wave, loop=loop)
    except:
        return

while True:
    if not button1.value:
        while not button1.value:
            pass
        play_wav("hum",True)

    if audio.playing:
        x, y, z = msa.acceleration

Here is a wiring diagram: ItsyBitsy

I'm pretty new at adding sound to projects so I'm hoping it's something simple I'm not seeing. Any help figuring this out would be greatly appreciated.

tannewt commented 4 years ago

@caternuson want to see if you can reproduce this?

caternuson commented 4 years ago

@tannewt Yep, recreated. I went back to just the basic setup per the guide: https://learn.adafruit.com/adafruit-max98357-i2s-class-d-mono-amp/circuitpython-wiring-test and still see the issue. So may be something more fundamental and not related to MSA301.

itsy_m0_i2s_test

Tested as follows. Libraries from 20200714 bundle release.

Adafruit CircuitPython 5.3.1 on 2020-07-13; Adafruit ItsyBitsy M0 Express with samd21g18
>>> import audioio
>>> import board
>>> import audiobusio
>>> wave_file = open("StreetChicken.wav", "rb")
>>> wave = audioio.WaveFile(wave_file)
>>> audio = audiobusio.I2SOut(board.D1, board.D0, board.D9)
>>> audio.play(wave)
>>> audio.play(wave)
>>> audio.play(wave)
>>> audio.play(wave)
>>> 

The issue isn't there all the time. Some of the playbacks above were OK. Some (most) were distorted.

tannewt commented 4 years ago

Thanks @caternuson! @jepler any ideas what could cause this?

dhalbert commented 10 months ago

Increasing the buffer size may help. We saw similar issues on RP2040 PropMaker.

ValleyKX commented 6 months ago

Just a heads up: The board you have doesn't support I2S, and you're attempting to use I2S to run the speaker. I ran into this problem and figured I'd leave this here in case anyone else runs into this issue.