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

Software Preparation

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:

DFR0972-Multimeter

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

DFR0972-- Experimental Power supply

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

DFR0972-Wiring diagram

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?

TOP