Example Code for Arduino - Timed Pumping

Last revision 2026/10/08

The peristaltic pump operates for a set duration, dispensing a specific volume of liquid, which is then displayed on the Serial Monitor. This project uses an DFRduino UNO+IO Expansion Board and a Gravity: Peristaltic Pump V2, connected via a simple wiring setup. The example code provided demonstrates how to control the pump's operation, including setting up the necessary library and defining the pump's runtime. It's like programming a precise liquid delivery system.

The peristaltic pump pumps liquid for the specified duration. The dispensed liquid volume is printed in the Serial Monitor.

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

DFR1261 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"

#if defined(ESP32)
int servoPin = 17;
#else
int servoPin = 9;
#endif

DFRobot_PeristalticPump_V2 pump(servoPin);

uint8_t timePumpFlag = 1;
float   volume       = 0.0f;

void onTimerPumpCallback(float volume, bool finished)
{
  if (finished) {
    Serial.print(F("Final volume(ml): "));
    Serial.println(volume, 4);
  } else {
    Serial.print(F("Current volume(ml): "));
    Serial.println(volume, 4);
  }
}

void setup()
{
  Serial.begin(115200);
  pump.begin();
  pump.setTimerPumpCallback(onTimerPumpCallback, 50);

  Serial.println(F("timerPump demo (call once)."));
  Serial.println(F("Run calPump.ino first to store flowRate in EEPROM."));
}

void loop()
{
  // Update pump status; this must be called repeatedly in loop().
  pump.updatePumpStatus();

  /**
   * Note:
   * 1. Use timerPump() to run the pump for a fixed duration.
   * 2. Parameter 1: run time in milliseconds.
   * 3. Parameter 2: pointer to a float for the precalculated pumped volume (ml).
   * 4. Return true: task started successfully.
   * 5. Return false: start failed.
   *    - A task may already be running. Wait until it finishes, or call stopPump().
   *    - Calibration data may be invalid. Run calPump.ino to recalibrate flow rate.
   */
  if (timePumpFlag) {
    timePumpFlag = 0;
    Serial.println(F("Start: timerPump 5000ms"));// Modify 5000 to set the serial monitor prompt
    if (pump.timerPump(5000, &volume)) {// Modify 5000 to set the serial monitor prompt, unit: ms
      Serial.print(F("Precalculated volume(ml): "));
      Serial.println(volume, 4);
    } else {
      Serial.println(F("timerPump start failed."));
    }
  }
}

Result

The peristaltic pump runs for the specified duration. The dispensed liquid volume is printed in the Serial Monitor.
DFR1261 Example Code for Arduino - Timed Operation Result

Was this article helpful?

TOP