Example Code for Arduino-Advanced Control

Last revision 2026/01/05

This article offers a comprehensive guide to controlling a peristaltic pump using Arduino, including hardware and software setup, wiring, and sample code for both calibration and normal operation modes. It explains how to configure the Arduino IDE, use the GravityPump and Button libraries, and interpret serial port data for precise liquid dosing. Additionally, the article covers the creation of a button interface for starting, stopping, and continuing pump operation, with all data stored in Arduino's E2PROM.

Hardware Preparation

Software Preparation

Wiring Diagram

Gravity: Digital Peristaltic Pump Wiring Diagram

Other Preparation Work

Since the motor power consumption (about 5W) is a little large, it’s better to add an external power supply on your microcontroller, USB port can only provide 2.5W power supply.

Sample Code

    #include "GravityPump.h"
    #include "Button.h"//https://github.com/DFRobot/Button

    GravityPump pump;
    Button button;
    bool run = true;
    int debug = 1;

    void setup()
    {
        pump.setPin(9);
        button.init(2);
        Serial.begin(115200);
        pump.getFlowRateAndSpeed();
    }

    void loop()
    {
        pump.update();
        button.update();
        if (debug)
        //in debug mode the pump will do calibation.
        //if set the debug mode off then the function works.
        {
            pump.calFlowRate();
        }
        else
        {
            if(run)
            {
                //switch the function by using Comments.
                run = false;
                // Serial.println(pump.flowPump(6.6));
                //just put the number in ml then the pump will dosing the numbers of liquid to you.
                //and you can find the numbers from serial port.
                Serial.println(pump.timerPump(120000));
                //just put the number in milisecend then the pump will dosing the time of numbers for you.
                //and you can find the numbers from serial port.
            }

        }
        if(button.click())
        {
            //Serial.println("click");
            //when you click the button the pump will stop immediately
            pump.stop();
        }
        if(button.press())
        {
            Serial.println(pump.flowPump(100));
            //when you press the button the pump will continue working.
            //Serial.println("press");
        }
    }

Result

The sample code has 2 mode, one is the debug mode, please set the bebug into 1, in debug mode we can calibarite pump. Please open the serial port and input STARCAL, When hit the enter key, the Pump will start to runing some time, then please using the measuring cylinder to get the measurement. then you can input the SETCAL:XX(XX is what you have read in)at the serial port and the cal mode is over. The date will be store in arduino's E2PROM. In normal mode please set debug into 0. After code be upload,the pump will runing press the button, pump will stop,and press again the pump will running again.

Was this article helpful?

TOP