Example Code for Arduino-DAC Accuracy and Calibration Tutorial

Last revision 2026/02/11

Adopt a linear fitting algorithm to quickly calculate calibration parameters using Excel spreadsheets, then integrate into the DAC control program to correct output deviation and improve accuracy for 0-6V range in PWM mode.

Overview of DAC Accuracy and Calibration

The accuracy of a DAC (Digital-to-Analog Converter) refers to the proximity between its actual output value and the ideal output value. Higher accuracy indicates smaller deviation.

Purpose of Calibration:

Due to differences in internal components or environmental influences during use, DAC modules may have "offset" and "gain" errors. The core of fitting calibration is to correct the original output deviation through mathematical fitting algorithms, establishing an accurate mapping relationship between "program control parameters" and "actual output values" to significantly improve accuracy.

Practical DAC Calibration Tutorial (PWM Mode · 0-6V Range)

Taking the "0-6V range (program parameter range: 0-1023)" in PWM mode as an example, this tutorial adopts a linear fitting algorithm to quickly calculate calibration parameters using Excel spreadsheets. The steps are simple and easy to understand, requiring no complex programming.

Hardware Preparation

Software Preparation

Wiring Diagram

DFR1229-PWM wiring

Connect the module to the ESP32-E as shown in the diagram. The core corresponding relationships are as follows:

  • Module PWM pin "+" → ESP32-E 3.3V
  • Module PWM pin "-" → ESP32-E GND
  • Module PWM pin "D" → ESP32-E GPIO13 (PWM output pin)
  • DIP switch configuration: A0=0, A1=0, A2=0 (output range = 0-6V)

Environmental Requirements

  • Stable ambient temperature (to avoid the impact of temperature drift on measurements).
  • No obvious fluctuations in power supply voltage.

Other Preparation Work

Data Collection Requirements:

  • Select 5 or more intermediate program parameters (do not select range endpoints 0 or 1023): Endpoint data is easily affected by the extreme characteristics of hardware, which may cause the fitting curve to deviate from the actual working range. Intermediate values can ensure fitting accuracy;

  • Uniform distribution of program parameters: Cover the entire 0-1023 range (e.g., 171, 341, 512, 682, 853 in the example, corresponding to theoretical outputs of 1-5V) to ensure the fitting curve covers the entire range.

1. Step 1: Record Raw Data

Configure the DAC according to the selected program parameters, measure the actual output voltage with a multimeter, and record it in the following table (example data):

Program Parameter (x) Theoretical Output Voltage (V) Actual Output Voltage (V) Absolute Error (V) Full-Scale Error
0 0 0.0014 0.0014 0.0233%
171 1 0.9928 -0.0072 -0.1200%
341 2 1.9923 -0.0077 -0.1283%
512 3 2.9867 -0.0133 -0.2217%
682 4 3.9882 -0.0118 -0.1967%
853 5 4.9848 -0.0152 -0.2533%
1023 6 5.9942 -0.0058 -0.0967%

Note: 0 and 1023 in the table are only for displaying full-scale errors. When performing fitting calculations, exclude endpoint data and only use the 5 intermediate groups (171, 341, 512, 682, 853).

Calculation of Calibration Parameters (Excel Tool)

Use the linear fitting functions in Excel to calculate the conversion coefficient c and conversion offset o required for calibration. The core logic is to correct the linear deviation between the original "program parameters and actual output".

2. Step 2: Input Data into Excel

Enter the 5 intermediate groups of data in Excel in the following format (example cell range):

Cell Content (Program Parameter x) Cell Content (Actual Output Voltage y)
B11 171 D11 0.9928
B12 341 D12 1.9923
B13 512 D13 2.9867
B14 682 D14 3.9882
B15 853 D15 4.9848

3. Step 3: Calculate Original Slope k

The slope k represents the linear relationship between "changes in program parameters" and "changes in actual output" in the original state. The Excel formula is as follows:

=SLOPE(D11:D15, B11:B15)
  • Meaning: D11:D15 is the range of actual output voltages, and B11:B15 is the range of corresponding program parameters. This formula calculates the linear slope between the two.

4. Step 4: Calculate Original Intercept b

The intercept b represents the actual output offset when the program parameter is 0. The Excel formula is as follows:

=INTERCEPT(D11:D15, B11:B15)
  • Meaning: Calculate the intercept b based on the linear equation y = k*x + b.

5. Step 5: Calculate Conversion Coefficient c

The coefficient c is used to correct the original slope deviation so that the output conforms to the theoretical range. The Excel formula is as follows:

=(Theoretical Maximum Voltage / Maximum Program Parameter) / k
  • Example substitution: Theoretical maximum voltage = 6V, Maximum program parameter = 1023. Therefore, the Excel formula is:
=(6/1023)/k
  • Meaning: Correct the original slope k to the ideal slope that meets the theoretical range.

6. Step 6: Calculate Conversion Offset o

The offset o is used to offset the original intercept deviation. The Excel formula is:

=-b/k
  • Meaning: Eliminate the inherent output offset when the program parameter is 0 through offset compensation.

7. Step 7: Code Integration and Activation

Substitute the c (conversion coefficient) and o (conversion offset) calculated in Excel into the DAC control program, and real-time correct the program parameters using the calibration formula to ensure the actual output is close to the theoretical value.

Calibration Formula:

y = c * x0 + o
  • Explanation: y = Calibrated program parameter (final control value written to the DAC); x0 = Original target program parameter (uncalibrated, calculated from the theoretical output voltage).

  • The complete calibration formula obtained by substituting the above-calculated conversion coefficient and offset is: y = 1.0020 * x0 + 1.1550

Sample Code

Fill in the obtained c (conversion coefficient) and o (conversion offset) values into the following code:

dfr1229-code

Then, you can obtain more accurate data using the fitting calibration algorithm. The complete code is as follows:

#include <DFRobot_GP8XXX.h>

// Fix the PWM pin to 13 for the ESP32 platform, no need for multi-platform adaptation
const int pwmPin = 13;
DFRobot_GP8600_PWM GP8600(pwmPin);

/**
 * @brief Calibration function (ESP32-specific, matching 0-6V range)
 * @param data Uncalibrated original program parameter (0-1023)
 * @param coefficient Fitting calibration coefficient c (calculated in Excel)
 * @param offset Fitting calibration offset o (calculated in Excel)
 * @return Calibrated PWM control parameter (0-1023)
 */
uint16_t calibrationFun(uint16_t data, double coefficient, double offset)
{
  double tempdata = data * coefficient + offset;
  // The PWM parameter range for the ESP32 platform is fixed at 0-1023 (corresponding to 0-6V), directly clamp the value
  if (tempdata > 1023) tempdata = 1023;
  if (tempdata < 0) tempdata = 0;

  Serial.print("Calibrated PWM parameter: ");
  Serial.println((uint16_t)tempdata);
  return (uint16_t)tempdata;
}

void setup() {
  // Initialize the serial port (for debugging and viewing calibration parameters)
  Serial.begin(115200);
  // Initialize the GP8600 module (PWM mode)
  GP8600.begin();
  
  /**
   * @brief 0-6V range output configuration (ESP32-specific)
   * @note 1. The module's DIP switches must be configured for 0-6V output (refer to the instructions on the back of the module)
   * @note 2. The PWM parameter range for the ESP32 platform is 0-1023, corresponding to an output of 0-6V
   * @note 3. To enable calibration, pass the data below into calibrationFun
   */
  uint16_t data = 1023; // Uncalibrated original target program parameter (calculated from the theoretical output voltage)
  
  // Enable calibration (replace with the actually calculated coefficient and offset)
  GP8600.setDACOutData(calibrationFun(data, caliCoeff, caliOffset));
}

void loop() {
  // No operations required in the loop
}

Result

After calibration, the actual output value will be closer to the theoretical output value, significantly improving the DAC accuracy. The calibrated program parameters can be viewed via the serial port.

Was this article helpful?

TOP