กลับไปหน้ารวมไฟล์
dosing-peristaltic-pump-pwm-ssr-hx711-scale-and-an-fsm-36c4d1-en.md

This application shows a dosing application using 2 peristaltic pumps (PWM driven via solid state relays), 1 digital weight transducer (using a HX711), a LED traffic light and an Arduino Mega2560 with an Ethernet shield.

First the vessel on the scale is filled in 15 ml steps up to 100ml and then it is emptied. Commands to start and stop are issued via Ethernet using WawiLib to read and write sketch variables. The state changes of the state machine are reflected to the output window of WawiLib.

There are 3 movies documenting the application:

Application demonstration.
The hardware explained (SSR and flywheel diode concepts)
Software with WawiLib variable access, C++ timer objects, PWM Solid State Relay and FSM

Technologies used and explained in detail:

The Finite State Machine (FSM) Logic

A linear delay() program crashes when implementing emergency stops. This engine uses an explicit Finite State Machine (FSM). The logic for a precision dosing application typically follows these states:

  1. State IDLE: Waiting for target input (e.g., 50g requested).
  2. State DOSING_FAST: The SSR blasts 100% PWM power to the 12V Peristaltic pump.
  3. State DOSING_SLOW: When weight approaches the target (e.g., 5g remaining), the Arduino drops the SSR PWM to a slow, controlled pulse.
  4. State DOSING_DONE: The load cell hits the exact target weight, shutting off power instantly.

This state-based approach ensures reliable, interruptible operation. The state diagrams below illustrate this flow.

Finite state machine diagram for dosing.
Finite state machine diagram.

Here is a code example from the implementation:

// Finite state machine code example:
   //+++++++++++++++++++++++++++++++++++++++
   case FsmState::STEP3_WAIT_CMD_FILL:
   {
     if (cmdFill)
       FsmGoTo(FsmState::STEP4_P_FILL_FAST);
   }
   break;

A more generic example of the FSM logic for precision dosing could be structured as follows:

switch (currentState) {
  case DOSING_FAST:
    analogWrite(SSR_PIN, 255); // Full power
    if (currentWeight >= (targetWeight - 5.0)) { // Approach threshold!
      currentState = DOSING_SLOW; // Shift to fine control
    }
    break;

  case DOSING_SLOW:
    analogWrite(SSR_PIN, 80); // Reduced power for drop-by-drop control
    if (currentWeight >= targetWeight) {
      analogWrite(SSR_PIN, 0); // Exact weight achieved! Hard stop!
      currentState = DOSING_DONE;
    }
    break;
}

Arduino "millis()" based software timers

Arduino "millis()" based software timers (as a C++ object in a separate file in the sketch). The use is different from what you normally see (millis() is encapsulated in the C++ object and the object is added in a separate file to the ide). All is explained step by step in the.pdf that can be downloaded from sylvestersolutions.com/documentation.

// blinking 2 timers at the same time with variable time period:
#include "WawiTimer.h"
// blinker bit helper timers(software timers):
WawiTimer tBlink250; // 250 ms on
WawiTimer tBlink500; // 500 ms
// blinking bits that toggle:
bool blink250;
bool blink500;
//------------------------------------------------------------
// create blinkbits:
void Loop()
{
  if (tBlink250.isZero())
  {
      tBlink250.setMs(250);
      blink250 = !blink250;
  }
  if
  {
      tBlink500.setMs(500);
      blink500 = !blink500;
   }
   tBlink250.loop(); // 250 ms
   tBlink500.loop(); // 500 ms
}

Forget about millis()! Add your C++ timer object that is counts down so your loop is not interrupted: the only thing you need to do is add 2 files to your sketch! (The documentation explains in detail how.)

PWM (Pulse Width Modulation) via SSR (Solid State Relay)

Pulse width modulation is a technique used to control (e.g.) the speed of an DC motor. Some of the Arduino outputs can switch on and of very fast (hardware based) at multiple kHz. If you use this function to regulate the current to a DC motor you can make it run at variable speed. The movie and documents explain in detail how to do this.

The Solid State Relay (SSR) PWM Challenge

You CANNOT send PWM signals (fast pulsing) to a standard mechanical clicky relay! It will physically buzz and melt in seconds due to contact arcing. You must use a DC-to-DC Solid State Relay (SSR) (e.g., Fotek or a heavy-duty MOSFET board). The SSR has zero moving parts, allowing the Arduino to rapidly switch high current hundreds of times per second (analogWrite()), smoothly throttling the DC peristaltic motor speed!

The diagrams below show the basic setup and connection.

Basic setup of 2 pumps with DC motor and flywheel diode.
Connection of SSR to Arduino (inverse logic)

Flywheel Diode Protection

A DC motor contains a coil, which is an inductance. You must make sure that its current flow is not interrupted when the relay opens, as this causes a large voltage spike that can damage components. A diode is the ideal component to do this. This tutorial explains the concept of a flywheel diode in detail.

Flywheel diode not active and active.

Hardware & System Architecture

In pharmaceutical labs or hydroponic chemical dosing, achieving precise liquid delivery using timing alone is unreliable. Viscosity changes and voltage drop cause massive errors. This Dosing Peristaltic Pump system relies on a closed-loop mass feedback controller. The Arduino constantly evaluates the absolute weight of the target beaker using an HX711 Load Cell amplifier, proportionally throttling the Solid State Relay (SSR) over PWM to smoothly ramp down the peristaltic pump right before hitting the exact target weight!

Hardware Chemical Infrastructure

  • Arduino Mega2560 with Ethernet Shield (or Nano/Uno for simpler setups).
  • 12V Peristaltic Pump (Silicone tubing ensures the liquid never touches metal parts, perfect for sterile bio-fluids or harsh fertilizers).
  • HX711 Amplifier & Load Cell (Provides extreme sub-gram mass resolution).
  • DC-to-DC SSR (Solid State Relay) or high-power Logic Level MOSFET (e.g., IRLZ44N).
  • Heavy 12V Power Supply (To drive the torque of the peristaltic roller assembly).
  • LED Traffic Light for visual state indication.

Monitoring and Control with WawiLib

Commands to start and stop are issued via Ethernet using WawiLib to read and write sketch variables. The state changes of the state machine are reflected to the output window of WawiLib.

Wawilib To monitor and edit variables and see sketch debug output at the same time.
  • The AppNotePeriPump.pdf detailed tutorial can be downloaded from www.sylvestersolutions.com explaining the details step by step. Go to the documentation page of the Sylvester Solutions website.

Sylvester solutions site.

If you do not know how to program an automation task properly (i.e. without delay statements). Look at this tutorial and you will learn a lot. All concepts are in detail explained in the pdf and the.ino source code is also included.

The code can be used as a framework for many other applications as it is structured like the pro's do.

Have fun and learn!

ข้อมูล Frontmatter ดั้งเดิม

apps:
  - "1x WawiLib"
author: "user1793315"
category: "Sensors & Environment"
components:
  - "1x Bench Power Supply, DC"
  - "1x OOTDTY 2 Channel SSR Solid State Relay High Lever Trigger 5A 5v12v For Uno R3 APR11_10"
  - "1x Arduino Ethernet Shield 2"
  - "1x Arduino Mega 2560"
  - "1x DC Small Dosing Pump 3V 6V 12V 24V Micro Self priming Mute Mini Peristaltic Liquid"
  - "1x     Digital Load Cell Weight Sensor HX711 AD Converter Breakout Module 5KG Portable Electronic Kitchen Scale for Arduino Scale new     Digital Load Cell Weight Sensor HX711 AD Converter Breakout Module 5KG Portable Electronic Kitchen Scale for Arduino Sca"
  - "1x Mini 5V Traffic Light LED Display Module for Arduino Red Yellow Green 5mm LED RG"
description: "Industrial analytical chemistry! Construct an ultra-precise fluid dosing engine driven by a finite state machine, orchestrating peristaltic DC tracking algorithms dynamically scaled against mass feedback from an HX711 load cell bridge."
difficulty: "Intermediate"
documentationLinks: []
downloadableFiles:
  - "https://projects.arduinocontent.cc/2c1f185c-449b-45cb-8c9a-6b0965e398ce.ino"
  - "https://projects.arduinocontent.cc/2c1f185c-449b-45cb-8c9a-6b0965e398ce.ino"
encryptedPayload: "U2FsdGVkX19vNyoikWRBY8LtfEiYn11ZobPJc7jbaKDAEyFkfklX0XnOa3MuxdhQwQkxk0DkUOCR4CFl3vAq5Q=="
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/dosing-peristaltic-pump-pwm-ssr-hx711-scale-and-an-fsm-36c4d1_cover.jpg"
lang: "en"
likes: 0
passwordHash: "cfe852765ad69bbabde9423ec500249a45ae16a247d6d5a5e954cbd5758b8767"
price: 2450
seoDescription: "Learn to build a Dosing Peristaltic Pump with PWM SSR, HX711 scale, and Finite State Machine using WawiLib and software timers."
tags:
  - "peristaltic pump"
  - "timer"
  - "fsm"
  - "solid state"
title: "Dosing Peristaltic Pump (PWM SSR), HX711 Scale and an FSM"
tools: []
videoLinks:
  - "https://www.youtube.com/embed/kgk6FQgIbCU"
  - "https://www.youtube.com/embed/r9Xk9aEaWg8"
  - "https://www.youtube.com/embed/SnCkaiOW8Sw"
views: 15206