it to debug my code all the time.
My favorite scope is the Digilent Analog Discovery
scope. This is a two-channel, 10 MHz bandwidth, 100
Msample/sec, USB scope with 14-bit vertical resolution
and a built-in two-channel arbitrary wave generator. It also
has a 16-channel logic analyzer. The user interface is
incredibly easy to use and incredibly versatile, and at
$279, it’s a steal.
The 10 MHz analog bandwidth is a perfect range for
the Arduino. With this receiver bandwidth, we can see
rise times on the order of 0.35/10 MHz = 35
nanoseconds. Figure 8 is a screenshot of the zoomed in
rising edge of a toggling digital output pin on my Arduino
showing a measured 10-90 rise time of about 16 ns —
well below the estimated limit of 35 ns. Note that the
acquisition rate is only 100 Msamples per second (or one
sample every 10 ns), so there is a bit of interpolation
used to get the 16 ns rise time. This is plenty fast for most
The RedBoard Arduino has an onboard 16 MHz
clock generated by a very precise crystal. A single clock
cycle is about 60 ns. In another experiment, I found that
by using the PORTB command, I could toggle eight of the
digital output pins off and on in four clock cycles, or in
250 ns. This sort of time is overkill for any typical data
acquisition I had in mind, and I wanted to leverage the
The first question I wanted to explore was just how
fast can I toggle an output pin off and on using the
digital Write command. I set up a sketch to just have the
two commands digitalWrite( 13, LOW) and then
digitalWrite( 13,HIGH) inside the void loop() function to
turn output pin 13 off and then on. This code is shown in
I used the venerable pin 13 which also flashes the
LED on the board. Figure 10 is a snapshot of the screen
of the Analog Discovery scope showing the voltage at the
output of pin 13. The pin is set low, then the next
command is to write it high. In the scope trace, you can
see the low lasts for 5. 25 µs. This is about 90 clock cycles.
Did I mention that the Analog Discovery scope also has
over 50 math functions allowing high resolution waveform
feature measurements? In this case, I extracted the off
time and on time of the digital signal using a simple math
function and displayed the extracted values in real time.
In this measured output, we see that the pin stays low
for 5. 25 µs, and then the output HIGH command is
executed. This lasts for 7. 18 µs, which includes the
execution time for the loop command. Overall, this loop is
executed in 12. 43 µs. Altogether, this is about 200 cycles.
This is plenty fast for my testing.
Adding comment lines to the code has no impact on
the execution time. In fact, I wrote the toggle commands
in a function and just called the function in the void loop()
function. The execute time went up to 12. 62 µs. This is
only 190 ns of overhead to execute a function call, or
about three clock cycles. Function calls are pretty fast.
We can use the overhead time as a starting place
estimate; the overhead time to turn off pin 13 and turn it
on, that’s about 12. 4 µs. Now, we can start using it to help
decode the timing for executing other functions like
reading the analog pins and writing to the serial port.
How Long Does It Take to
Read an Analog Pin?
I set up my initial experiments to read three analog
channels. I connected some simple sensor inputs just so I
June 2015 39
FIGURE 9. Code snippet to toggle pin 13 off then on
as fast as possible.
FIGURE 10. Screenshot for the Analog Discovery scope
measuring the voltage on pin 13, showing the on and
off time. Inset is the on-screen real time measurement
of the waveform features.
FIGURE 11. Code snippet to use the digital write pins as
timers when reading three analog channels.