Basically, when I try to analodRead() as quickly as I can I am seeing a drop in voltage to the analog pin. The faster I try to read, the greater the drop.
My project is to build a system capable of measuring rapid deceleration as a falling object hits a target. The deceleration I want to track occurs in about 10-20 milliseconds so I want to read the data at around 20000 sample per second.
Running the code below on my Arduino Uno reads the analog input at a maximum of about 9000 samples/sec - not good enough. So I got myself a Teensy 3.1 which manages well over 100000/sec! Very nice.
So I have the ADXL377 accelerometer (Adafruit product ID 1413) hooked up to a Teensy 3.1. I'm only interested in acceleration in one dimension so pins X and Z on the accelerometer aren't connected. Pin Y goes to Teensy pin 16 (A2) and the 3V pin on the ADXL377 goes to Aref on the Teensy.
I've powered this setup from both the USB cable and a 9V battery run through a 5V regulator (Adafruit product ID 1065) using bluetooth for the serial interface. Different power supplies don't change these results.
The table shows the data for the first 40 analogRead()s from both the Arduino Uno with no delay and from the Teensy 3.1 with various delays in the code. For zero delay the line of code was commented out.
The ADXL377 was sitting on a flat surface when all of these readings were taken and I have confirmed it is operating correctly by tilting up and down and observing a small change in analogRead().
I've also recorded the A2 pin input from the centre of a voltage divider made from two 100KOhm resisters between the Teensy's 3V output and GND. There was much more noise than I saw from the ADXL377 but there was no reduction in analogRead() even with no delay.
Any ideas?
Code: Select all
const int accelPin = A2;
const int samples = 100;
int count;
char c[21];
unsigned int accel[samples];
float startTime,finishTime,intTime;
String data;
String readInput(){
while(Serial.available() == 0){
}
Serial.readBytesUntil('*',c,20);
return c;
}
void startTest(){
Serial.println("Starting...");
startTime = micros();
for(count=0; count<samples; count++){
accel[count] = analogRead(accelPin);
// Delay value to slow down the sample rate
delayMicroseconds(10);
}
finishTime = micros();
Serial.println("Completed.");
}
void showData(){
Serial.print(1000000*samples/(finishTime-startTime),0);
Serial.println(" samples per second");
Serial.println();
Serial.println("Time (ms),ADXL377");
intTime = (finishTime-startTime)/samples/1000;
for(count=0;count<samples;count++){
Serial.print(count*intTime,3);
Serial.print(",");
Serial.println(accel[count]);
}
}
void setup(){
analogReference(EXTERNAL);
Serial.begin(9600);
}
void loop(){
data = readInput();
switch(data[0]){
case '1':
startTest();
break;
case '2':
showData();
break;
default:
Serial.println("Incorrect");
}
}