Example Code for Arduino-Calibrate 4-20mA Current
Last revision 2025/12/25
The article offers a comprehensive guide on calibrating 4-20mA current using Arduino and GP8302 module, detailing hardware setup, software installation, wiring instructions, and step-by-step calibration process with sample code for precise current output.
Hardware Preparation
- DFRduino UNO R3 (or similar) x 1
- Gravity: I2C 4-20mA DAC Module(SKU: DFR0972) x 1
- Multimeter x 1
- Resistance of 220Ω~650Ω (greater than 1/8 watt) x 1
- Experimental Power supply
- M-M/F-M/F-F Jumper wires
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE.
- Download and install the DFRobot_GP8302.
- For Arduino IDE V1.8.19 (or earlier), install the library manually: How to Add a Library?
- For Arduino IDE V2.0.0 (or later), directly search for the "DFRobot_GP8302" in the Library Manager and install it.
Wiring Diagram
Step 1: Insert the probe of the multimeter into the jack for current, and change it to DC current, as shown in the figure:

Step 2: Adjust the voltage of the experimental power supply to 24V, as shown in the figure:

Step 3: Connect the UNO main board, the experimental power supply, the current module, the load resistor and the multimeter according to the figure:

The test proved that the result is incorrect when using load meter as load resistor, because the load meter is not a pure resistance load, which will lead to inaccurate output voltage.
Notice
For calibration, first input the exact DAC value for 4mA and for 20mA respectively, then output the current of 4mA and check the actual output value on the ammeter, finally adjust the DAC value until the actual output is exactly 4mA; similarly, output the current of 20mA and check the actual output value on the ammeter, finally adjust the DAC value until the actual output is exactly 20mA. Then the values in 4-20mA are calibrated.
Sample Code
Code initializes GP8302 module, calibrates 4-20mA parameters, controls module to output 4mA current.
#include "DFRobot_GP8302.h"
DFRobot_GP8302 module;
void setup(){
Serial.begin(115200);
while(!Serial){
//Wait for USB serial port to connect. Needed for native USB port only
}
Serial.print("I2C to 0-25 mA analog current moudle initialization ... ");
uint8_t status = module.begin(); // use the pin used by the I2C Wire object of the MCU hardware by default
if(status != 0){
Serial.print("failed. Error code: ");
Serial.println(status);
Serial.println("Error Code: ");
Serial.println("\t1: _scl or _sda pin is invaild.");
Serial.println("\t2: Device not found, please check if the device is connected.");
while(1) yield();
}
Serial.println("done!");
module.calibration4_20mA(/*dac_4 =*/652, /*dac_20 =*/3265);
uint16_t dac = module.output(/*current_mA =*/4);
}
void loop(){
}
Result
Calibrate 4mA:
- Set the parameter in the statement to 4 and output 4mA current:
uint16_t dac = module.output(/*current_mA =**/4);
- Download the code and check the actual output value of the ammeter.
- If the actual output current is 4.05mA, then lower the DAC value 652 corresponding to 4mA in the code statement, for example, change it to 645:
module.calibration4_20mA(/*dac_4 =*/652, /*dac_20 =*/3265);
- Redownload the code and check the actual current value. Repeatedly modify the DAC value until the measured current is exactly 4mA.
- The calibration of 4mA is complete.
Calibrate 20mA:
- Set the parameter in the statement to 20 and output 20mA current:
uint16_t dac = module.output(/*current_mA =*/20);
- Download the code and check the actual output value of the ammeter.
- If the actual output current is 20.05mA, then lower the DAC value 3265 corresponding to 20mA in the statement, for example, change it to 3260:
module.calibration4_20mA(/*dac_4 =*/652, /*dac_20 =*/3265);
- Redownload the code and check the actual current value. Repeatedly modify the DAC value until the measured current is exactly 20mA.
- The calibration of 20mA is complete.
So the calibration of the current within 4-20mA is complete. The calibrated current value within 4-20mA can be output using this statement:
corresponding to 20mA in the statement, for example, change it to 3260:
uint16_t dac = module.output(/*current_mA =*/10);
Was this article helpful?
