microrobotics icon

ESP32 YF-S401 with 0.96" OLED

microrobotics | PRO | 08/13/24 08:50:38 AM UTC (Edited) | 0 ⭐ | 15093 👁️ | Never ⏰ | []
Arduino |

3 KB

|

None

|

0 👍

/

0 👎

/*
 * Written by Lucienne Swart
   YF-S401 connected to a logic level converter with a 10k Pull-down resistor on signal.  
 */
 
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
// OLED display dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
 
// I2C OLED address
#define OLED_ADDR 0x3C // Change to 0x3D if your OLED has a different address
 
// Create an SSD1306 display object
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
 
volatile int pulseCount = 0;   // Variable to count the number of pulses in the last second
unsigned long lastTime = 0;    // Variable to track the last time the flow rate was calculated
double totalLiters = 0;        // Total volume of water passed in liters
 
const int flowMeterPin = 14;   // Define the pin connected to the flow meter
const double pulsesPerLiter = 5880.0; // Number of pulses per liter (adjust as needed)
 
void IRAM_ATTR pulse() {
  pulseCount++;               // Increment pulse count for each pulse detected
}
 
void setup() {
  Serial.begin(115200);       // Initialize serial communication at 115200 baud
 
  // Initialize the OLED display
  if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  
  // Clear the display
  display.clearDisplay();
 
  // Display static labels
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println("Flow Meter Readings:");
  display.setCursor(0, 16);
  display.println("Flow Rate:      L/min");
  display.setCursor(0, 32);
  display.println("Total Volume:    L");
  display.display(); // Display static text
 
  // Attach interrupt to the flow meter pin
  pinMode(flowMeterPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(flowMeterPin), pulse, RISING);
}
 
void loop() {
  unsigned long currentTime = millis(); // Get the current time
 
  // Calculate flow rate every second
  if (currentTime - lastTime >= 1000) {
    // Calculate flow rate in liters per minute
    double flowRate = (pulseCount / pulsesPerLiter) * 60.0;
 
    // Calculate total volume in liters
    totalLiters += (pulseCount / pulsesPerLiter);
 
    // Reset pulse count for the next second
    pulseCount = 0;
 
    // Update last time
    lastTime = currentTime;
 
    // Display the flow rate and total volume on OLED
    display.setTextSize(1);
    display.setTextColor(SSD1306_WHITE);
 
    // Clear the area where the flow rate and total volume are displayed
    display.fillRect(70, 16, 58, 8, SSD1306_BLACK); // Clear Flow Rate area
    display.fillRect(70, 32, 58, 8, SSD1306_BLACK); // Clear Total Volume area
 
    // Display flow rate with 2 decimal places
    display.setCursor(70, 16);
    display.printf("%7.2f", flowRate);
 
    // Display total volume with 2 decimal places
    display.setCursor(70, 32);
    display.printf("%7.2f", totalLiters);
 
    display.display(); // Update the display
  }
}
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎