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
| Name | SKU | Quantity | Purchase Link | Description |
|---|---|---|---|---|
| DFRduino UNO+IO Expansion Board | DFR0216-2 | 1 | https://www.dfrobot.com/product-2136.html | Controller |
| Gravity: Peristaltic Pump V2 | DFR1261 | 1 | ||
| Gravity Sensor Cable(Comes with Peristaltic Pump V2) | 1 | Cable |
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.
Wiring Diagram

| Device A | Port/Pin | Device B | Port/Pin |
|---|---|---|---|
| DFRduino UNO+IO Expansion Board | D9 | Gravity: Peristaltic Pump V2 | D |
| DFRduino UNO+IO Expansion Board | 5V | Gravity: Peristaltic Pump V2 | + |
| DFRduino UNO+IO Expansion Board | GND | Gravity: Peristaltic Pump V2 | - |
| 12V DC Power Supply | DC2.1 | Gravity: Peristaltic Pump V2 | DC2.1 jack |
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?
