Example Code for Arduino-Output 20mA in 4–20mA Range (PWM Mode)

Last revision 2026/02/11

Control the module to output 20mA within the 4–20mA range via PWM communication on the ESP32-E main controller, and verify the result with a multimeter.

Hardware Preparation

Software Preparation

Wiring Diagram

DFR1230-pwm wiring

Connect the module to the ESP32-E as shown in the diagram. The core corresponding relationships are as follows:

  • Module PWM interface "+" → ESP32-E 3.3V
  • Module PWM interface "-" → ESP32-E GND
  • Module PWM interface "D" → ESP32-E GPIO13 (PWM output pin)

Other Preparation Work

  • DIP switch configuration: A0=0, A1=0, A2=1 (output range = 4–20mA)

Sample Code

Open Arduino IDE, copy the following code, and upload it to the ESP32-E:

#include <DFRobot_GP8XXX.h>

// Define the PWM pin as 13 (applicable to ESP32)
const int pwmPin = 13;
DFRobot_GP8630N_PWM GP8630N(pwmPin);

void setup() {
  // Initialize the sensor
  GP8630N.begin();
  
  // Set output data (PWM range for ESP32 is 0–1023)
  uint16_t data = 1023;  // Maximum output value
  GP8630N.setDACOutData(max(data*0.83, 170.5));
}

void loop() {

}

Result

  1. Set the multimeter to the "DC A" range;
  2. Connect the red probe to the module's "IOUT" terminal and the black probe to the module's "GND" terminal;
  3. Under normal conditions, the measured current should be close to 20mA. In this example, the actual measured output current is 20.24mA.

DFR1230-pwm result

Was this article helpful?

TOP