Very basic question, but I can't reconcile what I've been reading in other posts with what I see in my own experiments.
In an introductory teaching lab, I would like to illustrate the ability of Arduino (Uno) to acquire analog signals, convert them to digital, and send them to a computer. This should also show the limitations (ADC resolution with 10 bits, as well as maximum sampling / transfer frequency). To this aim, I hooked up a signal generator set to a sine wave, 200mV peak to peak, offset by 300mV to be sure that the signal is always positive, and I vary the frequency.
The signal generator is connected to A0 on the Arduino, and I monitor the signal on the Serial Monitor of the Arduino IDE. It works fine: I can see the discrete levels due to the ADC resolution, and for low frequencies I get a sine wave that looks just like the one on the oscilloscope. However, I am puzzled with the fact that after about ~300Hz, the sine wave already shows significant degradation due to poor sampling rate. I would have expected this to happen at a substantially higher frequency.
I changed the baud rate for the Serial connection, assuming this was the problem, but even at the maximum option available (2000000 bauds) input signals above ~300Hz don't seem to be well resolved in time. I wonder what I'm doing wrong, and how I should make sense of the results, and possibly improve the situation (though it's not critical: I don't actually need high transfer rate, but I would like to understand where the limitations come from).
Basic code, slightly edited from the AnalogReadSerial example:
void setup() {
Serial.begin(2000000);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}
I moved your topic to an appropriate forum category @baptnz.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
Umm. With a 1 ms delay, plus the actual execution time, you're likely not going to sample much faster than 800, maybe 900 samples per second. For 300 Hz, that's barely above Nyquist.
Good point – I didn't have the code with me so I copied over the AnalogSerial example and kept this line by mistake in my post. I'd commented it out in my experiments, but you're right: I did not think enough of what a decent digitisation would require – I just saw a clear degradation after 300Hz and that seemed strange. After doing more experiments, and checking the actual timestep between measurements, it makes more sense.
If you use the default settings on a 5V Arduino, the resolution of the ADC is about 5 mV/step. You can use another ADC reference, such as the internal 1.1 V reference, to get about 1 mV/step.
You need to individually calibrate the internal reference as the accuracy is +/- 10%, but it is very stable. Of course, input signal voltages above the ADC reference are converted to 1023.