Example Code for Arduino-Output 10mA Current

Last revision 2025/12/25

This tutorial explains how to use Arduino to output a current of 10mA, detailing hardware and software setup, wiring diagrams, and sample code for precise implementation.

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

Write and download the following code into the UNO main board to make the current module output a current of 10mA, and save the value in the module. Changing the value in the "module.output(10)" function can change the output current, the range of the value is 0-25, corresponding to 0-25mA respectively.

Sample Code

Code initializes GP8302 module, outputs 10mA current, saves config (not lost after power-off).

#include "DFRobot_GP8302.h"

DFRobot_GP8302 module;

void setup(){
  Serial.begin(115200);

  while(!Serial){

  }
  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!");

  uint16_t dac = module.output(10); //control the converter module to output a current of 10mA and return the corresponding DAC value
  Serial.print("DAC value: 0x"); Serial.println(dac, HEX);
  module.store(); Serial.println("Save current configuration."); //the above current config will be saved and will not be lost after power off
}

void loop(){

}

Result

As shown in the figure, the multimeter shows an output current of 10.05mA. The error is 0.05mA (0.5%), which is resulted from the accuracy of the multimeter and lack of calibration.

DFR0972-result

Was this article helpful?

TOP