I'm currently trying to use the Arduino's ADC to sample a waveform that will be between 0V-5V and transmit the samples over serial so that it can be recreated on a PC running some plotting software. I would like to know how fast can I sample a waveform (1ms? 1us?) and be able if possible, to transmit this data real time. Another option would be to store about 8 seconds worth of sampling and transmitting such samples. How fast could I sample? I'm willing to use the fastest baud-rate possible to achieve this and if there is a better option than using the Arduino.
The fastest you can sample is limited by the ADC clock, which can be as fast as the main clock frequency divided by 2, or 8 MHz. Now, at that sampling frequency your results will not be accurate! In addition, the datasheet says the ADC clock should not exceed 1 MHz (I'm guessing results will just be ridiculously wrong beyond that point). The "recommended" ADC clock frequency to meet datasheet parameters is 200 kHz or less (many of the ADC parameters are specified at 200 kHz). Another parameter of note is the "input bandwidth", which has a typical specification of 38.5 kHz so (according to Nyquist's theorem) there's no information beyond 77 kHz of sampling (though it is beneficial to sample faster than that if your signal truly does have spectral content up to 38.5 kHz).
OK, so with that out of the way, let's say you settle on 200 kHz, that means you are sampling data every 5 microseconds. That's way too fast for serial. At the 230.4 kbps baud rate and 8 bits per sample (throwing away the 2 LSB's of the 10-bit sample) you could transmit 23040 samples per second (10 bits per 8-bit data packet over serial) if your code was 100% efficient. So there's no need to even think about the 200 kHz sampling frequency number.
Storing data has a similar tradeoff. Assuming you use an Arduino Mega with 8K of RAM, set aside 1k for the C stack and other stuff, you could store (7*1024)/200,000 = 35.8 milliseconds worth of data at 200 kHz sampling rate. If you want to store 8 seconds using 7k of RAM you will have to sample at 896 Hz.
Just some numbers to get you thinking.
--
The Ruggeduino: compatible with Arduino UNO, 24V operation, all I/O's fused and protected
With respect to the baudrate, the Arduino serial monitor can do 115200, but the arduino can do much faster. I have done some test with 230400 and 345600 baud (using putty.exe to capture the output on windows 7. A dozen tests worked well for approx 24 hours per test without loss of data. -
@ 345600 you can send approx 15000 (2byte) samples per second! - Give it a try
If you need 10 bit resolution, the Arduino ADC is a key limit. For full resolution on a 16 MHz CPU, the maximum ADC clock is 125 kHz. It take 13 cycle for a conversion so you are limited to about 9,600 conversions per second.
It will be much less than that if you include Serial overhead.
RuggedCircuits:
The fastest you can sample is limited by the ADC clock, which can be as fast as the main clock frequency divided by 2, or 8 MHz. Now, at that sampling frequency your results will not be accurate! In addition, the datasheet says the ADC clock should not exceed 1 MHz (I'm guessing results will just be ridiculously wrong beyond that point). The "recommended" ADC clock frequency to meet datasheet parameters is 200 kHz or less (many of the ADC parameters are specified at 200 kHz). Another parameter of note is the "input bandwidth", which has a typical specification of 38.5 kHz so (according to Nyquist's theorem) there's no information beyond 77 kHz of sampling (though it is beneficial to sample faster than that if your signal truly does have spectral content up to 38.5 kHz).
OK, so with that out of the way, let's say you settle on 200 kHz, that means you are sampling data every 5 microseconds.
There seems some confusion here - the ADC does not sample at the ADC clock rate. It takes something like 13 ADC clock cycles to convert one sample (one clock for each result bit and some setup-overhead). An ADC clock of 200kHz means max sample rate around 13kHz. The default arduino setting is ADC clock = 125kHz, so about 100us per conversion. This is about 100kbit/s, just beyond the capability of serial with baud rate of 115200.
The datasheet suggests using an ADC clock of 50kHz to 200kHz for good results.
This is about 100kbit/s, just beyond the capability of serial with baud rate of 115200.
As stated above baudrate can go 3x higher, but that still leaves a gap of a factor 3-4. So you should investigate if the signal can be compressed if you want to send it over serial.
If the signal does not change fast or often runlength encoding can be interesting. Note with the help of a local buffer "peaks" of fast changes can be handled too.
If signal does change often but not by a big amount consider sending the delta in one signed byte. Keep one value free for "here comes a new 16 bits value" flag
I have to say I will need to attack this on a different approach than I initially intended. This is what I'm trying to do...
A signal comes in, which will be between 0V and 5V. The signal is between 1ms and 5ms long. I would be sampling this as fast as possible, storing the values and later sending them over a wireless serial radio. So the main thing then would be a way to store as many samples as possible during that short amount of time. If the Arduino could simply store those samples in it's memory then great! But I think it is beyond it's memory capacity, but I could use external EEPROM to store the information and send it after the signal has finished. How fast could I sample then if I don't have to worry about the baud rate, only storing on EEPROM ?
I see you guys mention how fast the ADC on the Arduino samples, I was not aware that you could manipulate the sampling rate of the ADC on the Arduino, any light on this?
I also like the idea of a faster baurdrate to help do most of this on a real time basis, I might end up doing this on a separate project for signal detection.
After I get some SRAM, I could just do some simple code to read and write over SPI as fast as possible for say 5ms and see how many times it does it. I wonder how you guys manipulate the sampling rate of the Arduino's ADC? If this even helps...?
"I wonder how you guys manipulate the sampling rate of the Arduino's ADC? If this even helps...?"
Did you take a look at link in reply #3? Bet there are some answers there.