Example Code for Arduino - Timed Operation
Last revision 2026/10/08
To run the peristaltic pump, use DFRduino UNO+IO Expansion Board and Gravity: Peristaltic Pump V2. Download Arduino IDE and DFRobot_PeristalticPump_V2 library. Set up the code to run the pump for 5 seconds. It's like setting a timer for a motor.
Run the peristaltic pump for 5s and stop.
Hardware Preparation
- DFRduino UNO+IO Expansion Board (SKU:DFR0216-2) ×1
- Gravity: Peristaltic Pump V2(SKU:DFR1216)×1
- Gravity Sensor Cable(Comes with Peristaltic Pump V2)
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE
- Download DFRobot_PeristalticPump_V2 library:DFRobot_PeristalticPump_V2
- Arduino IDE V1.8.19 (or below) can extract the .zip library downloaded from GitHub into the “libraries” folder located in the Arduino IDE installation directory.
- Arduino IDE V2.0.0 (or above) can directly search for the "DFRobot_PeristalticPump_V2" library in the Library Manager and install it.
Example Code
#include "DFRobot_PeristalticPump_V2.h"
/* For continuous rotation servo:
* speed = 0 : full speed counterclockwise
* speed = 90 : stop
* speed = 180 : full speed clockwise
*/
#if defined(ESP32)
int servoPin = 17;
#else
int servoPin = 9;
#endif
DFRobot_PeristalticPump_V2 pump(servoPin);
int setPumpRunFlag = 0;
void onPumpRunDone()
{
// Task finished. This demo keeps one-shot behavior, so the flag is not reset.
// setPumpRunFlag = 0;
Serial.println(F("Pump run task finished!"));
}
void setup()
{
Serial.begin(115200);
pump.begin();
// set pump run done callback function
pump.setPumpRunDoneCallback(onPumpRunDone);
Serial.println(F("setPumpRun callback demo."));
Serial.println(F("One-shot task: run clockwise for 5000ms."));
}
void loop()
{
// Update pump status; this must be called repeatedly in loop().
pump.updatePumpStatus();
if (setPumpRunFlag == 0) {
setPumpRunFlag = 1;
// run clockwise for 5000ms
/* Note:
* 1. setPumpRun() can be called repeatedly.
* If a task is still running, this call returns false.
* After the current task finishes, it returns true and starts a new task.
* 2. The pump can be forcibly stopped at any time by calling stopPump().
*/
if (pump.setPumpRun(180, 5000)) {
Serial.println(F("Run clockwise task started!"));
} else {
Serial.println(F("The task of setting the water pump to operate failed!"));
}
}
}
Result
After uploading the code, the Serial Monitor prints the information below. The peristaltic pump runs for 5 seconds and then stops.

Was this article helpful?
