Pages: [1]   Go Down
Author Topic: Fast sampling ADC  (Read 4928 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 112
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 28, 2011, 12:37:56 pm by bastukee » Logged

No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13085
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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();
  }
}

Logged

0
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2855
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1673
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14048
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

See also - http://arduino.cc/forum/index.php/topic,61381.msg444421.html#msg444421 -
« Last Edit: July 01, 2011, 08:14:51 am by robtillaart » Logged

Rob Tillaart

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

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1673
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12717
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

[ I won't respond to messages, use the forum please ]

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14048
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged

Rob Tillaart

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

0
Offline Offline
Full Member
***
Karma: 0
Posts: 112
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27425
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27425
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://ww1.microchip.com/downloads/en/DeviceDoc/22127a.pdf as an example

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

0
Offline Offline
Full Member
***
Karma: 0
Posts: 112
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Crossroads

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...?
Logged

No trees were harmed by the transmission of this message. However a few million electrons were temporarily inconvenienced.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27425
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: