Though i commented all of the code and every statement clearly reflects its meaning but i found out that some of you still need more explanations. Firstly i named the pin 3 and 7 of arduino as ‘Pulses’ and ‘led’. Then two volatile variables ‘pulsecount‘ and ‘i‘ are used to count pulses and check led status.
You might be wondering how led checks status? Well in the ISR(interrupt service routine) of pin 3 i am toggling the state of led on each pulse. So on one pulse led switches on and on the next one it is switched off. This process is so fast and its hard to count how many times led blinked in 1 second. But you can see the fading-luminous effect with naked eye.
In the setup function i declared the arduino pin 3 as input and activated the internal pull up resistor attached with pin 3. Pull up resistor value is 10 k ohm checked from arduino data sheet. After activating pull up resistor and declaring pin as input i attached interrupt with it. Now pin 3 can count external events. I initialized the external event count on falling edge. Since the pull up resistor is activated so its better to count event on falling edge(HIGH to LOW transition). An ISR(interrupt service routine) is also attached with the pin and i named it as ‘CountPulses‘. Now on every external high to low transition on pin 3 control will jump to isr ‘CountPulses‘. In isr i am incrementing the ‘pulsecount‘ variable. Not only i am incrementing the ‘pulsecount‘ i am also toggling the led in isr. At the end i declared the led pin as output in setup function and i also opened the arduino serial channel at 9600 baud rate.
In the loop function both the volatile variables are initialized to 0. Then arduino interrupts are enabled. Interrupts are enabled for 1 second and then disabled. During this one second number of pulses are counted and are stored in variable ‘pulsecount‘. After disabling interrupts its time to calculate the flow rate.
To calculate the flow rate i used the same formulas highlighted above. I calculated the flow rate in milli liters per minute. You can transform the milli liter per minute to liters per minute by dividing milli liter per minute with 1000. Flow rate is then printed on the arduino serial monitor. You need to open the arduino serial monitor at 9600 bps in order see the flow rate.
Loop function is running infinitely and as long as the power is supplied and sufficient. So the flow rate is calculated continuously. The above code is open source you can modify and use it according to your needs.
You can attach a character 16×2 lcd to the system on which the current flow rate is displayed. With some more mathematical calculations you can estimate the amount of water passed through sensor and poured in the water tank. You can close the valve autonomously when a desired amount of water is poured in the tank.
Filed Under: Arduino, Microcontroller Projects