It turns out the order in which you list these libraries is
important. The SPI MUST come before the analogShield.
resolution — or 4096 levels — spread over a 0 to 3V range.
The voltage resolution of each ADC in this case is
There are commands in the analogShield library that call
SPI commands. At the beginning of each sketch, make
sure to use these two include lines, IN THIS ORDER:
#include <SPI.h>
#include <analogShield.h>
3.3V/4095 = 0.8 mV. The analog shield has a resolution
that is about 5x more sensitive than the Due, but more
importantly, extends over a wider voltage range. This is a
huge advantage in giving a larger dynamic range for
measurements.
This was another issue I struggled with. Some of my
sketches worked fine, while others hung up during
uploading. The hidden variable was the order of the
include statements.
Reading a voltage from channel 0 and placing it into
the variable ADC0val_adu is as simple as using the
command ADC0val_adu = analog.read(0);. Writing the
same value to the DAC channel 0 is as simple as
analog.write(0, ADCval_adu);.
To verify it’s installed and working, open up
file/examples and look for the analogShield-master listing.
Out-of-the-Box Accuracy
Under it, you’ll see a few example sketches. Click on
“ramp.” You can run this to verify your computer talks to
your Due and DAS. Just remember to make sure the Due
(programming port) is selected under Tools/board, and the
correct port is selected and check-marked under
As a first test of the accuracy of the DAS, I wrote a
simple sketch which averages a bunch of consecutive
Tools/port.
ADC readings. The scaling is based on subtracting 2^15-1
or 32,767 as the 0 value, and then scaling the ADU value
by 10V/65535.
New Commands for the
DAS
Two new commands are introduced in this library to
read data from the ADC or to write data to the DAC
channels. While there are a number of advanced
commands, all that is required to get started are the two
commands:
I try to use functions as often as possible in my
sketches, and then just call the function in the void loop()
whenever needed. As a good habit, I always label
functions with a func_ in the front. This makes it obvious
what name is a function call or just a simple variable.
This function acts as a simple digital multimeter
(DMM) with the four channels. I named this function
func_uncalDMM_to_serialMonitor () to indicate this
• analog.read(chan) — Reads the ADC value of channel
“chan” (0 to 3) in units of ADUs
• analog.write(chan, val_adu) — Writes to the DAC
channel, “chan,” the value in ADUs
Note that the commands to read an analog voltage
from the native 12-bit ADC built into the Arduino Due is
analogRead(ID). These commands have a slightly different
syntax and so can be used in the same sketch without the
possibility of confusion. With the DAS attached, the native
analog channels are still available and can be accessed.
The values read or written to the channels are 16-bit
integers referred to in units of ADU (analog-to-digital
units). For these 16-bit channels, the values range from 0
to 2^ 16 – 1, which is 65535. In the Due, all integers of
any sort are stored as long, 32-bit. When initializing
variables, it’s a good habit to always use “long” instead of
“int” as this reminds you that they are 32 bits in size.
void func_uncalDMM_to_serialMonitor() {
// reads voltage on each channel
long nptsAve_local = 10000;
long nptsChan_local = 4;
for (i = 0; i <= nptsChan_local - 1; i++) {
arrV_meas_nChan_once_v[i] = 0.;
Serial.println(“”);
}
Serial.println(“attach a voltage to the
inputs”);
for ( i = 1; i <= nptsAve_local; i++) {
for (i2 = 0; i2 < nptsChan_local; i2++) {
arrV_meas_nChan_once_v[i2] =
((analog.read(i2) 1.) - 32767 1.) *
10./65535.+ arrV_meas_nChan_once_v[i2];
}
}
for (i2 = 0; i2 < nptsChan_local; i2++) {
arrV_meas_nChan_once_v[i2] =
arrV_meas_nChan_once_v[i2] / (nptsAve_local);
Serial.print(“Value for channel “);
Serial.print(i2); Serial.print(“: “);
Serial.println(arrV_meas_nChan_once_v[i2], 6);
}
}
One nice feature of the DAS is that it is bipolar. The
ADC is able to measure voltages from -5V to +5V, and the
DAC is able to output voltages from -5V to + 5V. The
65,535 different voltage levels are mapped over this
voltage range. Each ADU level corresponds to 10
V/65535 = 0.153 mV.
The native Arduino Due ADC has only 12 bits of
I’ve noticed a small bug in the IDE when dealing with
integers that are returned in the analog.read(0) command.
As long as I only do integer math with the returned value,
there are no problems. If I mix integer and floating point, I
can get an incorrect calculation unless I first turn the value
of the analog.read(0) number into a floating point by
18 June 2017