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
- DFR1229 GP8600: I2C&PWM to 0-5V&0-10V&4-20mA ×1
- DFR0654 FireBeetle 2 ESP32-E ×1
- Multimeter ×1
- Several jumper wires
Software Preparation
- Download and install Arduino IDE: [Download Link]
- Download dependent libraries:
- DFRobot_GP8XXX Library: [Download Link]
- DFRobot_RTU Library: [Download Link]
- Library Installation Guide: [View Installation Tutorial]
Wiring Diagram

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
- Set the multimeter to the "DC A" range;
- Connect the red probe to the module's "OUT" terminal and the black probe to the module's "GND" terminal;
- Under normal conditions, the measured current should be close to 20mA. In this example, the actual measured output current is 20.12mA.

Was this article helpful?
