Flow Meter at MCP23017

Hey there,

the Code below is working at my nodemcu without using the MCP23017:


#define SENSOR 8

long currentMillis = 0;
long previousMillis = 0;
int interval = 1000;
//boolean ledState = LOW;
float calibrationFactor = 4.5;
volatile byte pulseCount;
byte pulse1Sec = 0;
float flowRate;
float flowRate_m3_h;

unsigned int flowMilliLitres;
unsigned long totalMilliLitres;

void IRAM_ATTR pulseCounter()
{
  pulseCount++;
}

void setup()
{
  Serial.begin(9600);

//  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(SENSOR, INPUT_PULLUP);

  pulseCount = 0;
  flowRate = 0.0;
  flowMilliLitres = 0;
  totalMilliLitres = 0;
  previousMillis = 0;

  attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING);
}

void loop()
{
  currentMillis = millis();
  if (currentMillis - previousMillis > interval) {
    
    pulse1Sec = pulseCount;
    pulseCount = 0;

    // 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() - previousMillis)) * pulse1Sec) * calibrationFactor;
    flowRate_m3_h = flowRate / 16,666667;
    previousMillis = 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;
    
    // Print the flow rate for this second in litres / minute
    Serial.print("Flow rate: ");
    Serial.print(flowRate_m3_h);  // Print the integer part of the variable
    Serial.print("m3/h");
    Serial.print("\t");       // Print tab space

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

But now i need some more Pins so i decided to use a MCP23017.
So i changed the Code to:

#include <Adafruit_MCP23X17.h>
 
Adafruit_MCP23X17 mcp;

#define SENSOR 8

long currentMillis = 0;
long previousMillis = 0;
int interval = 1000;
//boolean ledState = LOW;
float calibrationFactor = 4.5;
volatile byte pulseCount;
byte pulse1Sec = 0;
float flowRate;
float flowRate_m3_h;

unsigned int flowMilliLitres;
unsigned long totalMilliLitres;

void IRAM_ATTR pulseCounter()
{
  pulseCount++;
}

void setup()
{
  Serial.begin(9600);

//  pinMode(LED_BUILTIN, OUTPUT);
  mcp.pinMode(SENSOR, INPUT_PULLUP);

  pulseCount = 0;
  flowRate = 0.0;
  flowMilliLitres = 0;
  totalMilliLitres = 0;
  previousMillis = 0;

  attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING);
}

void loop()
{
  currentMillis = millis();
  if (currentMillis - previousMillis > interval) {
    
    pulse1Sec = pulseCount;
    pulseCount = 0;

    // 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() - previousMillis)) * pulse1Sec) * calibrationFactor;
    flowRate_m3_h = flowRate / 16,666667;
    previousMillis = 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;
    
    // Print the flow rate for this second in litres / minute
    Serial.print("Flow rate: ");
    Serial.print(flowRate_m3_h);  // Print the integer part of the variable
    Serial.print("m3/h");
    Serial.print("\t");       // Print tab space

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

I can compile and Run it nut now the Results of the Flow Meter are not shown. It stays at "0".

I added a Relay to another Pin at the MCP23017 to see if it works and it did.
Maybe somewone can tell me whats to modify at the new code to get Results with the MCP23017.

Thanks a lot!

Never used the MCP23xxx. However I would expect the Adafruit_MCP23X17 library has some example sketches to get you going.

attachInterrupt() will only work with the ESP board's own pins, not with pins of an external I/O expander.

What you can do, if needed, is connect the INT pin of the MCP23017 to one of the ESP board's pins and use attachInterrupt() with that pin. Then, any change in the signals connected to the MCP's input pins will cause an interrupt on the ESP. The interrupt function can set a bool variable. The code in loop() can check that variable, and if it has been set, read the MCP's pins and figure out which pin changed.

But it would be easier to keep the flow sensor attached to an ESP pin and move other pin functions to the MCP.

That sounds Good can you please Tell me how i have to write the Code therefore?
Which pin do i have to Connect with which one.?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.