Example Code for Arduino - Timed Pumping

Last revision 2026/10/08

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

Hardware Preparation

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"

#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"));//此处修改5000可设置串口监视器的提示信息
    if (pump.timerPump(5000, &volume)) {//此处修改5000可设置泵取的指定时间,单位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.

Was this article helpful?

TOP