Categories
TUTORIALS

Signaling the difference between two flow meters

Introduction

Liquid flow meters measure water flow and water volume. In this tutorial, I compare the flow rates of two flow meters and produce a signal when the two do not match.

Parts

Liquid Flow Meter

The Liquid Flow Meter measures flow rate using a pinwheel. Flow rate is represented by the frequency of the pinwheel’s spinning. Each time the pinwheel completes another cycle, the sensor generates a “HIGH” signal and feeds it back to the Arduino. Then, we can calculate the water volume by a linear conversion from the number of completed cycles.

Comparing Two Meters

Here, we are comparing the flow rates from two meters. In this case, I set the threshold at 500 Hz – namely, if the flow rate (or spinning frequency) of the Flow Meter 1 is 500 Hz more than Flow Meter 2 or more, an LED lights up. When the difference shrinks down to lower than 500 Hz, the LED goes off.

Circuit

Circuit diagram made with Frizing

The Liquid Flow Meter has three wires to be connected. The red wire connects to 5V, the yellow wire connects to a digital pin (e.g., pin 2), and the black wire connects to GND. The 5V pin powers the sensor, and the digital pin collects signals from the sensor and feeds them to the Arduino.

The Code

Setting variables

First, we set up the necessary variables. It might look confusing at first, but it will clear up once you read through the rest of the tutorial. Note that each group contains two variables, one for each Flow Meter.

Timer interrupts

The Flow Meter produces a “HIGH” pulse when the pinwheel completes another cycle. Rather than checking for pulses continuously and incorporating it in the main loop, it is more convenient if we set an automatic “timer interrupts” mechanism (see https://learn.adafruit.com/multi-tasking-the-arduino-part-2/timers) that checks for pulse every millisecond.

To do this, first we need to add some code in the setup:

This means that the frequency of this “timer interrupts” mechanism is approximately 976 Hz. We can take that for 1000 Hz for our purpose.

Then, we have a separate section that includes everything that should happen when an “interrupt” happens every millisecond. This section is separate from the main loop.

The code basically does this (and the process repeats itself for both meters):

  1. We have a timer and a counter for each sensor. They are set to 0 in the beginning. We also have a variable to record what the sensor’s state the last time we checked and a variable to record the accumulate number of pulses.
  2. Every millisecond, we check if the meter returns the same deactivated state (“LOW”) as the last time we checked.
  3. If so, then the timer adds 1. This means that the deactivated period of this cycle adds one millisecond (since we check every millisecond).
  4. If not – if the sensor returns “HIGH” – then it means that this cycle is at its end. The number of pulses adds 1.
  5. The next time, the sensor gets “LOW” again. Since this cycle is complete, we can now calculate the flow rate (i.e., frequency of the pinwheel) at this point, which equals the reciprocal of the cycle time length, multiplied by 1,000 (1 second equals 1,000 milliseconds).
  6. Then, the timer is reset. But the flow rate variable remains until the next activation.
  7. Repeat from step 2.

Main loop

While we “interrupt” and check sensor statuses every millisecond, the flow rate variables are constantly and rapidly updating in the background. Meanwhile in the main loop, we access the flow rate of each Flow Meter every 2 seconds, printing them to the Serial Monitor.

We can also calculate the accumulate water volume of each meter and print them to the Serial Monitor.

When the flow rate from Meter 1 is 500 Hz greater than that from Meter 2 or more, the LED lights up. When the gap shrinks to lower than 500 Hz, the LED goes off. Then we delay 2 seconds until the next reading.

The entire code is as follows.

Leave a Reply

Your email address will not be published. Required fields are marked *