Using Arduino as DAQ? (Help with sampling)
hi,
first off thank arduino community indirect project far! learned coding on arduino far has been forums. senior biomedical engineering major comfortable topics such dsp, nyquist, c programming , such; therefore consider myself intermediate user. know want arduino do, don't know how code it. anyways task @ hand.
what i'm trying sample analog signal (microphone custom pre-amp filter) , output each sample binary on bluetooth computer or cell phone it's collected. pre-amp filter filter out frequencies on 2000hz , bias around 0-5v, therefore need sample @ 4000hz or greater. signal once on computing platform, plotted , analyzed using frequency analysis. reason i'd sampling continuous opposed being stored in array because computing platform continuously plot data comes in (similar ecg).
the problem having cannot figure out how control sampling rate consistent. @ first let while loop run uninhibited, sample period vary little between samples. thought implementing if statement in loop sample when difference between micros() , last sample 236us (4237hz), sampling rate of 4166.66hz....?
my question there other better method sample @ consistent sampling rate? or problem how calculating sampling rate, , in actuality sampling @ prescribed rate?
i have attached code far reference. test, use putty , a0 connected 3.3v rail. thanks,
cody
first off thank arduino community indirect project far! learned coding on arduino far has been forums. senior biomedical engineering major comfortable topics such dsp, nyquist, c programming , such; therefore consider myself intermediate user. know want arduino do, don't know how code it. anyways task @ hand.
what i'm trying sample analog signal (microphone custom pre-amp filter) , output each sample binary on bluetooth computer or cell phone it's collected. pre-amp filter filter out frequencies on 2000hz , bias around 0-5v, therefore need sample @ 4000hz or greater. signal once on computing platform, plotted , analyzed using frequency analysis. reason i'd sampling continuous opposed being stored in array because computing platform continuously plot data comes in (similar ecg).
the problem having cannot figure out how control sampling rate consistent. @ first let while loop run uninhibited, sample period vary little between samples. thought implementing if statement in loop sample when difference between micros() , last sample 236us (4237hz), sampling rate of 4166.66hz....?
my question there other better method sample @ consistent sampling rate? or problem how calculating sampling rate, , in actuality sampling @ prescribed rate?
i have attached code far reference. test, use putty , a0 connected 3.3v rail. thanks,
cody
code: [select]
/*********************************************************************************************
* *
* this program reads 'n' data points @ a0 port , sends out value *
* over spp. calculates sampling rate of each sample. *
* *
*********************************************************************************************/
const byte analoginpin = 0; // a0
const byte sampleperiod = 236; // sampling period in us, must multiple of 4
unsigned int sensorvalue = 0; // value of microphone
unsigned long int = 0; // number of samples
char test = 's'; // controls when program executed
unsigned long initial = micros(); // time sample collected
unsigned long scratch1 = micros(); // used calculate sampling rate
unsigned long scratch2 = micros(); // used calculate sampling rate
byte scratch = 0; // used control sampling
float rate = 0;
void setup() {
serial.begin(115200); // initialize serial communications
pinmode(13, output);
serial.print("forced sampling @ ");
serial.print(sampleperiod);
serial.println("us");
}
void loop() {
test = serial.read();
if (test=='r') {
serial.println();
serial.println("start:");
initial = micros();
i = 0;
scratch1 = micros(); // start of data collection
while(test != 's'){ // if input 's' serial computer exit loop
scratch = micros() - initial;
if (scratch == sampleperiod) {
initial = micros();
sensorvalue = analogread(analoginpin); // read in value
serial.write(highbyte(sensorvalue)); // output msb of int in binary
serial.write(lowbyte(sensorvalue)); // output lsb of int in binary
i++;
test = serial.read(); // read value serial
}
}
scratch2 = micros(); // end of data collection
rate = i/(0.000001*(scratch2-scratch1)); // calculate sampling rate
serial.println();
serial.println("end");
serial.print(i);
serial.println(" total samples");
serial.print("calculated sampling rate: ");
serial.print(rate);
serial.println("hz");
}
else { // flash led while waiting input
digitalwrite(13, high); // set led on
delay(100); // wait
digitalwrite(13, low); // set led off
delay(100); // wait
}
}
please don't cross post.
http://arduino.cc/forum/index.php/topic,100963.msg757022.html#msg757022
from reference page micros():
" function has resolution of 4 microseconds (i.e. value returned multiple of four). "
1 / 4166 = 240µs. looks 4µs off of 236µs value....
http://arduino.cc/forum/index.php/topic,100963.msg757022.html#msg757022
quote
last sample 236us (4237hz), sampling rate of 4166.66hz....?
from reference page micros():
" function has resolution of 4 microseconds (i.e. value returned multiple of four). "
1 / 4166 = 240µs. looks 4µs off of 236µs value....
Arduino Forum > Using Arduino > Programming Questions > Using Arduino as DAQ? (Help with sampling)
arduino
Comments
Post a Comment