About Us Knowledge Base Community Forum Electronics Toolbox FAQ Shipping & Delivery

Community Forum

Share your projects and post your questions

Search
Register or Sign In
Forum

Need faster sampling rate from ADC DAC Pi Zero

70 Views - Created 19 days ago

19 days ago

Posted by:
Samp38

Users Avatar

I am currently using the ADC from the ADC DAC Pi Zero to record analogue data coming from an ultrasonc sensor with my Raspberry Pi 4. I have created a file in C simply recording 10000 samples and saving the data in an array as an integer, however I am only getting around 30000 samples per second, and I was wondering how I could get this value up closer to the stated 100 ksps. I have set my code up very similarly to the demo-adcread.c file from github. Thanks!

19 days ago

Posted by:
andrew

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

Hello

The 100 kps speed is the maximum rated speed of the ADC chip based on the datasheet for the MCP3202 ADC.   On the Raspberry Pi 4 with the C library 30 ksps will be close to the limit of the ADC performance. 

You may get a small increase in speed if you used assembly language but you still have all of the processes running in linux competing for CPU cycles so it would be difficult to get the full rated speed for the ADC chip.

I will updated the ADC-DAC Pi page with a list of ADC read speeds for each programming language to make it clearer what can be expected when developing software with the board.

AB Electronics UK Tech Support

18 days ago

Posted by:
Samp38

Users Avatar

Just wondering if there is any way it can be sped up by using the pigpio library?

18 days ago

Posted by:
andrew

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

The example code for the MCP3202 on the pigpio website runs at 24968 sps on a Pi 4.

I will have a look at our C library and see if I can find any ways to optimise the reading speed.

AB Electronics UK Tech Support

18 days ago

Posted by:
andrew

Users Avatar

Location:
United Kingdom

Users Twitter  Users Website  

I have spent most of the day trying to make the C library faster and unfortunately due to the way the SPI driver works in Linux it does not look like it will be possible to get the full speed out of the ADC.

The screenshot from my logic analyser below shows the problem.  The linux SPI driver sets the enable line low early and waits more than the length of an extra byte before setting it high so you loose the equivalent time of two bytes of data on each sample.  This seems to be a feature of the SPI driver and I can not find a way around the problem.  Once the enable line does go high it takes approximately 22us for the library to process the data before starting the next loop.  I have tried trimming the code down to the bare minimum but I couldn't get the time much below 22us so I think this is the limit of the C library and the Raspberry Pi.

Post image

The pigpio library gets around this problem by bit-banging the SPI bus instead of using the SPI driver which may allow a faster sample rate but as this could make the C library incompatible with existing code as well as other hardware platforms I don't want to make the change to use pigpio on our C library.

You could try using the pigpio library and example code for the MCP3202 in your project and see if it gives you any better results.

Another alternative would be to connect the ADC-DAC Pi to a separate microcontroller like an Arduino.  The Arduino could read the samples from the ADC and send the data to the Raspberry Pi via the I2C or UART port.  The main advantage this is the low level of hardware access would give you consistent timing for the sample reads.  The Arduino would also only have a single task running on the microcontroller where as the Raspberry Pi has dozens of other processes taking up CPU time. 

With an Arduino Uno or any other 5V Arduino you could run the ADC-DAC Pi at 5V allowing you to use a faster clock speed on the SPI bus.  A 5V Arduino would need to use a level shifter for the connection between the Arduino and the Raspberry Pi to drop the 5V bus down to 3.3V.

AB Electronics UK Tech Support

12 days ago

Posted by:
jayben

Users Avatar

If you use DMA, you should be able to get accurately-timed samples at full speed, see my blog post https://iosoft.blog/fast-data-capture-raspberry-pi/

I used the MCP3008 ADC, but the modifications to drive an MCP3202 shouldn't be too difficult. If you can lend me a board, I could give it a go...

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.