Support Forum

Share your projects and post your questions

Register   or   Sign In
The Forum

Need faster sampling rate from ADC DAC Pi Zero

The ADC-DAC Pi Zero is a 2 Channel ADC and 2 Channel DAC for the Raspberry Pi

08/09/2020

Posted by:
Samp38

Samp38 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!

08/09/2020

Posted by:
andrew

andrew Avatar

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 update 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.

09/09/2020

Posted by:
Samp38

Samp38 Avatar

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

09/09/2020

Posted by:
andrew

andrew Avatar

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.

09/09/2020

Posted by:
andrew

andrew Avatar

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 for more than the length of an extra byte before setting it high so you lose 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.

forum 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 of 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 whereas 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.

15/09/2020

Posted by:
jayben

jayben 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...

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.

Home

Shop

Learn

Forum

FAQ

Contact

0 item

Your cart is empty

Please browse our shop to order from the wide range of Raspberry Pi boards and accessories.

Subtotal:£0.00
View Basket Continue to Checkout