Example Code for Arduino-Calibration and Adjustable Analog Voltage Output
The article offers an in-depth guide on calibrating and adjusting analog voltage output using Arduino and the MCP4725 DAC module. It covers essential hardware and software preparation, provides a wiring diagram, and includes sample code for accurate voltage output. The guide helps in eliminating output errors due to inaccurate reference voltage and provides a step-by-step calibration process using a digital multimeter.
Hardware Preparation
- DFRduino UNO R3 + Gravity IO Expansion Shield * 1
- Gravity: MCP4725 12-Bit I2C DAC Module * 1
- Gravity 4P sensor wire (comes with Gravity 12-bit DAC Module) * 1
- Digital multimeter (optional) * 1
- Oscilloscope (optional) * 1
Software Preparation
- Arduino IDE: Click to Download Arduino IDE
- Search and install the 'DFRobot_MCP4725' library in the library manager of Arduino IDE.
- Or download the .zip library in via Github and install the .zip library in Arduino IDE. How to install .zip library?
Wiring Diagram
Other Preparation Work
Although the DAC's voltage output accuracy is affected by several factors that cause the actual output voltage to deviate from the user-specified voltage values, the DAC reference voltage, Vref, of the MCP4725 is the same as the supply voltage VCC, and the supply voltage is usually not accurate (the actual voltage is not exactly 5.000 V or 3.300V), which results in large output error. Here, we provide a simple calibration method to eliminate the error caused by the inaccurate reference voltage. If calibration is not carried out, users can still simply set REF_VOLTAGE to 5000 in the code below (for a 5V controller such as an Arduino) or 3300 (for a 3.3V controller such as Raspberry Pi, FireBeetle) depending on the controller used.
Users are required an additional high-precision digital multimeter to complete the calibration, the specific steps are as follows:
-
Connect the module to the Arduino according to the connection diagram above and set I2C address to 0x60 by ADDR switch on the module. If I2C address 0x61 is preferred, you need to modify the first parameter of the function DAC.init() in the code below.
-
Install the MCP4725 library
-
Open Arduino IDE and upload the following code to UNO
-
Use the multimeter to measure the output voltage of VOUT and change the value of REF_VOLTAGE accordingly. For example, VOUT = 4950mV, the "#define REF_VOLTAGE 5000" should be revised to "#define REF_VOLTAGE 4950". Calibration completed.
-
Change the value of OUTPUT_VOLTAGE (unit in mV) to your desired analog output voltage. This value should be smaller than REF_VOLTAGE or the maximum output voltage will be limited to REF_VOLTAGE. Before using the sample code below, write the calibration value to REF_VOLTAGE first to get a more accurate output voltage.
Sample Code
/*
* file OutputVoltage.ino
*
* @ https://github.com/DFRobot/DFRobot_MCP4725
*
* connect MCP4725 I2C interface with your board (please reference board compatibility)
*
* Output a constant voltage value and print through the serial port.
*
* Copyright [DFRobot](https://www.dfrobot.com), 2016
* Copyright GNU Lesser General Public License
*
* version V1.0
* date 2018-1-15
*/
#include "DFRobot_MCP4725.h"
#define REF_VOLTAGE 5000
DFRobot_MCP4725 DAC;
uint16_t OUTPUT_VOLTAGE = 5000; // Input DAC output voltage (0~REF_VOLTAGE,unit: mV)
void setup(void) {
Serial.begin(115200);
/* MCP4725A0_address is 0x60 or 0x61
* MCP4725A0_IIC_Address0 -->0x60
* MCP4725A0_IIC_Address1 -->0x61
*/
DAC.init(MCP4725A0_IIC_Address0, REF_VOLTAGE);
}
void loop(void) {
Serial.print("DFRobot_MCP4725 output: ");
Serial.print(OUTPUT_VOLTAGE);
Serial.println(" mV");
DAC.outputVoltage(OUTPUT_VOLTAGE);
delay(500);
}
Was this article helpful?
