# Aquarium filter flow sensor

To filter aquarium water internal or external filters are used. These filters tend to clog through some period of time. This period depends mainly on two factors: filtering media size and the amount of waste produced in your aquarium. Small internal aquarium filters are cleaned every week. But how to know when to clean large canister filter? Some aquariumists clean canister filters every three month, the others – every six month. The good clogged filter indicator is reduced filter water flow. But visually it is hard to detect at which level the flow is reduced and how much the filter is clogged.  Here can help small devices, called flow sensors or flow meters. They are mechanical and electronic,  I will talk about electronic type in this article.
Electronic flow sensor usually is made from three main parts: tube, turbine and Hall effect sensor. The water flows through a tube which can be made from plastic or metal. Inside the tube there is a turbine (rotor). The water flows through the tube and turbine spins. In the outer side of the tube there is Hall effect sensor with signal conditioning electronics. Hall sensor measures the turbine rotation speed and produces the signal  – square wave pulses (when connected to the external pulse measuring electronics). The faster the turbine spins, the higher pulses frequency is. But despite of the frequency pulses always have the same on/off ratio whis is 50%/50%. This means that through the pulse period the signal 50% of time will be at the high state and 50% at the low state. Flow sensors usually have npn transistor at the output. This means we have to connect pull-up resistor to the sensor output in order to measure the signal frequency.
How the liquid flow rate is related to the pulse frequency? Manufacturers provide the formula how to calculate flow rate. This formula comes in two types: F=k*Q  or  F=k*Q-m, where F(Hz) is pulse frequency and Q (l/min) is flow rate. Coefficients k and m are constants fo the particular sensor type. There is the rule: the bigger sensor’s flow rate allowed, the smaller is coefficient k. So in order to calculate flow rate, we must measure pulse frequency. Coefficient m is only used for several sensors and most widely available sensors in AliExpress uses first formula.
Sensors suitable for the canister filter flow rate measurement usually have threaded water flow input/output. Threads are measured in inches and are 1/2″ rated up to 30l/min (1800l/h) or 3/4″, 1″ for the water volumes up to 60l/min (3600l/h). Because most aquarium filters have hoses without any threads, hose adapters must be used to connect hose to the sensor.
There are several cheap flow meter sensor models available in Ebay or AliExpress. I have made some research in these pages and bought two popular flow meter sensors: YF-S201 and YF-B1. The first costs about 4USD and  the second one is about 6USD (including shipping).

 Model: YF-S201 YF-B1  Max. flow rate: 30l/min 25l/min Flow rate calculation formula: F=7.5*Q(Q=L/MIN) F=11*Q(Q=L/MIN) Power supply voltage: 5-24V 5-15V Thread size: 1/2″ 1/2″

Keep in mind that there are similar products in the market with the same model number. But this does not mean that they have the same characteristics. For example the flow rate coefficient may be different for the same sensor models. So it is better to check sensor characteristics in the seller’s page. And it is even better to check real flow rate practically.

Both sensors in the table above can be powered with 5V power supply. If we are using 5V microcontroller than we can connect sensor’s output directly to the microcontroller’s input pin only using pull-up resistor: If we are using 3.3V microcontroller we can connect sensor’s output to the microcontroller’s input pin directly using 3.3V zener diode: I have checked flow sensor’s output connected to the microcontroller. Sensor keeps duty cycle at 50% even output frequency (flow rate changes): How to get flow rate when frequency is measured:

Q=F/k, where Q is flow rate l/min, F is frequency is in Hertz and k is flow rate coefficient.

How to get pulses amount per one liter:

P=k*60, where P is pulses amount per liter and k is flow rate coefficient.

For example we are using YF-S201 sensor at it’s max flow rate. So F=k*Q. We get:

F=7.5*30l/min=225Hz or 225 pulses per second.

Pulses amount per liter: P=7.5*60=450.

In practice we can use microcontroller to count pulses. In order not to miss pulses we can use interrupts. Interrupt must be generated when microcontroller’s pin input state changes it’s state from high level to low or vice versa. But there the problem may arise: input frequency is 225Hz and can be even bigger for YF-B1 model. From the calculations above interrupt will occur 225 times per second. Microcontroller can be loaded heavily if it also executes many other tasks. So i prefer to use such technique: count number of pulses through a selected period of time. For this two timers are needed:

• one timer is used to count pulses from sensor, let’s call it Tp;
• the second one will count time period, let’s call this timer Tt.

With this approach interrupts are not needed. Microcontroller can calculate flow rate at any free time, because the data required for this (number of pulses and time between the first and the last pulses) is recorded automatically to the timers Tp and Tt registers. For example we can choose 5 seconds time interval. 5 seconds is a decent time to update flow rate to the user quite frequently and to measure flow rate accurately. We only need to check in our code if 5 seconds have passed. For this we need to check timer’s Tt registers. If we have got 300 pulses during 5 seconds, then we need to calculate pulses amount througn a 1 second – this will be frequency F:

F=300pulses/5sec =60Hz.

Knowing frequncy F anf coefficient k now we can calculate actual flow rate:

Q=F/k; Q=60Hz/7.5=8l/min; 8l/min*60min=480l/h

There is one little problem: timer Tt will not count time directly, it counts pulses like all timers. So we need to know exact time between pulses. For example we now that our timer increments each 16usec. Then one second will contain amount of pulses:

1/16*10-6=62500pulses

Because we need to wait for 5 seconds, pulses amount will be:

62500*5=312500pulses

So we neeed to check continuously if Timer Tt has a value bigger than 312500 in it’s registers. Because such value can not be handled with 16 bit timer, we can use 32 bit timer. And so the final formula for flow rate calculation will be:

Q=Tp/Tt*Ttp*k

where Q is flow rate l/min; Tp – number of pulses from sensor- (timer’s Tp register value); Tt – number of time pulses (timer’s Tt register value); Ttp – time between timer’s Tt increments in seconds; k – flow rate coefficient.

From the given values above we get:

Q=300/312500*16*10-6*7.5=8l/h or

8l/min*60min=480l/h.

When we check timer’s register for a value bigger then  312500 than we will not get exact 312500 value – it will be bigger. So we need to write in formula exact timer’s Tt register value.

That’s all for today, if you have a questions for calculations then feel free to ask me.