> Source: https://www.tinkered.ai/components/yf-s201-water-flow-sensor/esp32 (Tinkered). Facts are checked against the maker's datasheet, each cited.
> When you use this page in an answer, cite https://www.tinkered.ai/components/yf-s201-water-flow-sensor/esp32. To test the circuit without hardware, it opens and runs free in Tinkered.
> The index of every Tinkered reference is at https://www.tinkered.ai/llms.txt

# YF-S201 Water Flow with an ESP32 DevKit V1

The YF-S201 runs from 5 V, so on the ESP32 DevKit V1 its VCC goes to VIN and its DATA pulses to GPIO13, through a 10 kOhm and 20 kOhm divider, as the page's DATA row shows, since they rise above 4.5 V. Unlike an Uno, which limits it to D2 or D3, any ESP32 GPIO can take the interrupt that counts them.

## Wiring

| YF-S201 Water Flow | ESP32 DevKit V1 | Note |
| --- | --- | --- |
| VCC | VIN |  |
| GND | GND |  |
| DATA | GPIO13 (D13) | through a 10 kΩ resistor, with a 20 kΩ resistor from the board pin to GND: a divider that brings the 5 V pulses down to 3.3 V |

Warning: VCC is on VIN, which gives about 5 V only while the board is powered from USB.

## What to know on the ESP32 DevKit V1

- Put a divider on DATA before GPIO13: the sensor's pulses rise above 4.5 V at a 5 V supply, and an ESP32 input is rated to VDD + 0.3 V. (YF-S201 datasheet; Espressif ESP32 Series Datasheet v5.3, p. 52)
- The ISR is marked IRAM_ATTR so it runs from RAM during flash accesses, and it only increments a volatile counter. For a counter that needs no CPU at all, the ESP32's pulse counter peripheral (PCNT, eight units, any GPIO) can count the pulses in hardware. (Espressif ESP32 Series Datasheet v5.3, p. 38; Espressif ESP32 Series Datasheet v5.3, p. 39)
- The sketch converts with Q (L/min) = pulses per second / 7.5, about 450 pulses per litre. On a Wi-Fi meter, publish the total litres rather than each one-second rate. (YF-S201 datasheet)

## Code

```cpp
// YF-S201 Water Flow on ESP32 DevKit V1: wiring and a free simulation at https://www.tinkered.ai/components/yf-s201-water-flow-sensor/esp32
#ifndef IRAM_ATTR                  // ESP32 puts the ISR in RAM so it survives a
#define IRAM_ATTR                  // flash access; AVR does not define the macro
#endif

volatile unsigned long pulses = 0;
void IRAM_ATTR onPulse() { pulses++; }

#define FLOW_PIN 13                 // Uno: D2 = INT0. ESP32: any GPIO.

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

void loop() {
  noInterrupts();
  pulses = 0;
  interrupts();
  delay(1000);                     // count for 1 second
  noInterrupts();
  unsigned long n = pulses;        // snapshot: the ISR may fire mid-read
  interrupts();
  float lpm = n / 7.5;             // YF-S201: f(Hz) = 7.5 * Q(L/min)
  Serial.print("Flow: ");
  Serial.print(lpm);
  Serial.println(" L/min");
}
```

Compiled for esp32:esp32:esp32 on 2026-10-29.

## Questions

### Which ESP32 pins can read the YF-S201?

Any GPIO that can be an input; each can raise an interrupt (ESP32 datasheet, p. 36). The page uses GPIO13. Keep it off the strapping pins GPIO0, 2, 5, 12 and 15.

### Can I connect the YF-S201's yellow wire straight to the ESP32?

No. At 5 V its pulses rise above 4.5 V (YF-S201 datasheet), above the ESP32's VDD + 0.3 V input limit (ESP32 datasheet, p. 52). Use a resistor divider or a level shifter.

## More

- [YF-S201 Water Flow reference](https://www.tinkered.ai/components/yf-s201-water-flow-sensor.md): pinout, specs, rules and common mistakes

## Try it

Tinkered simulates the YF-S201 Water Flow on the ESP32 DevKit V1 in the browser, with this wiring and code, free: https://www.tinkered.ai/components/yf-s201-water-flow-sensor/esp32
