Issues with my Esp8266 with YF S201

Hello, i am beginner at esp8266 and it's my first time use ESP8266. I used ESP32 before. When i run my code in my ESP32 it works, but in ESP8266 got this message. I got error message from serial monitor: ~ld
����n�{��g|�d� lc�e �|s�l�g��n�l��s�d�l� �
ets Jan 8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3424, room 16
tail 0
chksum 0x2e
load 0x3fff20b8, len 40, room 8
tail 0
chksum 0x2b
csum 0x2b
v000423c0
~ld

Here my code:


int sensorPin = 4;
volatile long pulse;
float volume;

void increase() {
  pulse++;
}

void setup() {
  pinMode(sensorPin, INPUT);
  Serial.begin(11520);
  attachInterrupt(digitalPinToInterrupt(sensorPin), increase, RISING);
}

void loop() {
  volume = 2.663 * pulse;
  Serial.print(volume);
  Serial.println(" mL");
}

I Use D2. Please Help me :). Thank you

Did you do any interrupt-stuff with your ESP32?

It is the same with ESP32 and ESP8266: you have to add the

IRAM_ATTR 

attribute to your interrupt-service-routine

int sensorPin = 4;
volatile long pulse;
float volume;

void IRAM_ATTR increase() { // <<<<<<<<<<<<<<<< IRAM_ATTR  added
  pulse++;
}

void setup() {
  pinMode(sensorPin, INPUT);
  Serial.begin(11520);
  attachInterrupt(digitalPinToInterrupt(sensorPin), increase, RISING);
}

void loop() {
  volume = 2.663 * pulse;
  Serial.print(volume);
  Serial.println(" mL");
}
2 Likes

have you connected to D4 (GPIO2) or D2 (GPIO4)?
have a look at esp8266-pinout-reference-gpios

i use D2 GPIO4

in addition to @StefanL38 suggestion to use IRAM_ATTR

should the above be

Serial.begin(115200)

thank you for correcting my typo! and the message error solve! But there is no output in my code. Am i wrong with my intitalitation of pin?

if I run your code of post with the corrections and setting input mode to INPUT_PULLUP, e.g.

int sensorPin = 4;
volatile long pulse;
float volume;

void IRAM_ATTR increase() {
  pulse++;
}

void setup() {
  pinMode(sensorPin, INPUT_PULLUP);
  Serial.begin(115200);
  attachInterrupt(digitalPinToInterrupt(sensorPin), increase, RISING);
}

void loop() {
  volume = 2.663 * pulse;
  Serial.print(volume);
  Serial.println(" mL");
  delay(1000);
}

if I touch D2 to GND the serial monitor output is

0.00 mL
0.00 mL
2.66 mL
2.66 mL
2.66 mL
2.66 mL
2.66 mL
2.66 mL
2.66 mL
2.66 mL
367.49 mL
367.49 mL
367.49 mL
378.15 mL
716.35 mL
716.35 mL

does your D2 (GPIO4) input have pullup or pulldown?

I dont use pull up or pull up external, should i add with resistor 4.7?

i used Esp8266 Lolin, i run my code in my esp32 it works. in esp8266 my value of volume change so rarely.

no idea why ESP32 and ESP8266 should be different
for GPIO interrupt you require a good rising or falling edge - try INPUT_PULLUP

the yf-s201-water-flow-measurement-sensor documentation states "Output type: 5V TTL"
the ESP32 and ESP8266 use 3.3V logic and can be damaged by 5V logic input
use a potential divider or level converter on the input signal

you will need to measure the pulse frequency to determine the flow rate

as a general hint:

The microcontroller-world is not asuperstandardised like USB-devices

You have to check and take care about more details than just
"does the plug fit into the socket?"

such a voltage-divider looks like this

Anyway. If it does not yet work as expected you should start with very basic tests.

Do you have any kind of a 5V power-supply?

If not
As third best alternative use your ESP8266 but there is a danger if you shortcut the 5V-pin of your ESP8266 you can destroy the USB-port of your computer.

You should run a very basic test.
Connecting GND and GND of the power-supply
connect 5V pin of your flowmeter with 5V

measure the voltage between gnd and signal-pin of your flowmeter with a digital multimeter

If you blow air gently into the flowmeter.
Does the voltage change on the digital multimeter ?

Try if you can make the flow-sensor stop with signal-voltage HIGH (aproximately 5 V)
Try if you can make the flow-sensor stop with signal-voltage LOW (aproximately 0 V)

Use the voltage-divider as shown above to protect your ESP8266 against to high voltages.

Next test:
write a testcode that does nothing more than printing the logic state of your input-pin to the serial monitor.

Hello, i have try to use voltage divider but the measurement still 0 mL

if you disconnect the YFS201 and take the GPIO4 pin alternately to 3.3V and GND via a resistor does the count change?
what specific ESP8266 board are you using ? give a link or a photo?

Can you explain to me how this message is helping to analyse your problem?
This is a way too short information to be really useful.

Post exact that complete sketch that you used in your test.
If you have modified the code repeat the test.
Then post the complete sketch

Assuming youuse a code that configures the IO-pin into mode INPUT_PULLUP
What happends if you do a test not with your sensor but with just a wire or a button
where you have one end of your wire connected to the IO-pin and then make temporarily contact to ground of the ESP8266

Are you powering the flow sensor with 5V?
What flow rate are you using for testing?
How are you measuring the actual flow rate?

No, mine with Vin ESP8266

I use this

I am so sorry, okay let me explain my problem.

YF S201 is sensor for water flow, and measure volume of water. I use this sensor on my valve, when i connect my sensor through ESP32, it can read from sensor with my code. e.g (Volume =200mL). But if i use ESP8266 V3, with voltage divider (in my esp32 i use this too), this sensor cannont read my sensor. The output just stuck in 0mL. I tried to print the pulse, as the result the volume stay with zero value.

Additionally, i do not have modified my code (I modify only the pin). I use my D2 and i write GPIO4.

I will try your advice about test without my sensor. I will report in here. Thankyou :slight_smile:

Does your NodeMCU board have a pin labeled VU?

Yes there is