Seeed Studio XIAO ESP32S3 microphone incompatible with Micropython? #16048
-
So far, all attempts to read data from the Seeed Studio XIAO ESP32S3 microphone have been unsuccessful. Cannot find any Micropython code samples to reference. I've tried with the pins found in the C sample code, but others have also found that these are incorrect. Is I2S completely incompatible here, or is there something I'm missing? Have tried PDM to PCM conversion, other pins, other buffer sizes, other bit rates, etc. and no data ever comes in. I'm aware that the mic might have a sound threshold. Have tried loud noises as well. Nothing. FYI: I'm using the Micropython firmware provided by Seeed Studio. Here is some sample code: import time
from machine import I2S, Pin
# Define GPIO pins for Xiao ESP32S3 Sense
BCLK_PIN = 12 # Bit Clock (SCK)
LRCK_PIN = 11 # Word Select (WS)
DATA_PIN = 13 # Data In (SD)
def initialize_i2s():
try:
# Initialize I2S interface
print("Initializing I2S...")
i2s = I2S(
0, # Use I2S0
sck=Pin(BCLK_PIN),
ws=Pin(LRCK_PIN),
sd=Pin(DATA_PIN),
mode=I2S.RX,
bits=16, # 16 bits per sample
format=I2S.MONO, # Mono audio
rate=16000, # 16 kHz sample rate
ibuf=4096, # Input buffer size
)
print("I2S initialized successfully")
return i2s
except Exception as e:
print("I2S Initialization Error:", e)
return None
def capture_and_print_audio(i2s, num_samples=10):
if not i2s:
print("I2S not initialized. Cannot capture audio.")
return
print("I2S stabilized. Starting audio capture...")
try:
buf = bytearray(2048) # Buffer for 1024 samples (2 bytes per sample)
for sample_num in range(num_samples):
bytes_read = i2s.readinto(buf)
print(f"Bytes read: {bytes_read}")
if bytes_read:
# Print the first 8 bytes for debugging
print(f"Raw bytes: {buf[:8].hex()}")
# Print first few sample values
for i in range(0, min(16, bytes_read), 2):
if i+2 > bytes_read:
break # Prevent overflow
sample = int.from_bytes(buf[i:i+2], 'little', True)
print(f"Audio Sample {i//2 + 1}: {sample}")
else:
print("No data read")
time.sleep(0.1) # Short delay between captures
except Exception as e:
print("Error during audio capture:", e)
def deinitialize_i2s(i2s):
if i2s:
try:
i2s.deinit()
print("I2S deinitialized successfully")
except Exception as e:
print("Error during I2S deinitialization:", e)
else:
print("I2S was not initialized; skipping deinitialization.")
def main():
i2s = initialize_i2s()
time.sleep(1) # Allow microphone to stabilize
capture_and_print_audio(i2s, num_samples=10)
deinitialize_i2s(i2s)
if __name__ == "__main__":
main() |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
The microphone on this Seeed Studio product is a PDM microphone. The PDM protocol is not supported by MicroPython. It is not possible to use the |
Beta Was this translation helpful? Give feedback.
-
Claudia have a look to the #14176 |
Beta Was this translation helpful? Give feedback.
-
You can use it now, if you use Circuitpython instead of Micropython. Circuitpython is not a drop-in replacement. Keep this in mind. |
Beta Was this translation helpful? Give feedback.
The microphone on this Seeed Studio product is a PDM microphone. The PDM protocol is not supported by MicroPython. It is not possible to use the
machine.I2S
class with a PDM microphone.