Skip to main content

Water Dispenser with a Flow Sensor Plugin

Loading...

AppBlocks has no built-in feature for pulse-output water flow sensors, the small hall-effect turbines such as the YF-S401. This project adds one as a Custom Plugin: a service that counts the pulses of the sensor and publishes the flow rate and the volume as two ordinary variables. Around it, a handful of blocks make a water dispenser: choose a quantity with the keypad, press RUN, and a pump runs until that quantity has gone through the sensor. The number of pulses the sensor gives per liter, which is also its calibration, is a setting that is changed on the device from the LCD menu.

In the video, the first key brings the target down from 1.0 L to 0.5 L and RUN starts the pump. The screen counts the water as it goes through the sensor, and the pump stops by itself at 0.50 L, with the water at the half-liter mark of the jug.

The project targets a TPP2(G2) running Zephyr, with the LCD and keypad enabled. The plugin's code is written for the Zephyr runtime.

Hardware​

A YF-S401 is a turbine with a magnet and a hall-effect sensor. Water spins the turbine, and the sensor gives one pulse for every fixed amount of water that passes: nominally 98 pulses per second for each liter per minute, which is 5880 pulses per liter. It has three wires: supply, ground and the pulse output.

The Hardware tab showing a TPP2(G2) board with a Tibbit 00-3, a Tibbit 00-1 and a Tibbit 03-1 side by side, two Tibbit 20 terminal blocks in front of them, and a Tibbit 10 power regulator with a Tibbit 18 power jack.
SocketTibbitRole
1#00-3, two direct I/O lines, 5V and groundPowers the sensor
3#00-1, four direct I/O linesLine 4 receives the pulses
5#03-1, two low-power relaysRelay 1 switches the pump
2 and 6#20, nine terminal blocksTerminals for the three Tibbits above
11 and 12#10 and #185V regulator and power jack

The sensor takes its supply and its ground from the Tibbit #00-3, and its pulse output goes to line 4 of the Tibbit #00-1. That line needs no resistor: the plugin turns on the pull-up of the microcontroller, so it does not float between pulses. The pump is a small 12V one, wired in series with its supply through relay 1 of the Tibbit #03-1.

Line 4 is the line of a Tibbit #00-1 that can raise an interrupt. In the Hardware tab, its Line 4 Mode is set to Interrupt and its Interrupt Edge to High, so that the line fires once per pulse. The plugin refuses to build with any other mode, or with the edge set to Both, which would count every pulse twice.

tip

A pump motor is an inductive load, and switching it can put spikes on the pulse line that get counted as water. A diode across the terminals of a DC pump, and sensor wiring kept away from the pump's, prevent this.

The Water Flow Sensor​

The sensor is handled by a Custom Plugin that comes with the project. There is nothing to install and no code to write: in the Features tab, Water Flow Sensor has its own page under Custom Services, and is set up like a built-in feature.

The settings of the Water Flow Sensor: Enabled, Pulse Input T00_1_S3, Pulses per Liter holding the pulses_per_liter variable, Flow Rate Variable set to FLOW_RATE (Flow rate, in L/min), Total Variable set to FLOW_TOTAL (Volume counted, in L) and Update Period 100 ms.
SettingWhat it isIn this project
Pulse InputThe Tibbit #00-1 whose line 4 receives the pulses.The Tibbit in socket 3.
Pulses per LiterWhat the sensor gives per liter, as a number or as a variable. It is also the calibration.The pulses_per_liter variable, which belongs to the setting described below.
Flow Rate VariableThe variable of the project that receives the flow rate.FLOW_RATE
Total VariableThe variable of the project that receives the volume.FLOW_TOTAL
Update Period (ms)How often the two variables are updated, from 100 to 60000 milliseconds.100, because the application compares the volume against a target.

What the sensor gives the application​

Everything the sensor measures arrives in two variables of the project. They are ordinary variables, created on the Variables page with the Value(number) data type and then picked in the sensor's settings:

VariableValue
FLOW_RATEThe flow rate, in liters per minute.
FLOW_TOTALThe volume counted since the device started, in liters. Writing a value to it restarts the count from that value.
The Variables list of the project with four variables of type Value(number): target_volume, is_pouring, FLOW_RATE described as Flow rate, in L/min, and FLOW_TOTAL described as Volume counted, in L.

Both move in steps of 0.01. The names are free: the sensor follows a variable that is renamed. The editor reports an error while one of the two settings is empty, and the project does not build if it holds a variable of another type.

Blocks use them like any other variable. An On Variable Changed block runs each time the value changes, and a Variable Set/Math block that writes 0 to FLOW_TOTAL starts a new count. The dispenser below is built from these two blocks.

To see how the plugin is made, open the Custom Plugins page of the Features tab, which holds its settings and its code.

The dispenser​

The application has two more variables: target_volume, the quantity to pour in liters, and is_pouring, which is 1 while the pump runs. The Readout screen of the LCD feature has a status line, the target, the quantity dispensed so far and a label above each of the four keys.

The Readout screen as the editor draws it: READY, TARGET QTY: 1.0 L, DISPENSED 0.00 L in large blue digits, and four key labels reading minus, plus, STOP in red and RUN in green.

The flows are in the AppBlocks tab, and in the project embedded at the bottom of this page. The control line of the relay is T03_1_S5_L1: 0 closes the relay and starts the pump, 1 releases it.

TriggerWhat happens
On BootReleases the relay, clears is_pouring and FLOW_TOTAL, then writes the target, 0.00 and READY to the screen.
KEY_1 pressedWhen idle and above the minimum, takes 0.1 L off the target and redraws it.
KEY_2 pressedWhen idle, adds 0.1 L to the target and redraws it.
KEY_3 pressedWhen pouring, releases the relay, clears is_pouring and shows STOPPED.
KEY_4 pressedWhen idle, sets is_pouring, sets FLOW_TOTAL to 0, shows 0.00 and POURING..., then closes the relay.
FLOW_TOTAL changedWhen pouring, writes the total to the screen with two decimals. Once it reaches target_volume, releases the relay, clears is_pouring and shows COMPLETED.
MD button pressedRuns Enter LCD Menu, which opens the menu where the pulses per liter are changed.

None of these blocks knows about pulses. They read FLOW_TOTAL like any other variable, and setting it to 0 with a Variable Set/Math block is enough to restart the count. The same two variables could just as well feed a gauge on the Web Console or an MQTT topic. FLOW_RATE is not used here, but it is updated all the same.

The pump takes a moment to stop and the hose holds some water, so expect the glass to get slightly more than the target.

The Pulses per Liter setting​

The figure of a sensor is configured once per device, so the project keeps it in the Settings feature instead of on the plugin's page:

PropertyValue
Namepulses_per_liter
DescriptionPulses Per Liter
Setting TypeValue(number)
Minimum0
Default Value5880, the nominal figure of the YF-S401
The Settings feature with one setting: Name pulses_per_liter, Description Pulses Per Liter, Setting Type Value(number), Minimum 0, Maximum 2147483647 and Default Value 5880.

A setting comes with a variable of the same name, and that variable is what the Pulses per Liter field of the plugin holds. The value is kept on the flash disk and loaded at startup, before the sensor starts counting with it.

Two more things put the setting in the user's hands:

  • In the Settings section of the LCD feature, a group named Settings contains it, with the Text Box control. This is what puts it on the LCD menu.
  • In the AppBlocks tab, an On Button Pressed block for the MD button runs Enter LCD Menu.
The Settings tab of the LCD feature: a group named Settings with one row, whose Connected Setting and Setting Display Name are Pulses Per Liter, UI Control Type Text Box and Edit Mode Read/Write.

On the device, pressing the MD button opens the menu over the Readout screen. The menu has a single group, so its setting is directly on the root page, which has three rows: Pulses Per Liter with its current value, Network status and Reboot device. K1 and K2 move the selection, K3 opens the editor and K4 closes the menu. The setting has no maximum, so the editor takes the value as text, one character at a time, from the digits and the decimal point: K1 and K2 change the selected character, K3 moves to the next one and stores the value after the last, and K4 cancels.

The root page of the LCD menu, titled Settings: Pulses Per Liter selected with the value 5880, then Network status and Reboot device. The bottom bar reads K1 up, K2 down, K3 select, K4 exit.The editor for Pulses Per Liter over the menu, showing 5880 with the first digit in brackets and the caption Character 1 of 12. The bottom bar reads K1 plus, K2 minus, K3 next, K4 cancel.

The new value is saved at once, but the sensor took its figure at startup. Pick Reboot device to restart with the new one.

The Reboot page of the LCD menu: Yes, reboot now, then No, go back selected, and the note Saved settings are applied during startup.
note

While the menu is open, the four keys belong to it and the key blocks of the dispenser do not run, STOP included. A pour that is under way still ends at its target, but open the menu when nothing is pouring.

Calibration​

5880 is the nominal figure for a YF-S401, and sensors differ from one unit to the next. To calibrate yours, pour a known volume and compare it with what the screen reports:

new Pulses per Liter = current value × liters reported ÷ liters really poured

Enter the result in Pulses Per Liter on the LCD menu, then reboot the device from the same menu. Nothing is rebuilt or uploaded, and each device keeps its own figure.

The plugin is not tied to this model. Any sensor with a pulse output works with its own figure, for example about 450 pulses per liter for a YF-S201.

Using the plugin in another project​

The Import/Export tab of the Custom Plugins page gives the plugins of this project as JSON, and imports them into another project, where they replace the plugins already there. The Water Flow Sensor then appears under Custom Services. To set it up:

  1. On the Variables page, add two variables of type Value(number), one for the flow rate and one for the volume.
  2. In the Hardware tab, set Line 4 Mode of the Tibbit #00-1 that receives the pulses to Interrupt.
  3. On the Water Flow Sensor page, enable the sensor, then pick that Tibbit and the two variables.
  4. Enter the pulses per liter of your sensor, as a number or, as in this project, as the variable of a setting.

The two variables are then ready to be used in blocks.