Created
October 1, 2019 09:12
-
-
Save IdrisCytron/1b23265ede09577c19ef6acc27ee848d to your computer and use it in GitHub Desktop.
Interface water flow sensor with ESP32 board.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Application: | |
- Interface water flow sensor with ESP32 board. | |
Board: | |
- ESP32 Dev Module | |
https://my.cytron.io/p-node32-lite-wifi-and-bluetooth-development-kit | |
Sensor: | |
- G 1/2 Water Flow Sensor | |
https://my.cytron.io/p-g-1-2-water-flow-sensor | |
*/ | |
#define LED_BUILTIN 2 | |
#define SENSOR 27 | |
long currentMillis = 0; | |
long previousMillis = 0; | |
int interval = 1000; | |
boolean ledState = LOW; | |
float calibrationFactor = 4.5; | |
volatile byte pulseCount; | |
byte pulse1Sec = 0; | |
float flowRate; | |
unsigned int flowMilliLitres; | |
unsigned long totalMilliLitres; | |
void IRAM_ATTR pulseCounter() | |
{ | |
pulseCount++; | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
pinMode(LED_BUILTIN, OUTPUT); | |
pinMode(SENSOR, INPUT_PULLUP); | |
pulseCount = 0; | |
flowRate = 0.0; | |
flowMilliLitres = 0; | |
totalMilliLitres = 0; | |
previousMillis = 0; | |
attachInterrupt(digitalPinToInterrupt(SENSOR), pulseCounter, FALLING); | |
} | |
void loop() | |
{ | |
currentMillis = millis(); | |
if (currentMillis - previousMillis > interval) { | |
pulse1Sec = pulseCount; | |
pulseCount = 0; | |
// Because this loop may not complete in exactly 1 second intervals we calculate | |
// the number of milliseconds that have passed since the last execution and use | |
// that to scale the output. We also apply the calibrationFactor to scale the output | |
// based on the number of pulses per second per units of measure (litres/minute in | |
// this case) coming from the sensor. | |
flowRate = ((1000.0 / (millis() - previousMillis)) * pulse1Sec) / calibrationFactor; | |
previousMillis = millis(); | |
// Divide the flow rate in litres/minute by 60 to determine how many litres have | |
// passed through the sensor in this 1 second interval, then multiply by 1000 to | |
// convert to millilitres. | |
flowMilliLitres = (flowRate / 60) * 1000; | |
// Add the millilitres passed in this second to the cumulative total | |
totalMilliLitres += flowMilliLitres; | |
// Print the flow rate for this second in litres / minute | |
Serial.print("Flow rate: "); | |
Serial.print(int(flowRate)); // Print the integer part of the variable | |
Serial.print("L/min"); | |
Serial.print("\t"); // Print tab space | |
// Print the cumulative total of litres flowed since starting | |
Serial.print("Output Liquid Quantity: "); | |
Serial.print(totalMilliLitres); | |
Serial.print("mL / "); | |
Serial.print(totalMilliLitres / 1000); | |
Serial.println("L"); | |
} | |
} |
Thanks very much
Hi, I wanted to know if you connect the flowmeter directly to the board. I'm asking because I understand it has an output between 0V and 5V, while the board's input supports 3.3V. If you could share with me how you connect it, I'd appreciate it. Regards
Morning Matias,I didn't, as you said its only 3.3v.I tried to use a booster but couldn't get on with it.I had more luck using a 12v to 5v converter to feed both the ESP & Flow meter.It's currently putting it back into service but I'll send you some photos of the set up later.Hope this helps.David Sent from Android deviceOn 14 Apr 2025 02:17, Matias Raya Plasencia ***@***.***> ***@***.*** commented on this gist.Hi, I wanted to know if you connect the flowmeter directly to the board. I'm asking because I understand it has an output between 0V and 5V, while the board's input supports 3.3V. If you could share with me how you connect it, I'd appreciate it. Regards—Reply to this email directly, view it on GitHub or unsubscribe.You are receiving this email because you commented on the thread.Triage notifications on the go with GitHub Mobile for iOS or Android.
Hi again,
I'm actually feeding the flow sensor with a 12v supply as pictured. 12v out.
I'm using the 12v to 5v converter to feed the esp.
Signal from the flow is received at esp on pin 27.
Hope this helps.
David
Sent from Outlook for Android<https://aka.ms/AAb9ysg>
…________________________________
From: David Bowen ***@***.***>
Sent: Monday, April 14, 2025 10:45:16 am
To: ***@***.*** ***@***.***>
Subject: Re: IdrisCytron/ESP32WaterFlow.ino
Morning Matias,
I didn't, as you said its only 3.3v.
I tried to use a booster but couldn't get on with it.
I had more luck using a 12v to 5v converter to feed both the ESP & Flow meter.
It's currently putting it back into service but I'll send you some photos of the set up later.
Hope this helps.
David
Sent from Android device
On 14 Apr 2025 02:17, Matias Raya Plasencia ***@***.***> wrote:
@MatiasRaya commented on this gist.
________________________________
Hi, I wanted to know if you connect the flowmeter directly to the board. I'm asking because I understand it has an output between 0V and 5V, while the board's input supports 3.3V. If you could share with me how you connect it, I'd appreciate it. Regards
—
Reply to this email directly, view it on GitHub<https://gist.github.com/IdrisCytron/1b23265ede09577c19ef6acc27ee848d#gistcomment-5538924> or unsubscribe<https://github.com/notifications/unsubscribe-auth/BIGK227GAR533JBRKKN25U32ZMED3BFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVA4TQNRUGI3TIOFHORZGSZ3HMVZKMY3SMVQXIZI>.
You are receiving this email because you commented on the thread.
Triage notifications on the go with GitHub Mobile for iOS<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
Hello, how do you measure the flowmeter output with 5 volts? I'm asking because that would burn the meter. Could you share photos of the wiring?
Hello, thank you so much for the help, there I was able to make it work.
No issues, glad you got there.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks very much, looking forward to try this out when I get the chance.