ESP8266 Flow Sensor Project

This project demonstrates how to interface a flow sensor with an ESP8266 microcontroller to measure the flow rate and total volume of liquid passing through the sensor. The data is displayed on the Serial Monitor.

How It Works

A force-sensitive resistor (FSR) is a polymer film whose resistance drops as pressure squeezes its conductive particles closer together. Unloaded it measures in the megohms; under firm finger pressure it falls to a few hundred ohms. That is a usefully wide range, but the relationship is logarithmic and quite imprecise — FSRs sense force, they do not measure it accurately.

Because a microcontroller reads voltage rather than resistance, the FSR is wired as one half of a voltage divider with a fixed resistor. The choice of that resistor sets which part of the force range gets the most resolution: 10 kΩ is a sensible general-purpose starting point, while a larger value favours light touches and a smaller one favours heavy pressure.

The ESP8266 (NodeMCU) samples with a 10-bit ADC, so analogRead() returns 0–1023 across 3.3 V at the NodeMCU A0 header (the bare chip reads 0–1 V). Expect repeatability of roughly ±10% between presses on the same sensor, and considerably more between different sensors. Anything needing real accuracy — a scale, for instance — calls for a load cell and an HX711 amplifier instead.

Components Needed

  • ESP8266 (NodeMCU)
  • Flow Sensor (e.g., YF-S201)
  • Jumper Wires
  • ESP8266 (NodeMCU)

Wiring to the ESP8266 (NodeMCU)

Wire one FSR leg to the 3.3 V rail and the other to A0, then connect the 10 kΩ resistor from A0 to GND. As force increases, FSR resistance falls, so the midpoint voltage — and the ADC reading — rises.

FSRs are unpolarised, so the two legs are interchangeable. Their tails are delicate: solder quickly or, better, use a crimp connector, because excessive heat delaminates the film permanently.

Press through a small rigid pad rather than directly with a fingertip. Spreading load across the whole active area makes readings far more consistent, which is how commercial FSR buttons are built.

ConnectionESP8266 (NodeMCU) pinFunction
FSR leg 13V3Supply
FSR leg 2A0Divider midpoint — the measured node
10 kΩ resistorA0 to GNDFixed half of the divider

Build and Upload

Open the Arduino IDE with ESP8266 board support installed.

Install the necessary libraries if required.

Create a new sketch and paste the provided Arduino code.

Connect the ESP8266 to your computer, select the appropriate board and port from the Tools menu.

Upload the code to the ESP8266.

After uploading the code, open the Serial Monitor.

You should see the flow rate and total volume printed every second.

Example Code

Reading an FSR and classifying pressure into usable bands. Upload it with the board set to ESP8266 (NodeMCU) and open the Serial Monitor at 115200 baud.

Reading an FSR and classifying pressure into usable bands
const int FSR_PIN = A0;
const long SERIES_OHMS = 10000;
const float VREF = 3.3;
const int   ADC_MAX = 1023;

void setup() { Serial.begin(115200); }

void loop() {
  int raw = analogRead(FSR_PIN);

  if (raw < 10) {
    Serial.println("no pressure");
  } else {
    // Recover the FSR resistance from the divider
    float volts = raw * VREF / ADC_MAX;
    float fsrOhms = SERIES_OHMS * (VREF - volts) / volts;

    Serial.print("raw=");
    Serial.print(raw);
    Serial.print("  R=");
    Serial.print(fsrOhms, 0);
    Serial.print(" ohm  ");

    if      (raw < 205) Serial.println("light touch");
    else if (raw < 614) Serial.println("medium press");
    else                                          Serial.println("firm press");
  }
  delay(200);
}

Applications

A force sensitive resistor turns up in a wide range of projects. These are the uses where it is the right choice rather than a compromise:

  • Pressure-sensitive buttons and musical interfaces with velocity response
  • Occupancy detection in seats, beds and mats
  • Grip-strength feedback on robotic hands and prosthetics
  • Detecting whether an object is present and roughly how heavy it is
  • Touch-sensitive panels behind a flexible surface

Working with the ESP8266 (NodeMCU)

The ESP8266 (NodeMCU) is built around the ESP8266 and runs on 3.3 V logic with roughly 80 KB usable of SRAM and 4 MB on most NodeMCU boards of program flash. These details change how this circuit is wired and what the sketch can do, so they are worth stating plainly before you build.

The ESP8266 is a 3.3 V part and is not 5 V tolerant — feeding a 5 V sensor output straight into a GPIO can damage the chip. Use a divider or level shifter.

There is only one ADC channel, so reading several analog sensors needs an external multiplexer such as a CD4051 or an ADS1115.

D3 (GPIO0), D4 (GPIO2) and D8 (GPIO15) are strapping pins sampled at boot; holding them at the wrong level stops the board starting.

Built-in WiFi means the same sketch can publish readings to a dashboard without extra hardware.

ESP8266 (NodeMCU) characteristicValueWhy it matters here
Logic voltage3.3 VSensor outputs above this level need a divider or level shifter
ADC resolution10-bit (0–1023)Sets how finely an analog reading can be resolved
Analog inputsa single analog channel, A0Determines how many analog sensors can share the board
PWM outputsany GPIO via software PWMNeeded for brightness, speed and tone control
I²C pinsD2 (SDA, GPIO4) and D1 (SCL, GPIO5) by defaultFixed by hardware — wiring copied from another board may not match
Interrupt pinsany GPIO except D0 (GPIO16)Required for counting fast or asynchronous events
Serialone hardware UART plus a transmit-only second portMonitor runs at 115200 baud by default

Troubleshooting

Most problems with this module fall into a handful of categories. Work through these before suspecting the part itself:

  • The reading never returns to zero after a press — the film is slow to relax, which is normal. Allow a settling period or subtract a drifting baseline.
  • Light touches barely register — increase the series resistor to 47 kΩ or 100 kΩ to favour the low-force end.
  • Readings differ between two identical sensors — unit-to-unit variation is large. Calibrate each one individually.
  • The sensor stopped working after soldering — the tail has delaminated from heat; use connectors in future.
  • The sketch compiles but the board resets or behaves erratically — a 5 V module output is being driven into a 3.3 V pin. Measure the signal before connecting it.
  • Readings differ from an Arduino tutorial for the same part — the 10-bit ADC returns 0–1023, not 0–1023, so any constant copied from an Uno example needs rescaling.

Taking It Further on the ESP8266 (NodeMCU)

Once the basic reading works, where you go next depends very much on which board you are using. These are the directions that suit the ESP8266 (NodeMCU) specifically:

The obvious extension on an ESP8266 is to stop printing to Serial and publish instead. A few lines using ESP8266WiFi and an HTTP client will push each reading to a dashboard such as ThingSpeak, or to an MQTT broker for home automation.

Running an onboard web server turns the board into its own display: serve a small HTML page that fetches the latest reading over AJAX, and any phone on the network becomes the instrument panel.

For battery operation, deep sleep is essential — the ESP8266 draws around 70 mA with WiFi active but under 20 µA asleep. Wire D0 (GPIO16) to RST so the board can wake itself, take a reading, publish and sleep again.

Notes and Practical Limits

Think of an FSR as a qualitative input. "Something is pressing, and roughly this hard" is reliable; "this object weighs 212 grams" is not.

For weight measurement use a load cell with an HX711 24-bit amplifier. That combination resolves single grams and holds calibration, which an FSR cannot approach.