9. Input Devices¶
group assignment: • probe an input device’s analog levels and digital signals
individual assignment: • measure something: add a sensor to a microcontroller board that you have designed and read it
I tested the following sensor:
Flow Rate Sensor¶
The YF-S201 water flow sensor was used in my system to measure the movement of water through a pipe. It works using a small turbine inside the sensor. When water flows, the turbine rotates and produces pulses using a Hall effect sensor. These pulses are then read by the microcontroller and converted into flow rate.
Specifications
- Operating voltage: 5–18V DC
- Output signal: digital pulse (5V TTL)
- Flow range: 1–30 L/min
- Accuracy: ±10%
- Max current: 15mA @ 5V
- Operating temperature: -25°C to +80°C
- Max water pressure: 2.0 MPa
Setup
The sensor was connected to a Xiao ESP32-C3 board. The signal pin was connected to a digital interrupt pin (D2). Every pulse generated by the sensor was counted in the code.
At the start, I tested the sensor by simply printing raw pulse counts on the serial monitor to confirm that it was responding correctly.
Working Principle in Code
The sensor produces pulses depending on the flow of water. These pulses are counted using an interrupt function so that none are missed.
The flow rate is calculated using the formula:
Flow rate (L/min) = pulse count / 7.5
The pulse count is reset every 1 second and a new value is calculated.
#include <WiFi.h>
#include <PubSubClient.h>
// WiFi
const char* ssid = "MT";
const char* password = "#@Innovate";
// MQTT
const char* mqtt_server = "broker.emqx.io";
WiFiClient espClient;
PubSubClient client(espClient);
// Flow sensor
const int flowPin = D2; // D2 (change if needed)
volatile int pulseCount = 0;
float flowRate = 0.0;
float totalLiters = 0.0;
unsigned long previousMillis = 0;
// Interrupt
void IRAM_ATTR pulseCounter() {
pulseCount++;
}
// Connect WiFi
void setup_wifi() {
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
}
// Reconnect MQTT
void reconnect() {
while (!client.connected()) {
if (client.connect("ESP32FlowClient")) {
Serial.println("MQTT connected");
} else {
delay(2000);
}
}
}
void setup() {
Serial.begin(115200);
pinMode(flowPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(flowPin), pulseCounter, FALLING);
setup_wifi();
client.setServer(mqtt_server, 1883);
}
void loop() {
if (!client.connected()) reconnect();
client.loop();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 1000) { // every 1 sec
previousMillis = currentMillis;
// Calculate flow rate (L/min)
flowRate = pulseCount / 7.5;
// Convert to liters per second and accumulate
float litersThisSecond = flowRate / 60.0;
totalLiters += litersThisSecond;
pulseCount = 0;
// Convert to string
char flowMsg[20];
char totalMsg[20];
sprintf(flowMsg, "%.2f", flowRate);
sprintf(totalMsg, "%.2f", totalLiters);
// Publish to MQTT
client.publish("water/flow", flowMsg);
client.publish("water/total", totalMsg);
// Debug
Serial.print("Flow: ");
Serial.print(flowRate);
Serial.print(" L/min | Total: ");
Serial.println(totalLiters);
}
}
Results