Need faster sampling rate from ADC DAC Pi Zero
1778 Views - Created 08/09/2020
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.
I will have a look at our C library and see if I can find any ways to optimise the reading speed.
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.
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.
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.