Diy boat fuel consumption

I'm working on setting up a fuel consumption monitor for my twin inboard boat, but the commercial options are way too pricey. I've decided to take matters into my own hands and create one using an Arduino Mega, a 20x4 LCD, and 2 OF05ZAT flow meters. I'm not very experienced in programming, so I could use some guidance.

Here's what I have:

  • Arduino Mega
  • 20x4 LCD
  • 2 OF05ZAT flow meters (0.454 ml per pulse)
  • 120-gallon fuel tank

I'm aiming to display the 10-second average gallons per hour for both motors on the LCD as follows:

  • Port Motor: P: [gph]
  • Starboard Motor: S: [gph]

Additionally, I'd like to show the total gallons used as T: [gallons] and the remaining gallons as R: [gallons].

Any help or guidance on the programming side would be greatly appreciated. Thanks in advance!

Did you try to write the code? Please show your effors

Using chat gpt yes but the numbers are way off

There is most likely a flow rate calculation you are missing.

This is what I got before I stopped messing with it, this should be for a 16x2 display but I bought a bigger one

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address and size (0x27 for a common 16x2 display)

volatile unsigned long pulseCountPin2 = 0; // Initialize pulse count variable for pin 2 (P)
volatile unsigned long pulseCountPin3 = 0; // Initialize pulse count variable for pin 3 (S)
unsigned long previousMillis = 0; // Initialize previous time
unsigned long interval = 1000; // Interval to calculate pulses per second
float microlitersPerPulse = 454.0; // Microliters per pulse
float gallonsPerHourPin2 = 0.0; // Flow rate in GPH for pin 2 (P)
float gallonsPerHourPin3 = 0.0; // Flow rate in GPH for pin 3 (S)

void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("P GPH: 0.00");
lcd.setCursor(0, 1);
lcd.print("S GPH: 0.00");

Serial.begin(9600); // Initialize serial communication
attachInterrupt(digitalPinToInterrupt(2), countPulsePin2, RISING); // Attach an interrupt to pin 2 (P)
attachInterrupt(digitalPinToInterrupt(3), countPulsePin3, RISING); // Attach an interrupt to pin 3 (S)
}

void loop() {
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
// Calculate pulses per second for both pins
float pulsesPerSecondPin2 = (float)pulseCountPin2 / (float)interval;
float pulsesPerSecondPin3 = (float)pulseCountPin3 / (float)interval;

// Calculate gallons per hour for both pins
gallonsPerHourPin2 = (pulsesPerSecondPin2 * microlitersPerPulse) / 1000.0 * 3600.0; // Convert microliters to gallons
gallonsPerHourPin3 = (pulsesPerSecondPin3 * microlitersPerPulse) / 1000.0 * 3600.0; // Convert microliters to gallons

// Display on the LCD
lcd.setCursor(7, 0);
lcd.print("   "); // Clear the previous value
lcd.setCursor(7, 0);
lcd.print(gallonsPerHourPin2, 2); // Display with 2 decimal places

lcd.setCursor(7, 1);
lcd.print("   "); // Clear the previous value
lcd.setCursor(7, 1);
lcd.print(gallonsPerHourPin3, 2); // Display with 2 decimal places

// Reset pulse counts and update the previous time
pulseCountPin2 = 0;
pulseCountPin3 = 0;
previousMillis = currentMillis;

}
}

void countPulsePin2() {
// This function is called whenever a pulse is detected on pin 2 (P)
pulseCountPin2++;
}

void countPulsePin3() {
// This function is called whenever a pulse is detected on pin 3 (S)
pulseCountPin3++;
}

The forum has a rather ambiguous attitude towards the use of AI for programming by beginners. To use the service correctly, you must understand what you are doing. In fact, only those who know how to program themselves should use AI to create code.

For a beginner, it is better to write programs yourself. Break the task into parts. Start with examples of using the screen, display some text on it. Then connect the meter and try to get readings from it.

2 Likes

Please insert the code using code tags

1 Like

I'm on my phone, how do I do that?

ok, I do it for you.
On the future, do not come to the forum from the phone, if you are going to post sketches and diagrams

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address and size (0x27 for a common 16x2 display)

volatile unsigned long pulseCountPin2 = 0; // Initialize pulse count variable for pin 2 (P)
volatile unsigned long pulseCountPin3 = 0; // Initialize pulse count variable for pin 3 (S)
unsigned long previousMillis = 0; // Initialize previous time
unsigned long interval = 1000; // Interval to calculate pulses per second
float microlitersPerPulse = 454.0; // Microliters per pulse
float gallonsPerHourPin2 = 0.0; // Flow rate in GPH for pin 2 (P)
float gallonsPerHourPin3 = 0.0; // Flow rate in GPH for pin 3 (S)

void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("P GPH: 0.00");
lcd.setCursor(0, 1);
lcd.print("S GPH: 0.00");

Serial.begin(9600); // Initialize serial communication
attachInterrupt(digitalPinToInterrupt(2), countPulsePin2, RISING); // Attach an interrupt to pin 2 (P)
attachInterrupt(digitalPinToInterrupt(3), countPulsePin3, RISING); // Attach an interrupt to pin 3 (S)
}

void loop() {
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
// Calculate pulses per second for both pins
float pulsesPerSecondPin2 = (float)pulseCountPin2 / (float)interval;
float pulsesPerSecondPin3 = (float)pulseCountPin3 / (float)interval;


// Calculate gallons per hour for both pins
gallonsPerHourPin2 = (pulsesPerSecondPin2 * microlitersPerPulse) / 1000.0 * 3600.0; // Convert microliters to gallons
gallonsPerHourPin3 = (pulsesPerSecondPin3 * microlitersPerPulse) / 1000.0 * 3600.0; // Convert microliters to gallons

// Display on the LCD
lcd.setCursor(7, 0);
lcd.print("   "); // Clear the previous value
lcd.setCursor(7, 0);
lcd.print(gallonsPerHourPin2, 2); // Display with 2 decimal places

lcd.setCursor(7, 1);
lcd.print("   "); // Clear the previous value
lcd.setCursor(7, 1);
lcd.print(gallonsPerHourPin3, 2); // Display with 2 decimal places

// Reset pulse counts and update the previous time
pulseCountPin2 = 0;
pulseCountPin3 = 0;
previousMillis = currentMillis;

}
}

void countPulsePin2() {
// This function is called whenever a pulse is detected on pin 2 (P)
pulseCountPin2++;
}

void countPulsePin3() {
// This function is called whenever a pulse is detected on pin 3 (S)
pulseCountPin3++;
}
1 Like

What is the problem with the code ? It looks good for me...

Ty and I get the sentiment about ai, I'm more mechanical and I don't know much at all about programming and I was trying to get it to work with out bothering people

It's time to learn, may be?

The lcd was showing gph of over 600 with a little water pump and was even showing with nothing attached

What is the pump rated for?

No where near 600, it was out of a cat water fountain I had

Do you have a pulldown resistor on each of the sensor pins? 1k should be enough.

I was going off this spec sheet and if was reading it right it wanted at least 10k so that's what I had

I don't see pulldown resistors in your image, I see a resistor between the two white wires, which are not the same as pulldown resistors.

This math is not looks correct:

float pulsesPerSecondPin3 = (float)pulseCountPin3 / (float)interval;

//Convert microliters to gallons
gallonsPerHourPin3 = (pulsesPerSecondPin3 * microlitersPerPulse) / 1000.0 * 3600.0;

The first line returns pulses per millisecond rather than per second.
And the second line has nothing to do with gallons, it is a microliters in hour

Interval is 1000, so 1000 milliseconds = 1 second