Advertisement
microrobotics

ESP32 YF-S401 with 0.96" OLED

Aug 13th, 2024 (edited)
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Written by Lucienne Swart
  3.    YF-S401 connected to a logic level converter with a 10k Pull-down resistor on signal.  
  4.  */
  5.  
  6. #include <Wire.h>
  7. #include <Adafruit_GFX.h>
  8. #include <Adafruit_SSD1306.h>
  9.  
  10. // OLED display dimensions
  11. #define SCREEN_WIDTH 128
  12. #define SCREEN_HEIGHT 64
  13.  
  14. // I2C OLED address
  15. #define OLED_ADDR 0x3C // Change to 0x3D if your OLED has a different address
  16.  
  17. // Create an SSD1306 display object
  18. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  19.  
  20. volatile int pulseCount = 0;   // Variable to count the number of pulses in the last second
  21. unsigned long lastTime = 0;    // Variable to track the last time the flow rate was calculated
  22. double totalLiters = 0;        // Total volume of water passed in liters
  23.  
  24. const int flowMeterPin = 14;   // Define the pin connected to the flow meter
  25. const double pulsesPerLiter = 5880.0; // Number of pulses per liter (adjust as needed)
  26.  
  27. void IRAM_ATTR pulse() {
  28.   pulseCount++;               // Increment pulse count for each pulse detected
  29. }
  30.  
  31. void setup() {
  32.   Serial.begin(115200);       // Initialize serial communication at 115200 baud
  33.  
  34.   // Initialize the OLED display
  35.   if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
  36.     Serial.println(F("SSD1306 allocation failed"));
  37.     for (;;); // Don't proceed, loop forever
  38.   }
  39.  
  40.   // Clear the display
  41.   display.clearDisplay();
  42.  
  43.   // Display static labels
  44.   display.setTextSize(1);
  45.   display.setTextColor(SSD1306_WHITE);
  46.   display.setCursor(0, 0);
  47.   display.println("Flow Meter Readings:");
  48.   display.setCursor(0, 16);
  49.   display.println("Flow Rate:      L/min");
  50.   display.setCursor(0, 32);
  51.   display.println("Total Volume:    L");
  52.   display.display(); // Display static text
  53.  
  54.   // Attach interrupt to the flow meter pin
  55.   pinMode(flowMeterPin, INPUT);
  56.   attachInterrupt(digitalPinToInterrupt(flowMeterPin), pulse, RISING);
  57. }
  58.  
  59. void loop() {
  60.   unsigned long currentTime = millis(); // Get the current time
  61.  
  62.   // Calculate flow rate every second
  63.   if (currentTime - lastTime >= 1000) {
  64.     // Calculate flow rate in liters per minute
  65.     double flowRate = (pulseCount / pulsesPerLiter) * 60.0;
  66.  
  67.     // Calculate total volume in liters
  68.     totalLiters += (pulseCount / pulsesPerLiter);
  69.  
  70.     // Reset pulse count for the next second
  71.     pulseCount = 0;
  72.  
  73.     // Update last time
  74.     lastTime = currentTime;
  75.  
  76.     // Display the flow rate and total volume on OLED
  77.     display.setTextSize(1);
  78.     display.setTextColor(SSD1306_WHITE);
  79.  
  80.     // Clear the area where the flow rate and total volume are displayed
  81.     display.fillRect(70, 16, 58, 8, SSD1306_BLACK); // Clear Flow Rate area
  82.     display.fillRect(70, 32, 58, 8, SSD1306_BLACK); // Clear Total Volume area
  83.  
  84.     // Display flow rate with 2 decimal places
  85.     display.setCursor(70, 16);
  86.     display.printf("%7.2f", flowRate);
  87.  
  88.     // Display total volume with 2 decimal places
  89.     display.setCursor(70, 32);
  90.     display.printf("%7.2f", totalLiters);
  91.  
  92.     display.display(); // Update the display
  93.   }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement