High-Speed Arduino A2D Sampling
Sometimes you may find a need to sample an analog sensor at high sppeds. For instance, you may be trying to read voltages in an antenna for low-frequency communications, or you may want to capture vibrations in an acceleometer at high resolution. If so, the question arises as to just how quickly you can do analog sampling on an Arduino? To try to answer that question, this project used an Arduino Mega with an ADXL330 accelerometer, as shown in Figure 1. The use of an accelerometer was arbitrary, chosen simply as something to sample quickly. Even so, there are some things that we need to understand about the circuit. Firstly, we will only be sampling the z-axis. Secondly, as we will be sampling at the closest time interval allowed by the Arduino, we can naturally expect the analog measurements to be lower quality than usual. Thirdly, the accelerometer may not be able to properly react to very quick changes in acceleration, so this circuit isn't meant as a fast vibration sensor. All we intend to do is to see just how fast we can sample a single analog channel, and hopefully have some fun learning about the Arduino at the same time.
Figure 1. The accelerometer on an Arduino Mega.
To determine the analog sampling speed, the first thing we need is some Arduino code, which you can get by clicking here. You will see in the code that we have two functions: one takes 200 analog readings and stores them in a buffer before sending them to the serial line (fast, buffered), and the other does the same but sends the data to the serial line immediately after each reading (slow, unbuffered). We would obviously expect the fast, buffered, function to allow much faster sampling times, as it doesn't have the overhead of having to write lines of serial data between each sample. In fact, as you will see in Figure 2, it's about five times faster. Using a buffer the Arduino managed to sample one analog input pin 200 times in 22 milliseconds, with a serial transmission speed of 115200bps (bits per second). Put another way, that's 200 samples in a fiftieth of a second, or one sample every ten thousandths of a second! Quite fast then!
Figure 2. Comparison of sampling speeds at 115200bps.
On that basis, you would surely expect that serial transmission speeds are important if a buffer isn't used. To investigate that different speeds were set on the Arduino and the graph of Figure 3 drawn using the results. As you would expect, it's obviously important to select as high a speed as possible if you want to send unbuffered analog sample data over a serial or USB cable to a PC. Here the lowest speed shown in 2400bps, but that's only because the time taken for 200 samples increased considerably at lower bps values. In fact, at around 300bps, the time taken was around 30 seconds!
Figure 3. Comparison of sampling speeds for a range of serial communication speeds.
You may be wondering what a high speed sample looks like from the accelerometer. Well, it's not that exciting because the 22 milliseconds doesn't allow for much to happen when all you can really do is shake the circuit. However, a result is shown in Figure 4 as an example. That really implies that for high-speed analog sampling you would most likely need to rig up some kind of trigger on an Arduino pin to start the sampling at just the right time. Also, our maximum speed requires a buffer, but for sending data continuously you will be limited to an unbuffered maximum of about 200 samples every 120 milliseconds at 115200bps. Finally, it's important to remember that this was a just for fun test, and could be faster if the data isn't output as decimal numbers, so isn't definitive. So, why not see if you can do better :-)