Support Forum

Share your projects and post your questions

Register   or   Sign In
The Forum

extremely varying ADC sample speeds.

1615 Views - Created 17/12/2018


Posted by:

mallok05 Avatar

Hi all,

I wanted to ask, if someone is experiencing the same odd behavior about the ADC sampling speeds of the Expander PI HAT or equivalent when using the python libs?

I am totally aware about the overhead that python has, but with the adcspeed demo of the AB lib I achieve ~25ksps, what is enough for my project. Even if the sampling speed would vary +-5ksps would be still a reasonable performance for my system, but it needs to be min <= 20ksps. But when I capture less than ~4000 samples, it decreases alot.

Enclose a summary about my testing.

Doese someone have some tips how to stabilize the sampling performacne? Maybe importing the C lib via wrapper to Python?

I used the method from the official python lib.

    def speed_ab_lib(self, samples):

counter = 1
totalsamples = int(samples)
readarray = np.zeros(totalsamples)
starttime =

while counter < totalsamples:
# read the voltage from channel 1 and display on the screen
readarray[counter] = self._inputDriverSpeed.get_sample_raw()
counter = counter + 1

endtime =
totalseconds = (endtime - starttime).total_seconds()
samplespersecond = totalsamples / totalseconds
return samplespersecond

Thanks for your help + grettings!

forum image


Posted by:

andrew Avatar

United Kingdom

andrew Twitter  andrew Website  


There are several things that can cause instability in the sampling performance.

Linux on the Raspberry Pi is not a real-time operating system so all of the processes running in the background will be competing for CPU cycles. Reducing the number of processes running on the Raspberry Pi will improve stability and sample rate. This page has some useful commands for seeing which processes are using the most CPU power.

Python has the disadvantage that it is an interpreted language so it has a lower performance than compiled languages like C. Without doing some in-depth analysis of your code I don't know for certain what is causing the slowdown but the best guess would be that it is to do with the way the program is being cached in memory. On the first loop, the code is being read from the disk and copied into memory so it will take longer to perform the first few reads until the code is fully cached.

There are some good tips on improving the performance of python programs on Python's own website at

Using the C library for the ADC sampling will probably improve performance as well but I would try going through some of the performance tips first to see if they help.


Posted by:

mallok05 Avatar

Hi Andrew,

thanks for your feedback and the sources about performance improvements. I´ll read through them and see if I get a grip on the performance issue. I´m aware about the tradeoffs of raspberry and linux, and that I definitely dont get the perfomance of a MCU driven system. But I thought I can manage a stable/adequate sampling performance first and then squeeze some more speed out of the system with linux based activities e.g. Preempt-RTpatch, isolcpus etc.

I will provide some feedback how it goes.


Sign in to post your reply

Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view.
Download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.