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

Last revision 2026/02/11

Communicate with the GP8600 module via the PWM interface on the ESP32-E main controller to control the module to output 20mA within the 4-20mA range, and verify the result using a multimeter.

Hardware Preparation

Software Preparation

Wiring Diagram

DFR1229-PWM wiring

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

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

Other Preparation Work

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

In PWM mode, A0 must not be set to 1; otherwise, the device may be damaged.

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_GP8600_PWM GP8600(pwmPin);

void setup() {
  // Initialize the sensor
  GP8600.begin();
  
  // Set output data (PWM range for ESP32 is 0-1023)
  double data = 1023;  // Maximum output value
  GP8600.setDACOutData(max(data, 42.5));
}

void loop() {
  // No operations required in the loop
}

Result

  1. Set the multimeter to the "DC A" range;
  2. Connect the red probe to the module's "OUT" 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.12mA.

DFR1229-PWM result

Was this article helpful?

TOP