Support Forum

Share your projects and post your questions

Register   or   Sign In
The Forum

Maximum sampling for DACADC Pi

1378 Views - Created 17/05/2019


Posted by:

tfv3 Avatar

I am using a rather simple sample code to write into and read from analog IOs:

    t1 = time.time() # start time in s    d=np.empty([10000])    for i in range(10000):        adcdac.set_dac_voltage(1, 1.5*i/10000) #set the voltage on channel 1         d[i]=adcdac.read_adc_voltage(1, 0)    t2 = time.time() # end time in s    dt = t2 - t1 # time delta

The measured time is approx 1 s, which means that I can transfer 10000 IO pairs (one write and one read) in 1 second. Read only takes 0.62 s for 10.000 reads. So this is all I can achieve, which is far off the 70 kHz communicated somewhere else?


Posted by:

andrew Avatar

United Kingdom

andrew Twitter  andrew Website  

The performance problems you are having are coming from using Python.

Python is an interpreted language which has a far lower performance than compiled programming languages like C or C++.
To get the most performance out of the ADC DAC Pi I would recommend using a compiled programming language like C.

To give you an idea of the performance difference I rewrote your program using the C version of our ADC DAC Pi library and ran it on a Raspberry Pi B 3+. The C program takes approximately 22.3 milliseconds to complete the same 10000 loops as your python program.

The code for the C program is shown below. You can download the ADCDAC Pi C library from our Github repository.

/* * test.c * compile with "gcc ABE_ADCDACPi.c test.c -o test" * run with "./test" */#include <stdio.h>#include <stdint.h>#include <sys/time.h>#include <time.h>#include <unistd.h>#include "ABE_ADCDACPi.h"void clearscreen (){    printf("\033[2J\033[1;1H");}int main(int argc, char **argv){	setvbuf (stdout, NULL, _IONBF, 0); // needed to print to the command line	struct timeval t1, t2;	double dt;	double d[10000];    set_dac_gain(1);	// start timer	gettimeofday(&t1, NULL);	int i;	for (i = 0; i <= 10000; i++){		set_dac_voltage(1, 1.5*i/10000);        d[i] = read_adc_voltage(1, 0);	}	// stop timer	gettimeofday(&t2, NULL);	// compute and print the elapsed time in millisec	dt = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms	dt += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms        printf("%G",dt);	return (0);}

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.