Increase ADC sample rate with Teensy4.1 (and Samd51 Thing Plus as well) #14319
Answered
by
robert-hh
LeandroFernandes88
asked this question in
m.iMX RT / Teensy 4.x
Replies: 2 comments 8 replies
-
|
Beta Was this translation helpful? Give feedback.
7 replies
Answer selected by
LeandroFernandes88
-
This is a way to do this with viper: import time
import array
import machine
#@micropython.viper
def read_signal(data:ptr16, length:int):
adc = machine.ADC(machine.Pin(10)) # The pin depends on the board used
acq_start = time.ticks_us()
i = 0
readadc = adc.read_u16
while i < length:
data[i] = int(readadc())
# time.sleep_us(delay) # commented for higher sample rate
i += 1
duration = (time.ticks_diff( time.ticks_us(), acq_start))
return duration
# a generator will use less memory than a list: [0]*length
length = const(1024)
data = array.array('H', (0 for i in range(length)))
duration = read_signal( data, length )
print("rate", length/duration*1_000_000 ) Unfortunately, I don't have a Teensy. On my ESP32-S3 with PSRAM this samples at 38 kHz. If your application can tolerate a small bit of jitter, this goes up to 300kHz: @micropython.viper
def read_signal2(data:ptr16, length:int):
adc = machine.ADC(machine.Pin(10)) # The pin depends on the board used
acq_start = time.ticks_us()
i = 0
readadc = adc.read_u16
length8 = length//8
while i < length8:
data[i+0] = int(readadc())
data[i+1] = int(readadc())
data[i+2] = int(readadc())
data[i+3] = int(readadc())
data[i+4] = int(readadc())
data[i+5] = int(readadc())
data[i+6] = int(readadc())
data[i+7] = int(readadc())
i += 8
duration = (time.ticks_diff( time.ticks_us(), acq_start))
return duration |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm having some difficulties when reading analog signals with the internal ADC of the Teensy4.1 and Samd51 Thing Plus microcontrollers.
Below is some data obtained when using Micropython on the following microcontrollers:
Raspberry Pi Peak: 46 kHz (real_fs as output)
Samd51 Thing Plus - 5.7 kHz (real_fs as output) with default firmware
Samd51 Thing Plus - 22 kHz, if you recompile the code as follows:
micropython\ports\samd "machine_adc.c"
#define DEFAULT_ADC_AVG 1
Teensy4.1 = 6.4 kHz (real_fs as output)
I tried to keep the code very simple and only perform the acquisition in the array.array module as it is faster than the acquisition with numpy array.
What caught my attention was the performance much below what I was expecting for the Teensy4.1.
Is there anything that could be done to improve this ADC data acquisition speed? As an example, the Raspberry Pi Pico library - Analogbufio, in which higher acquisition speeds are achieved.
Maybe with @ native and @ viper? (the problem is that I don't understand this coding well and I didn't find much information either)
Would it also be possible to perform acquisitions at audio processing frequencies? 22050Hz or 44100Hz?
Or is there a specific library for this purpose?
I saw some reports in posts saying it was possible to reach the ADC's maximum acquisition rate, but I'm really new to coding in Micropython and I can't find code that could be useful to me.
Could anyone suggest the necessary changes in the code to reach these frequencies of interest with @Native or @viper?
Here is my code:
Any help would be greatly appreciated. Thanks
Beta Was this translation helpful? Give feedback.
All reactions