I have fez panda 2 and I was wondering if it’s possible to get more than 1000 analog readings per second?
Using RLP would provide more speed.
I’ve been developing a little profiler library and this is a good test
Here’s the code:
static void TestAnalogReadSpeed()
{
AnalogIn an0 = new AnalogIn(AnalogIn.Pin.Ain1);
int result;
TraceEx.Start("AnalogRead10000");
for (int i = 0; i < 10000; i++)
{
result = an0.Read();
}
TraceEx.Stop();
an0.Dispose();
}
and the result:
#AnalogRead10000 Start: [128752523315287108] Stop: [128752523323056636] Duration: [776952]us
So, I see a speed of 77.6 uS per reading - or 1,000,000 / 77.6 =
12886 samples per second.
But consider that is not doing anything with the data - just reading it.
If I now put the data into an array things look different:
static void TestAnalagReadSpeedBuffer()
{
AnalogIn an0 = new AnalogIn(AnalogIn.Pin.Ain1);
int[] samples = new int[1000];
TraceEx.Start("AnalogRead10000Buffer");
for (int j = 0; j < 10; j++)
{
for (int i = 0; i < 1000; i++)
{
samples[i] = an0.Read();
}
}
TraceEx.Stop();
an0.Dispose();
}
This is still 10,000 readings but the result is:
#AnalogRead10000Buffer Start: [128752530897179400] Stop: [128752530915877041] Duration: [1869764]us
That is 187 uS per sample or 5347 samples per second.
This one looks for a analog value above a certain threshold. I forced it through 10,000 cycles:
static void TestAnalagReadSpeedThreshold()
{
AnalogIn an0 = new AnalogIn(AnalogIn.Pin.Ain1);
int i = 0;
TraceEx.Start("AnalogReadThreshold");
while (an0.Read() < 999 && i < 10000)
{
i++;
}
TraceEx.Stop();
an0.Dispose();
}
Result:
#AnalogReadThreshold Start: [128752537591703917] Stop: [128752537612030595] Duration: [2032667]us
So, 203 uS per sample on that one, which is 4926 samples per second.
So it really depends on what you do with the samples and when you do it. The more you do in the read loop, the slower the sample rate becomes.
If you need a faster sampling rate into a buffer, use RLP to sample the data and NETMF to process it afterwards.
Hope this helps!
“…I have fez panda 2 and I was wondering if it’s possible to get more than 1000 analog readings per second?..”
=> Follow those examples above I think you absolutely can read more than 1000 times per sec in C#.
But because I am a fan of RLP so I recommend you should use RLP for any loop that is bigger than 1000 times :).
Great, that is what I was looking for. Thank you. I will post my complete project when I test it. You all were extremely helpful.