I need to sample an analog signal that's a few hundred Hz. According to the AnalogRead reference page, it samples at 100us. I've been experimenting with a function generator, and it's drastically slower...around 5ms per read. I wrote this simple program:
int value = 0;
int input = A0;
void setup() {
Serial.begin(9600); // allows for Arduino to print to the screen
}
void loop() {
value = analogRead(input); // read analog input
Serial.println(value); // print to screen
}
Is printing to the screen causing it to slow down? If not, what else could be wrong? I'm very new to arduino and home electronic projects, so the more details, the better.
Also, I'm using an Arduino Uno with an ATMEGA328P-PU microcontroller.
Yes, printing and whatever code in the loop slow down interval of your readings.
There are two option:
if you can allow gaps in your reading, than make sampling input by analogread function in the for-loop, putting samples in the array ( < 800 int ), and process data right after that.
if readings must to be continuous, make sampling with analogread in the timer interrupt routine, setting frequency for your needs - Nyquist.
One simple solution to the serial issue is to run at a higher speed.
Crank it up to 57600 and it won't take nearly as much time as at 9600!
You can go all the way up to 115200 if you want.
Serial speed can even be much higher but that is not supported by the IDE. I have succesfully ran for weeks at 345600, and tested some other "non standard" speeds.
I tend to use 57600 most of the time, because I think that's the highest reliable speed on an 8MHz Arduino like the Pro Mini, otherwise I'd use 115200 all the time. There is seldom if ever a reason to use 9600 these days.
And analog conversion takes 13 ADC clocks and ADC clocks default to 125kHz (16MHz / 128) - this can be changed - setting it twice as fast means a slight loss in accuracy though.