Flow Sensor

I am trying to put a flow sensor in my project. Water Flow Hall Sensor Switch 1in 1" Flowmeter Flow Meter Counter 1-60L/min. I want to interface this sensor on a MCP23017. I am using 4 pins as outputs on the MCP23017 I have the Adafruit library for the MCP23017. Looking to see how this is done.

/*
  Liquid flow rate sensor -DIYhacking.com Arvind Sanjeev

  Measure the liquid/water flow rate using this code.
  Connect Vcc and Gnd of sensor to arduino, and the
  signal line to arduino digital pin 2.

*/

byte statusLed    = 13;

byte sensorInterrupt = 0;  // 0 = digital pin 2
byte sensorPin       = 68; //mcp4 pin

// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
float calibrationFactor = 4.5;

volatile byte pulseCount;

float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;

unsigned long oldTime;

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

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

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 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 ((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);

    // 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) / calibrationFactor;

    // 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.
    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 millilitres.
    flowMilliLitres = (flowRate / 60) * 1000;

    // Add the millilitres passed in this second to the cumulative total
    totalMilliLitres += flowMilliLitres;

    unsigned int frac;

    // Print the flow rate for this second in litres / minute
    Serial.print("Flow rate: ");
    Serial.print(int(flowRate));  // Print the integer part of the variable
    Serial.print("L/min");
    Serial.print("\t");       // Print tab space

    // Print the cumulative total of litres flowed since starting
    Serial.print("Output Liquid Quantity: ");
    Serial.print(totalMilliLitres);
    Serial.println("mL");
    Serial.print("\t");       // Print tab space
    Serial.print(totalMilliLitres / 1000);
    Serial.print("L");


    // 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);
  }
}

/*
  Insterrupt Service Routine
*/
void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}

Looking to see how this is done.

No one would do this, because, judging from the code, the flow sensor is not an I2C device. So, it would not make sense to use an I2C bus expander to interface with it.

jremington:
No one would do this, because, judging from the code, the flow sensor is not an I2C device. So, it would not make sense to use an I2C bus expander to interface with it.

I was thinking with this 16 pin digital port expander would be able to interface with the flow sensor. The output pins are interfacing with relays..

Is your aim to: poll the sensor via the 23017; use the 23017 interrupt ? It's a bit unclear, to me.

Regarding interrupts, the code comments don't precisely describe what is happening. The comments say 'disable' while the code 'detaches'. They are two different things. The preferred way of ignoring interrupts is to use the interrupts/noInterrupts pair.

dougp:
Is your aim to: poll the sensor via the 23017; use the 23017 interrupt ? It's a bit unclear, to me.

Regarding interrupts, the code comments don't precisely describe what is happening. The comments say 'disable' while the code 'detaches'. They are two different things. The preferred way of ignoring interrupts is to use the interrupts/noInterrupts pair.

Yes I want to poll the sensor. Digital read the sensors. Lots of info on buttons not a lot on reading the pin.

Thanks

sprinkfitter:
I am trying to put a flow sensor ......I want to interface this sensor on a MCP23017.

Why do you want to do that? These hall effect sensors are just a pulse generator, not an I2C device, and work fine when directly connected in the normal manner

Nick_Pyner:
Why do you want to do that? These hall effect sensors are just a pulse generator, not an I2C device, and work fine when directly connected in the normal manner

I have 2 more digital temp sensor the waterproof type to add. I am running out of pins on my esp8266 12e. I have a spi device for a couple of analog pins I am using 4 and 5 for the i2c pin 2 for the temp sensors and pin 14 12 13 15 for the spi. Thanks for the help.

Trying to make an SPI device out of something that isn't is probably not a good way to fix the problem. Using DS18B20s for the temp sensors might be more productive.

Nick_Pyner:
Trying to make an SPI device out of something that isn't is probably not a good way to fix the problem. Using DS18B20s for the temp sensors might be more productive.

I guess I could do the analog pins from another device and use the spi pins for the rest of the digital sensors. Lots of pins on that MCP23017.

4.5 pulses per second on average, should be straightforward to read through an MCP23017 using interrupts. You'd need a separate input on your ESP for the interrupt line of the MCP (it has two interrupt pins - each for one group of 8 inputs - so make sure you use the correct one).

Mind that each pulse will trigger two interrupts: the rising and the falling edge.

wvmarle:
4.5 pulses per second on average, should be straightforward to read through an MCP23017 using interrupts. You'd need a separate input on your ESP for the interrupt line of the MCP (it has two interrupt pins - each for one group of 8 inputs - so make sure you use the correct one).

Mind that each pulse will trigger two interrupts: the rising and the falling edge.

Now that will get me started Thank You. Is there any issue doing this, like dependability. Is there any drawbacks using the ic over pins on the board? It seems this chip has endless possibility Thanks again for the help

Speed and timing precision, as you have the I2C bus in the way. That's for example why there are no libraries to read the DHT sensors over a port expander.

To know WHEN the pulse arrived, use the moment the interrupt triggers. Then read which pin triggered that interrupt (this takes a while - a couple hundred microseconds or so). You can set the pins that trigger interrupts, if you have only one pin to trigger an interrupt then it's easy: that one.

wvmarle:
Mind that each pulse will trigger two interrupts: the rising and the falling edge.

An interesting chip. The datasheet, pg. 24, contains this paragraph:

1.7 Interrupt Logic
If enabled, the MCP23X17 activates the INTn interrupt
output when one of the port pins changes state or when
a pin does not match the preconfigured default. Each
pin is individually configurable as follows:
• Enable/disable interrupt via GPINTEN
• Can interrupt on either pin change or change from *
default as configured in DEFVAL

  • italics mine.

To me this reads as - if DEFVAL is set to zero an interrupt will be generated when the input goes high, that is, from zero. No interrupt is generated on the transition from high to low.

Am I reading too much into it? What say you?

Interesting, indeed. This is new to me. You'll have to try it out!

No interrupt is generated on the transition from high to low.

My reading of the docs suggest that an interrupt would be generated in this case, but I don't have one of these chips to test. I agree that the docs are not clear!

wvmarle:
You'll have to try it out!

It's on a list of a few other ICs to be ordered but, alas that hasn't been done yet.