Connecting the water flow sensor to the Arduino is pretty easy water flow sensor:
- Connect the Vcc to the Arduino 5v
- Connect the Gnd to Gnd
- Connect the Output pin (yellow) to the digitalpin 2
Led Connection
- Anode pin to the 13
- Cathode pin to the Gnd
Programming
- volatile int fan;
- int calc;
- int sensor=7;
- int led=13;
- void wfs()
- {
- fan++;
- }
- void setup()
- {
- pinMode(led,OUTPUT);
- pinMode(sensor,INPUT);
- Serial.begin(9600);
- attachInterrupt(0,wfs,RISING);
- }
- void loop()
- {
- fan=0;
- sei();
-
- delay(1000);
- cli();
- calc=(fan*60/7.5);
- if(fan > 40)
- {
- digitalWrite(led,HIGH);
- }
- else {
- digitalWrite(led,LOW);
- }
- Serial.print(calc,DEC);
- Serial.print("L/hour\r\n");
- }
Explanation
Declaring the variable as a fan in the volatile int data type volatile means it is a qualifier and it is applied to the variable. When it declares it tells the compiler that the value of the variable may change any time without any action that had been taken to the code and declare another variable called calc as integer datatype and declare another variable sensor and initialize the value to the variable void wfs(). This is the one type of the function that the interrupt calls fan++ is one type of the increment conditions to repeat the function based upon our conditions that get false and it is used to measure the rising and falling edge of the hall effect sensors signal. Then we move to our default functions called void setup() and void loop().
Void Setup()
In Setup() function here we set our pinMode as INPUT because the value of the sensor is given to digital pin 7 as INPUT in the Arduino. Set the BaudRate(9600) for data transfer between PC and Arduino board and attach the interupt 0, wfs, RISING.
Arduino Boards and Digital Pin usable for interrupts
1 |
Uno, Nano, Mini, other 328-based |
2, 3 |
2 |
Micro, Leonardo, other 32u4-based |
0, 1, 2, 3, 7 |
3 |
Zero, MKR1000 all digital pins, except 4 Due |
all digital pins |
4 |
Mega, Mega2560, MegaADK |
2, 3, 18, 19, 20, 21 |
Interrupts
The interrupts are useful for making things happen automatically in the microcontroller program and it is used to solve the timing problem.
Syntax:
The basic syntax is used to attach the interrupt:
attachInterrupt(digitalPinToInterrupt(Pin), ISR, mode)
Void loop()
This is an important function in Arduino; it is otherwise called as the condition function based up on our conditions the functions and the components will be work in the Arduino board. Set the fan variable to "0" and it is ready for the calculation; Sei is the one type of the function it is used to enable the interrupts and make some delay time as 1000 Cli is another function used to disable the interrupts. Here we want to use some mathematical operations such as:
calc=(fan*60/7.5)
(Pulse frequency * 60)/7.5Q =Flow Rate in L hour if the value > 40 the led will be ON else it will be the OFF condition
Then we want to print the values such as cal and DEC and L/hr
Conclusion
And finally, we made a water flow sensor using the Arduino.
Read more articles on Arduino: