How to continuously read analog pins?

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Jander42
 
Posts: 11
Joined: Tue Sep 15, 2020 11:35 pm

How to continuously read analog pins?

Post by Jander42 »

Is it possible to continuously read analog pins without interruption in Circuit Python? Ideally I'd like to sample audio on one core while doing a spectrogram+display on another core. I'm using an ESP32-S3 with the Neopixel Featherwing and the auto-gain adjusting microphone.

What I've done so far is use asyncio to create one task to read the pins with analogbufio and a second task to hand the analysis and display.

Here is the psuedocode:

Code: Select all

buffer_task = asyncio.start_task( analogbufio.readinto(buffer) )
while True:
	mic_buffer = await buffer_task
	buffer_task = asyncio.start_task( analogbufio.readinto(buffer) )
	/* Run Spectrogram and update NeoPixels */
	...
The behavior I get is this mostly works but occasionally there is a drum beat or other sharp short pulse that is missed entirely. I strongly suspect those happen in the gap between getting the buffer and starting the next collection. From the docs it sounds like asyncio tasks may not run on both cores. I tried an asyncio generator function but those aren't supported in Circuit Python yet.

Is there a better approach? Ideally I'd like an analogbufio buffer that is continually collecting input and when it is full it rolls over to overwrite the start of the buffer. Perhaps a callback function every N samples.

Thanks

User avatar
tannewt
 
Posts: 3315
Joined: Thu Oct 06, 2016 8:48 pm

Re: How to continuously read analog pins?

Post by tannewt »

This is the best way currently. We've discussed improving audio input support but haven't gotten there yet. Here is an open issue for it: https://github.com/adafruit/circuitpython/issues/2676

User avatar
Jander42
 
Posts: 11
Joined: Tue Sep 15, 2020 11:35 pm

Re: How to continuously read analog pins?

Post by Jander42 »

Thanks for the response. I had a feeling that was going to be the answer but worth a try.

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

Return to “Adafruit CircuitPython”