Example Code for Arduino

Last revision 2025/12/29

This tutorial demo how to read the 4~20mA current signal with this module and Arduino UNO.

The module input current is linear with the output voltage, as shown below:

Current&Voltage

Hardware Preparation

  • DFRduino UNO R3 (or similar) x 1
  • Analog Current to Voltage Converter x 1
  • Sensor or Device(output 4~20mA current) x1
  • Gravity 3pin Sensor Cable (or several DuPont cables) x1

Software Preparation

Arduino IDE (1.0.x or 1.8.x)

Wiring Diagram

wiring diagram

Other Preparation Work

Before uploading the sample code, it is recommended that you use a voltmeter to confirm the actual reference voltage of the ADC on the main control board, and then modify VREF in the sample code, to improve the measurement accuracy.

Sample Code

/***********************************************************
 DFRobot Gravity: Analog Current to Voltage Converter(For 4~20mA Application)
 SKU:SEN0262

 GNU Lesser General Public License.
 See <http://www.gnu.org/licenses/> for details.
 All above must be included in any redistribution
 ****************************************************/

#define CurrentSensorPin  A2
#define VREF 5000 // ADC's reference voltage on your Arduino,typical value:5000mV

unsigned int voltage; //unit:mV
float current;  //unit:mA

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

void loop()
{
    voltage = analogRead(CurrentSensorPin)/1024.0*VREF;
    Serial.print("voltage:");
    Serial.print(voltage);
    Serial.print("mV  ");
    current = voltage/120.0;  //Sense Resistor:120ohm
    Serial.print("current:");
    Serial.print(current);
    Serial.println("mA");
    delay(1000);
}

Result

result

Was this article helpful?

TOP