Example Code for Arduino-DAC Accuracy and Calibration Method Description
Last revision 2026/02/11
Adopt a linear fitting algorithm to quickly calculate calibration parameters using Excel, and correct the DAC output deviation to enhance accuracy. This tutorial uses the "0–12V range (program parameter range: 0–1023)" in PWM mode as an example.
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 a smaller deviation.
Purpose of Calibration:
Due to discrepancies in internal components or the impact of operating environments, DAC modules may exhibit "offset" and "gain" errors. The core of fitting calibration is to rectify the original output deviation through mathematical fitting algorithms, establishing a precise mapping relationship between "program control parameters" and "actual output values" to substantially enhance accuracy.
Practical DAC Calibration Tutorial (PWM Mode · 0–12V Range)
Taking the "0–12V 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. The steps are concise and easy to understand, requiring no complex programming.
Hardware Preparation
- DFR1230 GP8630N: I2C&PWM to 0–5V&0–10V&±10V&4–20mA × 1
- DFR0654 FireBeetle 2 ESP32-E × 1
- Multimeter
Software Preparation
- Excel spreadsheet (for calculating calibration parameters)
- DAC control program (Arduino IDE)
- Download dependent libraries:
- DFRobot_GP8XXX Library: Download Link
- DFRobot_RTU Library: Download Link
- Library Installation Guide: View Installation Tutorial
Wiring Diagram

Connect the module to the ESP32-E as shown in the diagram. The core corresponding relationships are as follows:
- Module PWM interface "+" → ESP32-E 3.3V
- Module PWM interface "-" → ESP32-E GND
- Module PWM interface "D" → ESP32-E GPIO13 (PWM output pin)
- Configure DIP switches for 0–12V output: A2=0, A1=1, A0=1
Environmental Requirements
- Stable ambient temperature (to avoid measurement errors caused by temperature drift)
- No significant fluctuations in power supply voltage
Data Collection and Preparation
Collection Requirements
-
Select 5 or more intermediate program parameters (do not select the range endpoints 0 or 1023): Endpoint data is susceptible to the hardware's extreme characteristics, which may cause the fitting curve to deviate from the actual operating range. Intermediate values can ensure fitting accuracy;
-
Uniform distribution of program parameters: Cover the full 0–1023 range to ensure the fitting curve spans the entire measurement 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 the data in the following table (sample data):
| Program Parameter (x) | Theoretical Output Voltage (V) | Actual Output Voltage (V) | Absolute Error (V) | Relative Error |
|---|---|---|---|---|
| 0 | 0 | 0.0052 | 0.0052 | 0.0433% |
| 85 | 1 | 1.002 | 0.0020 | 0.0167% |
| 171 | 2 | 1.9961 | -0.0039 | -0.0325% |
| 256 | 3 | 2.9926 | -0.0074 | -0.0617% |
| 341 | 4 | 4.0012 | 0.0012 | 0.0100% |
| 426 | 5 | 4.9951 | -0.0049 | -0.0408% |
| 512 | 6 | 5.9924 | -0.0076 | -0.0633% |
| 597 | 7 | 6.9846 | -0.0154 | -0.1283% |
| 682 | 8 | 7.9912 | -0.0088 | -0.0733% |
| 767 | 9 | 8.9891 | -0.0109 | -0.0908% |
| 853 | 10 | 9.9868 | -0.0132 | -0.1100% |
| 938 | 11 | 10.9826 | -0.0174 | -0.1450% |
| 1023 | 12 | 12.0060 | 0.0060 | 0.0500% |
The values 0 and 1023 in the table are only used to demonstrate full-scale errors. For fitting calculations, exclude the endpoint data (0 and 1023) and use only the 11 intermediate data groups.
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 11 intermediate data groups into Excel in the following format (sample cell range):
| Cell | Content (Program Parameter x) | Cell | Content (Actual Output Voltage y) |
|---|---|---|---|
| B11 | 85 | D11 | 1.002 |
| B12 | 171 | D12 | 1.9961 |
| B13 | 256 | D13 | 2.9926 |
| B14 | 341 | D14 | 4.0012 |
| B15 | 426 | D15 | 4.9951 |
| B16 | 512 | D16 | 5.9924 |
| B17 | 597 | D17 | 6.9846 |
| B18 | 682 | D18 | 7.9912 |
| B19 | 767 | D19 | 8.9891 |
| B20 | 853 | D20 | 9.9868 |
| B21 | 938 | D21 | 10.9826 |
3. Step 3: Calculate Original Slope k
The slope k represents the linear relationship between "changes in program parameters" and "changes in actual output" under the original state. The Excel formula is as follows:
=SLOPE(D11:D21, B11:B21)
- Meaning: D11:D21 is the range of actual output voltages, and B11:B21 is the range of corresponding program parameters. This formula calculates the linear slope between the two sets of data.
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:D21, B11:B21)
- 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, ensuring 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 = 12V, Maximum Program Parameter = 1023. Thus, the Excel formula is:
=(12/1023)/k
- Meaning: Adjust the original slope k to the ideal slope that matches the theoretical measurement range.
6. Step 6: Calculate Conversion Offset o
The offset o is used to counteract 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 (the 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 calculated conversion coefficient and offset is: y = 1.0018 * x0 - 0.0924
Sample Code
Fill the calculated c (conversion coefficient) and o (conversion offset) values into the code below:

You can then 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 multi-platform adaptation required
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() {
}
Result
After applying calibration, the actual output voltage will be close to the theoretical value. The calibrated accuracy can be verified using the DAC Module Accuracy Test Record - Calibrated Version.
Was this article helpful?
