Skip to content

Instantly share code, notes, and snippets.

@elktros
Created June 18, 2018 10:48
Show Gist options
  • Save elktros/a1caed1f2a4c29756735678bfefa2eca to your computer and use it in GitHub Desktop.
Save elktros/a1caed1f2a4c29756735678bfefa2eca to your computer and use it in GitHub Desktop.
Code for interfacing YF-S201 Water Flow Sensor with Arduino UNO.
const int watermeterPin = 2;
volatile int pulse_frequency;
unsigned int literperhour;
unsigned long currentTime, loopTime;
byte sensorInterrupt = 0;
void setup()
{
pinMode(watermeterPin, INPUT);
Serial.begin(9600);
attachInterrupt(sensorInterrupt, getFlow, FALLING);
currentTime = millis();
loopTime = currentTime;
}
void loop ()
{
currentTime = millis();
if(currentTime >= (loopTime + 1000))
{
loopTime = currentTime;
literperhour = (pulse_frequency * 60 / 7.5);
pulse_frequency = 0;
Serial.print(literperhour, DEC);
Serial.println(" Liter/hour");
}
}
void getFlow ()
{
pulse_frequency++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment