Measuring Pulse(Hall effect) while using other sensors for solar pump system

Hi Everyone,
I'm fairly new to Arduino and here for any help.

I'm trying to measure the flowrate, current and voltage and logging data to SD card while using RTC module for a solar pump project I'm working on.

I was successful in measuring both the current and voltage but I'm getting inconsistent numbers(sometimes very high and zeros) for my flow. The flow rate I'm trying to measure is between 1.5 gpm - 3 gpm.

I'm suspecting it might have something to do with some interference with interrupt pin since I'm using other sensors, or may be my code. Any help will be very helpful.
I compiled codes from different sources and Looks like this...Any help will be appreciated. Thank you:)

// ------ current and voltage code ----------
int val11;
float val2;


#define VIN A1 // define the Arduino pin A0 as voltage input (V in)
const float VCC   = 5.0;// supply voltage is from 4.5 to 5.5V. Normally 5V.
const int model = 1;   // enter the model number (see below)

float cutOffLimit = 1.01;// set the current which below that value, doesn't matter. Or set 0.5

float sensitivity[] ={
          0.185,// for ACS712ELCTR-05B-T
          0.100,// for ACS712ELCTR-20A-T
          0.066// for ACS712ELCTR-30A-T
     
         };


const float QOV =   0.5 * VCC;// set quiescent Output voltage of 0.5V
float voltage;// internal variable for voltage


//----Datalogger file ---------------------------------


// ----time code start - ------------------------
#include <Wire.h>
#include <DS3231.h>
RTClib myRTC;



// ---- time code end -----------------------------

//-----------flow sensor code start ---------------------


/*
YF‐ S201 Water Flow Sensor
Water Flow Sensor output processed to read in litres/hour
Adaptation Courtesy: www.hobbytronics.co.uk
*/

volatile int flow_frequency; // Measures flow sensor pulsesunsigned

double l_hour; // Calculated litres/hour
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;

void flow () // Interrupt function

{
   flow_frequency++;
}

//-----------flow sensor code end ------------------------

/*
  SD card datalogger

  This example shows how to log data from three analog sensors
  to an SD card using the SD library.

  The circuit:
   analog sensors on analog ins 0, 1, and 2
   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

  created  24 Nov 2010
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

*/

#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;





// Written by John Boxall from http://tronixstuff.com
#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val){
  return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val){
  return( (val/16*10) + (val%16) );
}
void setup(){

  //--------flow codes start -----------------

   pinMode(flowsensor, INPUT);
   digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
   Serial.begin(9600);
   attachInterrupt(0, flow, RISING); // Setup Interrupt
   sei(); // Enable interrupts
   currentTime = millis();
   cloopTime = currentTime;


  //----------flow codes end ----------------

  Wire.begin();
  Serial.begin(9600);
  // set the initial time here:
  // DS3231 seconds, minutes, hours, day, date, month, year

    // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
}




//


// main loop

void loop(){
 // ----- current and voltage code start ---------------------

 
    //voltage sensor codes
    float temp;
    val11=analogRead(0);
    temp=val11/4.092;
    //val11=(float)temp;//
    val2=(temp*0.1);
   
    //current sensor codes

     
  float voltage_raw =   (5.0 / 1023.0)* analogRead(VIN);// Read the voltage from sensor
  voltage =  voltage_raw - QOV + 0.012 ;// 0.000 is a value to make voltage zero when there is no current
  float current = voltage / sensitivity[model];


 // ----- current and voltage code end ---------------------

  //delay(1000); // every second
   // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.csv", FILE_WRITE);



  // if the file is available, write to it:
  if (dataFile) {
    DateTime now = myRTC.now();
   
    dataFile.print(now.year(), DEC);
    dataFile.print('/');
    dataFile.print(now.month(), DEC);
    dataFile.print('/');
    dataFile.print(now.day(), DEC);
    dataFile.print(',');
    dataFile.print(now.hour(), DEC);
    dataFile.print(':');
    dataFile.print(now.minute(), DEC);
    dataFile.print(':');
    dataFile.print(now.second(), DEC);
   
 


  //end write time data
//---- start write current and voltage data
    //dataFile.print("Current: ");
    dataFile.print(",");
    dataFile.print(current,1);
    Serial.print(",");
    Serial.print(current,1);  
    Serial.print(",");
    dataFile.print(", ");
    //dataFile.print("Voltage: ");
    dataFile.print(val2);
    Serial.print(val2);  
    Serial.print(",");  
    dataFile.print(", ");

//---- end write current and voltage data

  //----------start write flow sensor data --------
   currentTime = millis();
   // Every second, calculate and print litres/hour
   if(currentTime >= (cloopTime + 1000))
   {
      cloopTime = currentTime; // Updates cloopTime
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
      l_hour = ((flow_frequency  / 5.5 )* (0.264 *0.04*0.08 )); // (Pulse frequency ) / 7.5Q = flowrate in L/hour
      flow_frequency = 0; // Reset Counter
      dataFile.print(l_hour, 4); // Print litres/hour
      Serial.print(l_hour, 4);
      //Serial.println(" L/hour");
      //Serial.println("");
   }

//---------- end write flow sensor data ----------




//------- end write to sd with new line

    dataFile.println("");
    Serial.println("");

//--------
    dataFile.close();
    // print to the serial port too:
    //Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }delay(1000);
}

Because the variable

flow_frequency

is 2 bytes it could change between reading or writing to it.
To prevent this you should disable the interrupts before you read or write to it in normal code, and then re-enable them after you have done accessing it.

1 Like

Thanks for your comment! It definitely makes sense. I'm quite sure how I can do that though. Any advice?

Do you mean to say you are NOT quite sure how to do this?

To disable interrupts see:-
Disable interrupts

To enable interrupts see:-
Enable interrupts

1 Like

Thank you!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.