hall flow sensor program

Hi there,

I'm new on Arduino and I have no good background in programming. I'm using hall-effect sensor to measure the flow rate of water flow and display it on a 2 line 16-character LCD. On display I need to see flow rate, litres and time. Please help, I attached the Program I tried below..It was succesfully compiled but it didn't show anything.

Here is the code I tried:

**

  • Water Flow Gauge
  • Uses a hall-effect flow sensor to measure the rate of water flow and
  • output it via the serial connection once per second. The hall-effect
  • sensor connects to pin 2 and uses interrupt 0, and an LED on pin 13
  • pulses with each interrupt. Two volume counters and current flow rate
  • are also displayed on a 2-line by 16-character LCD module, and the
  • accumulated totals are stored in non-volatile memory to allow them to
  • continue incrementing after the device is reset or is power-cycled.
  • Two counter-reset buttons are provided to reset the two accumulating
  • counters. This allows one counter to be left accumulating indefinitely
  • as a "total" flow volume, while the other can be reset regularly to
  • provide a counter for specific events such as having a shower, running
  • an irrigation system, or filling a washing machine.
  • Copyright 2009 Jonathan Oxer jon@oxer.com.au
  • Copyright 2009 Hugh Blemings hugh@blemings.org
  • This program is free software: you can redistribute it and/or modify
  • it under the terms of the GNU General Public License as published by
  • the Free Software Foundation, either version 3 of the License, or
  • (at your option) any later version. Licenses - GNU Project - Free Software Foundation
  • www.practicalarduino.com/projects/water-flow-gauge
    123456789abcdef
    1239.4L 8073.4L
    */

// Specify the pins for the two counter reset buttons and indicator LED
byte resetButtonA = 11;
byte resetButtonB = 12;
byte statusLed = 13;

//byte sensorInterrupt = 0; // 0 = pin 2; 1 = pin 3
//byte sensorPin = 2;

byte sensorInterrupt = 0; // 0 = pin 2; 1 = pin 3
byte sensorPin = 2;

// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
//float calibrationFactor = 4.5;
float calibrationFactorHeat = 4.53.785411.1; //adjustment to gallons and calibration green wire heat pump
float calibrationFactorHot = (4.53.78541) 1.30; //adjustment to gallons and calibration white wire hot water
volatile byte pulseCount;

float flowRate;
unsigned int flowGallons;
unsigned long totalGallonsA;
unsigned long totalGallonsB;

unsigned long oldTime;
unsigned long ElapsedMilliseconds;

void setup()
{

// Initialize a serial connection for reporting values to the host
Serial.begin(9600);

// Set up the status LED line as an output
pinMode(statusLed, OUTPUT);
digitalWrite(statusLed, HIGH); // We have an active-low LED attached

// Set up the pair of counter reset buttons and activate internal pull-up resistors
pinMode(resetButtonA, INPUT);
digitalWrite(resetButtonA, HIGH);
pinMode(resetButtonB, INPUT);
digitalWrite(resetButtonB, HIGH);

pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);

pulseCount = 0;
flowRate = 0.0;
flowGallons = 0;
totalGallonsA = 0;
totalGallonsB = 0;
oldTime = 0;
ElapsedMilliseconds = 0;

// The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
// Configured to trigger on a FALLING state change (transition from HIGH
// state to LOW state)
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}

/**

  • Main program loop
    */
    void loop()
    {
    if(digitalRead(resetButtonA) == LOW)
    {
    totalGallonsA = 0;

}
if(digitalRead(resetButtonB) == LOW)
{
totalGallonsB = 0;

}

if( (digitalRead(resetButtonA) == LOW) || (digitalRead(resetButtonB) == LOW) )
{
digitalWrite(statusLed, LOW);
} else {
digitalWrite(statusLed, HIGH);
}

if((millis() - oldTime) > 1000) // Only process counters once per second
{
// Disable the interrupt while calculating flow rate and sending the value to
// the host
detachInterrupt(sensorInterrupt);
//lcd.setCursor(15, 0);
//lcd.print("*");

// Because this loop may not complete in exactly 1 second intervals we calculate
// the number of milliseconds that have passed since the last execution and use
// that to scale the output. We also apply the calibrationFactor to scale the output
// based on the number of pulses per second per units of measure (litres/minute in
// this case) coming from the sensor.

// flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactorHeat;
flowRate = ((((millis() - oldTime) / 1000) * pulseCount) / calibrationFactorHeat);

// Note the time this processing pass was executed. Note that because we've
// disabled interrupts the millis() function won't actually be incrementing right
// at this point, but it will still return the value it was set to just before
// interrupts went away.
// added next line
ElapsedMilliseconds = ((millis()-oldTime) + millis());//corrected millis use for cals
Serial.print(millis()-oldTime);
Serial.println(" (millis()-oldTime)");
oldTime = millis();

// Divide the flow rate in litres/minute by 60 to determine how many litres have
// passed through the sensor in this 1 second interval, then multiply by 1000 to
// convert to Gallons.
// flowGallons = (flowRate / 60) * 1000;
flowGallons = (flowRate / 60) * 1000;
//flowGallons = (flowRate / ElapsedMilliseconds) * 1000; //changed to this

// Add the Gallons passed in this second to the cumulative total
totalGallonsA += flowGallons;
totalGallonsB += flowGallons;

// During testing it can be useful to output the literal pulse count value so you
// can compare that and the calculated flow rate against the data sheets for the
// flow sensor. Uncomment the following two lines to display the count value.
Serial.print(pulseCount, DEC);
Serial.println (" pulseCount");

// Write the calculated value to the serial port. Because we want to output a
// floating point value and print() can't handle floats we have to do some trickery
// to output the whole number part, then a decimal point, then the fractional part.
unsigned int frac;

// Print the flow rate for this second in litres / minute

Serial.print(int(flowRate)); // Print the integer part of the variable
Serial.print("."); // Print the decimal point
// Determine the fractional part. The 10 multiplier gives us 1 decimal place.
frac = (flowRate - int(flowRate)) * 10;
Serial.print(frac, DEC) ; // Print the fractional part of the variable
Serial.println(" GPM ");
// Print the number of litres flowed in this second
// Serial.print(flowGallons);
// Serial.println(" Flow Gallons "); // Output separator

// Print the cumulative total of litres flowed since starting
Serial.print(totalGallonsA/1000);
Serial.println(" Total Gallons A"); // Output separator
Serial.print(totalGallonsB/1000);
Serial.println(" Total Gallons A");

// if(int(flowRate) < 10)
// {
// lcd.print(" ");
// }

// Reset the pulse counter so we can start incrementing again
pulseCount = 0;

// Enable the interrupt again now that we've finished sending output
attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
Serial.println(""); // Output separator

}

}

/**

  • Invoked by interrupt0 once per rotation of the hall-effect sensor. Interrupt
  • handlers should be kept as small as possible so they return quickly.
    */
    void pulseCounter()
    {
    // Increment the pulse counter
    pulseCount++;
    }

We are going to need more info than that.

What 'flow sensor' are you using?
How is it all wired up?
What flow rate do you need to detect?
What power supply is being used?

Otherwise it's a shot in the dark....

The flow sensor I’m using is Hall-effect flow sensor. It has 3 wires, red, black and yellow. Red is connected on the power supply +5V of the Arduino board, black is connected on the power supply GND of the board, and yellow is connected on pin2 of the board. The power supply I’m using is (DC) linear regulated it supplies 5V on the board. I want to detect the flow rate of the water flow passing through the sensor in L/m (Litres per minute) and display it on the LCD, time (in minutes) being displayed on LCD as well as the Litres being displayed

hall-effect flow sensor.jpg

Liters / gallon (US) = 3.785, gallons / liter = 0.2642, approximately. :slight_smile:
Looks like your problem is using integer math, for example, if flowRate = 20:

flowGallons = (flowRate / 60) * 1000;

20 / 60 = 0, then 0 * 1000 = 0. You need to use floating point math.

Have you opened a serial monitor to look and see what info you get? There are a lot of serial prints there.

Is the flow sensor actually working? Run the simple digital read sketch and just attach the sensor and run some water from a jug/tap/faucet. Does the sensor rotate internally? Do you get pulses out? - This will help eliminate some possible fail points.

Also, check continuity from sensor to arduino.

  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

You are enabling the weak internal pullup resistor on pin 2. It may not be strong enough to create reliable high/low transitions. Try a 4.7K external resistor between pin2 and 5v.

I would reiterate skywatch's suggestion to just use digitalRead(2) in a loop and see if you can see 0's and 1's as the internal paddlewheel turns.

Thanks :slight_smile: , I finally got it right, The sensor can now measure the flowrate and the volume of flow passing through the sonsor and display them on the LCD but there is a problem :frowning: Immediately after the flow have passed the sensor, the values of flowrate and volume in the LCD returns to back zero, Can you please edit my program and add code that will make volume values stay there forever, and also add the code to display timer in the LCD..Here is the code I used:

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);

volatile int FlowPulse; //measuring the rising edges of the signal
int Calc;
float Calc2;
int flowsensor = 2; //The pin location of the sensor

void setup() {

pinMode(flowsensor, INPUT); //initializes digital pin 2 as an input
Serial.begin(9600); //This is the setup function where the serial port is initialised,
attachInterrupt(0, rpm, RISING); //and the interrupt is attached

// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.setCursor(0, 1);
lcd.setCursor(0, 0);
lcd.print("Smart Meter");
}

void loop() {

FlowPulse = 0; //Set NbTops to 0 ready for calculations
FlowPulse = 0; //Set NbTops to 0 ready for calculations
sei(); //Enables interrupts
delay (1000); //Wait 1 second
cli(); //Disable interrupts
Calc = (FlowPulse * 60 / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate in L/hour
Serial.print (Calc, DEC); //Prints the number calculated above
Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a new linegbghgh
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 2);
lcd.print(Calc,DEC); // print the Flow Rate
lcd.print(" L/h");

Calc2 = (FlowPulse * 30 / 75); //(Pulse frequency x 60) / 7.5Q, = flow rate in L/hour
Serial.print (Calc2, DEC);
Serial.print (" Litres\r\n");
lcd.setCursor(0, 0);
lcd.print(Calc2,DEC);
lcd.print("L");

}

void rpm () //This is the function that the interupt calls
{
FlowPulse++; //This function measures the rising and falling edge of the hall effect sensors signal
}

Oxer's code works just fine. If there is no flow, it says flow=0, and the cumulative volume sticks until there is new flow.

It would help to read this previous thread delay () and interrupts - Programming Questions - Arduino Forum .

There is a simple modification of your code suggested in reply #4 which you should consider. The elimination of delay() is also shown in reply #2 which demonstrates the "blink without delay" approach to the flowmeter reading.

also add the code to display timer in the LCD

What timer?

Please post your code in between the code tags [code]your code here[/code] found at the </> icon in the tool bar. It should be in scrollable box like this

your code here