Flow Meter Arduino Code Tutorial: Writing Pulse Counter Sketches for YF-S201 Sensors

Flow Meter Arduino Code Tutorial: Writing Pulse Counter Sketches for YF-S201 Sensors

Price:¥1.00Quantity:9999

Market Price:¥1.00Reduced Price:¥1.00

Effevtive Time:8/26/2026

Sale Address:SilverProduction Address:Silver automation instruments

Keywords:Flow Meter Arduino Code Tutorial: Writing Pulse Counter Sketches for YF-S201 Sensors

Phone:QQ:Click:WhatsApp: +86 18936759191

Company:Xingtai water flow meter manufacturer

『Flow Meter Arduino Code Tutorial: Writing Pulse Counter Sketches for YF-S201 Sensors』Related information(v cone flow meter|rotary gas meter|gallon meter|gear meter|metal tube rotameter|glass tube rotameter|nutating disk meter|transit time ultrasonic flow meter|wedge flow meter|pd meters|hydraulic flow meter|ultrasonic water flow meter|water flow meter|air flow meter|magnetic water flow meter|oxygen flow meter|gas flow meter|argon flow meter|air velocity meter|gasoline flow meter|fuel flow meter|ultrasonic water meter|digital water meter|nitrogen flow meter|solid flow meter|Oxygen tank flow meter|digital water flow meter|diesel fuel flow meter|steam flow meter|02 flow meter|compressed air flow meter|oil flow meter|liquid flow meter)

Flow Meter Arduino Code Tutorial: Writing Pulse Counter Sketches for YF-S201 Sensors


Quick Answer: The YF-S201 flow sensor outputs a square wave frequency. Use an Arduino interrupt to count pulses on pin 2. Divide pulses per second by 7.5 to get flow rate in liters per minute.


What the YF-S201 Flow Sensor Does

The YF-S201 is a Hall effect turbine flow sensor for clean water and low viscosity fluids. The sensor operates at 5 VDC and covers a range of 1 to 30 L/min. The output signal is a square wave. The frequency is about 7.5 Hz per L/min. That equals roughly 450 pulses per liter.


Most engineers skip this part in a rush. But the sensor body is plastic. Rotor wear and hard water scale can change the pulse factor over time. Keep the sensor vertical and mount the rotor shaft horizontal. This setup reduces bearing friction.


Wiring the YF-S201 to an Arduino Uno

Connect the red wire to 5 V, the black wire to GND, and the yellow signal wire to digital pin 2. The YF-S201 signal output is open collector. Use the internal pull-up resistor in the Arduino sketch. For cable runs longer than 2 meters, add a 10 kΩ resistor from the signal wire to 5 V.


Keep the signal wire away from AC motor drives and contactor coils. Electrical noise can create false pulses. In practice, a twisted pair from the sensor to the Arduino solves most noise problems.


Pulse Counter Sketch for YF-S201

Copy this sketch into Arduino IDE. The code counts falling edges on digital pin 2. Every second the code calculates frequency and converts it to liters per minute.


#define SENSOR_PIN 2
volatile unsigned long pulseCount = 0;
unsigned long lastMillis = 0;

void countPulse() {
  pulseCount++;
}

void setup() {
  Serial.begin(9600);
  pinMode(SENSOR_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), countPulse, FALLING);
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - lastMillis >= 1000) {
    noInterrupts();
    unsigned long pulses = pulseCount;
    pulseCount = 0;
    interrupts();
    float flowLmin = pulses / 7.5;
    Serial.print("Flow rate: ");
    Serial.print(flowLmin);
    Serial.println(" L/min");
    lastMillis = currentMillis;
  }
}

Calibrating the YF-S201

Use this formula: flow rate in L/min equals pulse frequency in Hz divided by 7.5. For total volume, divide total pulse count by 450. The datasheet factor is a starting point. Sensor to sensor variation is real.


Here is the thing. A water treatment pilot plant in Malaysia saw a 9 percent offset at 2 L/min. The team filled a 5 L beaker and counted pulses. They adjusted the factor to 7.0 and the reading matched the beaker within 1 percent. Do this calibration before logging data.


When to Move from YF-S201 to an Industrial Flow Meter

The YF-S201 is a bench test tool. The sensor is not suitable for diesel, solvents, hot water above 80 °C, high pressure, or conductive wastewater. Plastic threads and a small rotor limit chemi

Flow Meter Arduino Code Tutorial: Writing Pulse Counter Sketches for YF-S201 Sensors
cal resistance and mechanical life.


We have seen this on customer sites many times. Last year a paint manufacturer in Ho Chi Minh City used a YF-S201 for solvent trials. After three months the rotor swelled and the pulse signal became unstable. Silver Automation Instruments supplied an oval gear flow meter for the solvent batching line. The meter now runs 24 hours with a pulse output to the PLC.


For conductive water and wastewater, an electromagnetic flow meter is a better fit. These meters have no moving parts. Accuracy is 0.5 percent of reading. Available sizes start at DN15 and go to DN2000. Output options include 4-20 mA HART, pulse, and RS485. Water must have conductivity above 20 µS/cm for a stable reading.


For diesel, lube oil, and solvents, an oval gear flow meter works well. These meters measure small volumes and tolerate viscosities up to 2000 cP. We can supply ATEX Zone 1 versions for storage areas.


For chemical mass flow, a Coriolis mass flow meter gives kg/h or g/min directly. It measures mass, density, and temperature. A PT100 sensor handles temperature compensation. This is the right choice when recipe accuracy matters.


Recommended Industrial Flow Meter Pairings

Pick a meter by fluid and pipe size. For a DN25 water line, use an electromagnetic flow meter. For a 2 inch diesel loading skid in Southeast Asia, use an oval gear flow meter with pulse output. For a chemical reactor feed in a food additive plant, use a Coriolis mass flow meter.


Silver Automation Instruments is an industrial flow meter manufacturer and supplier. We keep stock for common DN15 to DN100 sizes. Custom orders for larger lines ship from our Nanjing factory.


Contact us with these four values. Pressure in bar, temperature in °C, pipe size in DN, and flow range. We will confirm a model and price the same day.


Tel: +86-25-68650347

WhatsApp: +86-25-52155837

WeChat: +86 15365082610


FAQ: YF-S201 Pulse Counter Sketches

What is the pulse output frequency of the YF-S201 flow sensor?

The YF-S201 outputs about 7.5 Hz per L/min. One liter equals about 450 pulses.


Can the YF-S201 measure diesel or chemicals?

No. The plastic body and small rotor are for clean water and low viscosity non-corrosive fluids. Use an oval gear meter or a Coriolis meter for diesel and solvents.


How do I convert YF-S201 pulses to total liters?

Divide total pulse count by 450. Calibrate with a known volume for better accuracy.


What Arduino pins work for YF-S201 pulse counting?

On Arduino Uno use digital pin 2 or 3. On Arduino Mega use any interrupt capable pin. Use attachInterrupt with digitalPinToInterrupt.


What industrial flow meter should I use for a DN25 water line?

An electromagnetic flow meter is often a good fit for conductive water. Send pipe size, flow range, pressure, and temperature to Silver Automation Instruments for a quote.


『SILVER Official Website SERVICE』

Copyright2026SILVER E-Commerce
+86 15365082610