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
- 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
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.

Was this article helpful?
