Example Code for Arduino - Calibrate the Peristaltic Pump

Last revision 2026/10/08

Calibrate the peristaltic pump by running Example Code 1, ensuring the tube is fully filled with liquid. Calibration data is stored in EEPROM for precise liquid delivery. Use DFRduino UNO+IO Expansion Board and Gravity: Peristaltic Pump V2. Download Arduino IDE and DFRobot_PeristalticPump_V2 library. Follow the example code to set up and calibrate the pump. It's like tuning a precision instrument for accurate performance.

The peristaltic pump supports self-calibration, and calibration data will be stored in the EEPROM of the main controller for subsequent precise quantitative liquid delivery. Before calibration, ensure the tube is fully filled with liquid. You may run Example Code 1 for a period of time beforehand.

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

/*!
 * @file calPump.ino
 * @brief Calibrate peristaltic pump flowRate and store it in EEPROM
 * @copyright Copyright (c) 2026 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license The MIT License (MIT)
 * @author JiaLi(zhixin.liu@dfrobot.com)
 * @version V1.0
 * @date 2026-02-10
 * @url https://github.com/DFRobot/DFRobot_PeristalticPump_V2
 */

#include "DFRobot_PeristalticPump_V2.h"

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

DFRobot_PeristalticPump_V2 pump(servoPin);

void onCalPumpEvent(eCalPumpEvent_t event, float value)
{
  switch (event) {
    case eCalEventWaitSetCal:
      Serial.println(F("Type \"SETCAL\" to start calibration."));
      break;
    case eCalEventInvalidSetCal:
      Serial.println(F("Invalid command. Type \"SETCAL\" to start calibration."));
      break;
    case eCalEventStart:
      Serial.println(F("Calibration started..."));
      break;
    case eCalEventCountdown:
      Serial.print(F("Calibration count down: "));
      Serial.print((int)value);
      Serial.println(F("s"));
      break;
    case eCalEventStop:
      Serial.println(F("Calibration stop."));
      break;
    case eCalEventWaitVolume:
      Serial.println(F("Input pumped volume (ml):"));
      break;
    case eCalEventInvalidVolume:
      Serial.println(F("Invalid value. Please input a number > 0."));
      break;
    case eCalEventWriteFailed:
      Serial.println(F("Write flowRate to EEPROM failed."));
      break;
    case eCalEventDone:
      Serial.println(String(F("flowRate(ml/s): ")) + String(value, 4));
      break;
    default:
      break;
  }
}

void setup()
{
  Serial.begin(115200);
  pump.begin();
  pump.setCalPumpEventCallback(onCalPumpEvent);

  Serial.println(F("DFROBOT Peristaltic Pump - Calibration"));
  Serial.println(F("Type \"SETCAL\" and send newline to start."));
}

void loop()
{
  /**
   * note:
   * 1. Prime the tubing first: run setPumpRun.ino until the tubing is fully filled with water.
   * 2. Skipping priming can lead to inaccurate calibration results.
  */

  if (pump.calPump()) {
    Serial.println(F("Calibration success, pump flow rate is stored in EEPROM."));
  } else {
    Serial.println(F("Calibration failed."));
  }

  while(1);
}

Was this article helpful?

TOP