Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Delay between multiple analogReads

I'm trying to read 4 analog inputs (1kHz speed). I changed the prescaler to 16 for my Arduino Leonardo according to this link. I then tried to read the analog pins and I used the following code to display the time and the readings:


#define FASTADC 1
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
double t = 0;
void setup() {
 int start;
 int i;
 #if FASTADC
 // set prescale to 16
 sbi(ADCSRA, ADPS2);
 cbi(ADCSRA, ADPS1);
 cbi(ADCSRA, ADPS0);
 #endif
 Serial.begin(9600);
 Serial.print("ADCTEST: ");
 start = millis();
 for (i = 0 ; i < 1000 ; i++)
 analogRead(0);
 Serial.print(millis() - start);
 Serial.println(" msec (1000 calls)");
}
// the loop routine runs over and over again forever:
void loop() {
 // print out the value you read:
 t = millis() / 1000;
 Serial.print(t);
 Serial.print('\t');
 Serial.print(analogRead(A0)); delay(1);
 Serial.print('\t');
 Serial.print(analogRead(A1)); delay(1);
 Serial.print('\t');
 Serial.print(analogRead(A2)); delay(1);
 Serial.print('\t');
 Serial.println(analogRead(A3)); delay(1);
}

I connected 2V from the DC supply to all the 4 channels and the readings were similar. I used the output from the serial monitor and pasted the readings for 1 sec in Excel to see the frequency. With a 9600 baud rate and no delay between the readings I got 200 Hz (for the 4 analog inputs).

Why is that happening? And is there a relationship between the sampling frequency and the baud rate? Is my way of finding the frequency correct? If not what's the right way?

Finally, I'll be plotting the readings in Matlab, so I guess this will affect the sampling frequency. How can I control it so that I'm sampling with 1kHz rate for all the channels?

Here is the screenshot of the readings from the 4 channels (2V input).

enter image description here

Summarizing everything, I need to know the following:

  1. The effect of adding a delay between every two readings on the accuracy of the readings and the sampling frequency.

  2. How to find the best sampling frequency for every input and how to control it?

  3. Will using Matlab to plot the data affect the sampling frequency?

Thanks.

EDIT: I accepted the answer below, if anyone has the same problem , please make sure that you change the prescaler as I mentioned above for the best results.

Answer*

Draft saved
Draft discarded
Cancel
1
  • 1) You wrote "you DON'T want to do anything with the Serial object in setup() except configure it. (There is an under-the-hood reason for that.)". Could you elaborate? 2) You cannot get good control of the sampling rate with delayMicroseconds(), because the loop time is the delay plus the time needed to execute the code. Using micros(), like in @frarugi87's answer, is the standard method and it works well. Only a timer-triggered analog read will give you better accuracy (less jitter). Commented Jan 24, 2017 at 9:25

AltStyle によって変換されたページ (->オリジナル) /