The ATSAMD21G18A-AU microcontroller on the Mini Ultra Pro comes with a built-in Digital Analog Converter (DAC) that allow it to provide analog output. Usually, when such peripheral is not available on a microcontroller, it is very common to use a resistor-capacitor filter network on a Pulse Width Modulation (PWM) output to obtain the desired analog voltage.
The analog voltage output is provided by DAC0 located on pin A0. Both 8-bit and 10-bit resolution is available for the analog output voltage. At 10-bit resolution setting, you would be able to output 0 to 3.3 V voltage in 1024 steps (about 3.22 mV of resolution). We found this feature to be very useful when you need to drive a sensor with a voltage reference other than your microcontroller operating voltage.
To demonstrate the functionality of the DAC peripheral on the Mini Ultra Pro, we will be providing a sinusoidal output voltage on pin A0 using the DAC peripheral. Use the following code and upload it onto the Mini Ultra Pro.
// ***** CONSTANTS ***** // Angle increment to sweep from -pi to +pi const float angleDelta = 0.05; // Midpoint of DAC value (10-bit resolution span) const float midPoint = 1023 / 2; const float pi = 3.142; // ***** PIN DEFINITION ***** #define PIN_DAC0 A0 // ***** VARIABLES ***** // Argument of sin() function float angle = 0; // DAC value in analogWrite() function int dacValue = 0; void setup() { // Change analog output resolution to 10-bit from default 8-bit analogWriteResolution(10); } void loop() { // The sin() function returns a value between -1 & 1 // Map -1=0; 0=1023/2; & 1:1023 dacValue = (int)(midPoint + (midPoint * sin(angle))); // Increment the angle angle += angleDelta; // Wrap around the angle between -pi & +pi if (angle > pi) { angle = -pi; } // Generate a voltage between 0 and 3.3V. analogWrite(PIN_DAC0, dacValue); }
Use an oscilloscope to probe the signal on pin A0 (DAC0).
You should be able to get a similar waveform as below. Take note that the output swing between 0 and 3.3 V.
However, if you do not have an access to an oscilloscope, we would be able to plot the DAC0 output voltage on your Arduino IDE. Starting from v1.6.6 of the Arduino IDE, you can visualize your data in real time graphs using the Serial Plotter feature on the IDE. In order to use the Serial Plotter, remove the oscilloscope probe and replace them with a jumper wire shorting pin A0 and pin A1. We will use the ADC on pin A1 to read the output voltage of pin A0 (DAC0).
On top of that, we need to modify our code a little bit.
// ***** CONSTANTS ***** // Angle increment to sweep from -pi to +pi const float angleDelta = 0.05; // Midpoint of DAC value (10-bit resolution span) const float midPoint = 1023 / 2; const float pi = 3.142; // ***** PIN DEFINITION ***** #define PIN_DAC0 A0 #define PIN_ADC A1 // ***** VARIABLES ***** // Argument of sin() function float angle = 0; // DAC value in analogWrite() function int dacValue = 0; // Voltage output of DAC0 in V float outputVoltage = 0; void setup() { // Change analog input resolution to 12-bit from default 10-bit analogReadResolution(12); // Change analog output resolution to 10-bit from default 8-bit analogWriteResolution(10); // Wait until serial USB connection is ready while(!SerialUSB); // Set the baud rate SerialUSB.begin(115200); } void loop() { // The sin() function returns a value between -1 & 1 // Map -1=0; 0=1023/2; & 1:1023 dacValue = (int)(midPoint + (midPoint * sin(angle))); // Increment the angle angle += angleDelta; // Wrap around the angle between -pi & +pi if (angle > pi) { angle = -pi; } // Generate a voltage between 0 and 3.3V. analogWrite(PIN_DAC0, dacValue); // Read DAC0 output and convert to 0 to 3.3 V outputVoltage = (analogRead(PIN_ADC) * 3.3) / 4096; SerialUSB.println(outputVoltage); }
Upload the updated code to your Mini Ultra Pro and run the Serial Plotter (Tools >> Serial Plotter). You should be able to see your sinusoidal voltage output from your DAC0 of the Mini Ultra Pro!