-->
Page 1 of 1

YF-S201 Flow Sensor help getting code to return a value

PostPosted: Fri Feb 07, 2020 12:45 pm
by Dunkgrease
I connected my YF-S201 flow meter to my Arduino Uno and had it working fine.
I then replicated the code with an ESP8266 NodeMCU V3 board from LoLin.
I modified the interrupt function adding ICACHE_RAM_ATTR.
The code runs OK but I am now only getting a value of "0" even when the meter rotates.
Is there something else in the original Arduino Uno code that I need to modify to get it to work?
Code below:
Thanks

Code: Select allvolatile int  flow_frequency;  // Measures flow meter pulses
unsigned int  l_hour;          // Calculated litres/hour                     
unsigned char flowmeter = D2;  // Flow Meter Pin number
unsigned long currentTime;
unsigned long cloopTime;

void ICACHE_RAM_ATTR flow ()                  // Interrupt function
{
   flow_frequency++;
}

void setup()
{
   pinMode(flowmeter, INPUT);
   Serial.begin(9600);
   attachInterrupt(0, flow, RISING); // Setup Interrupt
                                     // see http://arduino.cc/en/Reference/attachInterrupt
   sei();                            // Enable interrupts 
   currentTime = millis();
   cloopTime = currentTime;
}

void loop ()   
{
   currentTime = millis();
   // Every second, calculate and print litres/hour
   if(currentTime >= (cloopTime + 1000))
   {     
      cloopTime = currentTime;              // Updates cloopTime
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min. (Results in +/- 3% range)
      l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flow rate in L/hour
      flow_frequency = 0;                   // Reset Counter
      Serial.print(l_hour, DEC);            // Print litres/hour
      Serial.println(" L/hour");
   }
}

Re: YF-S201 Flow Sensor help getting code to return a value

PostPosted: Sat Feb 08, 2020 4:28 am
by btidey
You have defined the input pin as D2 which in NodeMCU nomenclature is GPIO4.

You attach the interrupt for 0 (GPIO0).

So I think the reason is you are not getting interrupts is you have that assignment wrong. If you are using D2 then you need to attach to 4.

Re: YF-S201 Flow Sensor help getting code to return a value

PostPosted: Sat Feb 08, 2020 7:00 am
by Dunkgrease
btidey wrote:You have defined the input pin as D2 which in NodeMCU nomenclature is GPIO4.

You attach the interrupt for 0 (GPIO0).

So I think the reason is you are not getting interrupts is you have that assignment wrong. If you are using D2 then you need to attach to 4.


Tried your suggestion by replacing D2 with GPIO4 but get the compile error "GPIO4 is not defined in this scope"??

Re: YF-S201 Flow Sensor help getting code to return a value

PostPosted: Sat Feb 08, 2020 8:37 am
by Bonzo
This pin/GPIO business is all very confusing. Why can't they just call GPIO1 pin1?

I some tutorials the say pin 1 when in fact the mean GPIO1 and in others it is the other way around!