I need to acquire a signal at a minimum sample rate of 10 kHz and processing it real-time using a Python code. Since Raspberry Pi only reads digital signals I get an ADC (High Precision AD/DA Board from Waveshare, with ADS1256 8 CHANNELS 24 BITS, SPI protocol). I'm using the source code provided by Waveshare in order to read channels' value. How can I process ADC data at high sample rate in real-time? It seems that acquisition is interrupted when Raspberry is doing another task such as writing the values in a text file and the sampling frequency is not constant. I've read about buffers but my ADC does not implement these. Can I use threading and queues?

  • In addition to the Answer by joan DON'T use Python - which due to GIL only allows one task to run at a time - no matter how many cores you have. On a Pi4 with 4 CPU you can potentially run many tasks, but SPI will only read one channel at a time unless you use multiple SPI interfaces. – Milliways Nov 13 '20 at 10:23
  • So, even if I implement multiprocessing on Python and save a number of readings to a list and then I pass that block to Pi in another thread with a set delay I cannot process that signal. Is It true? – Tagalog Nov 13 '20 at 10:34
  • See docs.python.org/3.7/library/… My experience has not indicated much (if any) gain, but if the tasks are truly independent it may work. I usually find it easier to use c for logging and use Python for the tasks it excels in. Even in the 1970s many of my tasks used a combination of FORTRAN (for array processing) and COBOL for input and output. – Milliways Nov 13 '20 at 10:46
  • And what about processing a slow signal ( max 100Hz sampling rate)? Raspberry can take samples ~real time? – Tagalog Nov 13 '20 at 13:49

Linux is not a real-time operating system.

You will not be able to achieve consistent sampling times using the standard Linux environment.

I have used DMA bit banging to get accurately timed SPI samples.

By using bit-banging it is possible to get 12 bit ADC samples from one or more MCP3202s simultaneously. Rates of 25ksps per ADC can be achieved. Conventional SPI only allows one device to talk at a time. By using bit-banging you can share one CLK, MOSI, and SS (slave select) lines between many ADCs but give each its own MISO.

An advantage of bit-banging is that you have tight control of when the samples are taken, in the case given every 40 μs, not as and when the SPI driver gets around to it.

To bit-bang a chain of DMA blocks is used to switch the CLK, MOSI, and SS lines on/off according to the SPI requirements. The DMA blocks also contain gpio reads to capture the data transmitted by the ADC MISO lines. All gpios are read simultaneously (that's the way the Broadcom SOC works) so as many ADCs can be read as can be connected. In my experiment I used 2 ADCs as that is all I have.

The waveforms are constructed by a modified version of the pigpio library. Each waveform is made up of a series of pulses which define its characteristics. Multiple waveforms are generated so that samples can be held in a cyclic buffer in memory (giving enough time to extract the readings). The waveform to capture one sample is given below.


Also see http://abyz.me.uk/rpi/pigpio/examples.html#C_rawMCP3202_c

  • And what if I need only 1 second of signal sampled at 10 kHz? Can I only save this record in a variable and process it as soon as I reach the number of samples required? Or Raspberry isn't able at all to save a record at such sampling rate? – Tagalog Nov 13 '20 at 14:45
  • You are asking if the Raspberry Pi is capable of storing 30 thousand bytes. Yes, it is. – joan Nov 13 '20 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.