# How do i calculate the true sample rate of an ADC when it is being used in code?

Hello all,

If the Arduino is sequential processing, how do i calculate the true sampling speed of the ADC?

The arduino that i am using is the Arduino MEGA 2650 and have attached my code below

Do i calculate the length of time between each conversion and incorporate this into the speed?

The number of lines that will be executed is 6 between each ADC conversion. This I believe amounts to 6 instruction samples. 1 instruction sample is about 62.5ns. To run 6 instruction cycles would take 375ns. (and i am stuck here)

``````#include <SPI.h>
#include <string.h>

int val,k=0;
float sample=0;
int x=4000, i=0;
float temp,Vx,Vin,Vout,Vactual;
String c;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
}

float squared_voltage,sum_squared_voltage, mean, rms, rms1, Zmag;
int n;

void loop() {
// put your main code here, to run repeatedly:

for(n=0; n<500; n++)
{
Vactual = measure(temp); // this line converts the temp into the real actual value. You should test this piece with your circuit.
sample = Vactual;
squared_voltage = sample * sample;
sum_squared_voltage += squared_voltage;
}

mean = sum_squared_voltage/500.0;
rms = sqrt(mean);
sum_squared_voltage=0;
//         Serial.println(rms);

for(n=0; n<500; n++)
{
Vactual = measure(temp); // this line converts the temp into the real actual value. You should test this piece with your circuit.
sample = Vactual;
squared_voltage = sample * sample;
sum_squared_voltage += squared_voltage;
}

mean = sum_squared_voltage/500.0;
rms1 = sqrt(mean);
sum_squared_voltage=0;

Zmag = (rms/rms1)*9902;
String to_string(Zmag);
Serial.write(int(Zmag));

}

//  Serial.println(Vx,5);
Vin = (Vx - 2.111)*(118.0/18.0);
//  Serial.println(Vin,8);
return Vin;
}
``````

This I believe amounts to 6 instruction samples.

Time for a new belief system.

analogRead takes around 100us, and six lines of code does not equate to six instruction cycles.

If you want to know how long something takes, time it, preferably over multiple iterations.

``````float measure(int adc_value){
//  Serial.println(Vx,5);
Vin = (Vx - 2.111)*(118.0/18.0);
//  Serial.println(Vin,8);
return Vin;
}
``````

There seems to me to be very little point in returning something that is already a global.

Thanks but is there any way i can calculate the instruction cycle?

1 / 16000000 gives you the time of an instruction cycle.

Sorry, i meant the time it takes for one conversion with respect to my code.

Yes, I already told you - time (using millis () or micros() ) multiple iterations of the code.
Subtract the start time from the end time.

thanks.