Go Down

### Topic: Fast sampling ADC (Read 10616 times)previous topic - next topic

#### bastukee

##### Jun 28, 2011, 07:35 pmLast Edit: Jun 28, 2011, 07:37 pm by bastukee Reason: 1
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.

I could also use many Arduinos to do it too.
No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

#1
##### Jun 28, 2011, 07:42 pm
Quote
I'm willing to use the fastest baud-rate possible to achieve this and if there is a better option than using the Arduino.

Teensy...
http://www.pjrc.com/teensy/

Quote
I would like to know how fast can I sample a waveform (1ms? 1us?)

Test to find out...

Code: [Select]
`unsigned long Start;unsigned short Count;void setup( void ){  Serial.begin( 115200 );  Start = millis();}void loop( void ){  if ( millis() - Start < 1000 )  {    analogRead( 0 );    ++Count;  }  else  {    Serial.println( Count );    Count = 0;    Start = millis();  }}`

#### RuggedCircuits

#2
##### Jun 28, 2011, 07:48 pm
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

#### fat16lib

#3
##### Jun 29, 2011, 05:39 pm
I been using Microchip ADCs to sample signals and store the data on SD cards.

I have been able to record 12-bit data at 40,000 samples per second using a MPC3201 ADC.  A version of this sketch is here http://code.google.com/p/beta-lib/downloads/list in the file fastLoggerBeta20110623.zip.

I have used a MPC3001 to record 8-bit data at 80,000 samples per second and will post that sketch soon.

I have used the Arduino ADC to record 8-bit audio at 44,100 samples per second in this library http://code.google.com/p/waverp/.

It's not real-time but you can record data for long periods.

#### robtillaart

#4
##### Jun 29, 2011, 07:22 pmLast Edit: Jul 01, 2011, 03:14 pm by robtillaart Reason: 1
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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### fat16lib

#5
##### Jun 29, 2011, 08:38 pm
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.

#### MarkT

#6
##### Jun 30, 2011, 06:16 am

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.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### robtillaart

#7
##### Jul 01, 2011, 03:26 pm
Quote
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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### bastukee

#8
##### Jul 01, 2011, 08:45 pm
Thank you all for the information provided!

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.
No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

#9
##### Jul 01, 2011, 08:54 pm
EEPROM is slow - store it in much faster SRAM instead, then transfer to EEPROM at your leisure if you want to keep it long term.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#10
##### Jul 01, 2011, 09:01 pm

Digikey has a bunch
http://search.digikey.com/scripts/DkSearch/dksus.dll?Detail&name=23K256-I/P-ND
You'll have to do some level shifting due to 3.6V, I think 74AC125 like on a SD card interface can handle that for you.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### bastukee

#11
##### Jul 01, 2011, 10:25 pm

That SRAM chip might just do it for me!

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...?
No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

#12
##### Jul 01, 2011, 11:24 pm
"I wonder how you guys manipulate the sampling rate of the Arduino's ADC? If this even helps...?"
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### jlsilicon

#13
##### Sep 22, 2016, 05:41 pmLast Edit: Sep 22, 2016, 06:16 pm by jlsilicon
Sample Rate (from the Prescaler) can be up to the Maximum, but with loss of Resolution Bits
- No damage done of course ... somebody posted this ridiculous statement/question somewhere.

Code: [Select]
`ARDUINO ADC Speeds (with 16MHz Clock) : Speed: PreScaler: Time: Freq: ActFrq: BitsRes:  120KHz 128 116us 9.6ksps 9.6ksps 10b 240KHz 64 60us 19ksps 19ksps 10b 500KHz 32 36us 38ksps 38ksps 10b 1MHz 16 20us 77ksps 50ksps 9b 2MHz 8 13us 154ksps 77ksps 8b 4MHz 4 9us 308ksps 111ksps 6b 8MHz 2 7us 615ksps ? ?ARDUINO ADC Speeds (with 20MHz Clock) : Speed: PreScaler: Time: Freq: ActFrq: BitsRes:  1MHz 16 90ksps 9b 2MHz 8 170ksps 8b 4MHz 4 350ksps 6b 8MHz 2 615ksps ?ARDUINO ADC Speeds (with 32MHz Clock) : Speed: PreScaler: Time: Freq: ActFrq: BitsRes:  1MHz 16 150ksps 9b 2MHz 8 300ksps 8b 4MHz 4 600ksps 6b 8MHz 2 1.2Msps ?`

Good explaination here:

5Msps (yes 5Mega) using Ca3306 with Arduino:
https://digibird1.wordpress.com/arduino-as-a-5m-sample-osciloscope/

References (great High Speed ADC directions for AVR/Arduino) :