Interrupts problem with water flow sensor

I trying to write the coding for DHT11, rain sensor, and water flow sensor. Before I combine water flow sensor coding with DHT11 and rain sensor coding, the serial monitor prints the corrects output. After I combine the coding all together then the serial monitor prints the wrong output which is the water flow sensor not working but there will have value run on the serial monitor.

Anyone can help me to figure out the issue and help me arrange the coding?

~~~~~~~~~~~~~~~~~~~Coding b4 add in water flow sensor~~~~~~~~(looks fine).

#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DHT.h>

char auth[] = "xxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxx";
char pass[] = "xxx";

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

const int sensorMin = 0;     // sensor minimum
const int sensorMax = 1024;  // sensor maximum
#define DHTPIN 4         // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11

BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);

void setup()
  // Debug console

  // Set ESP8266 baud rate
  Blynk.begin(auth, wifi, ssid, pass);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "", 80);

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);

void loop()
  // read the sensor on analog A0:
  int sensorReading = analogRead(A0);
  // map the sensor range (four options):
  // ex: 'long int map(long int, long int, long int, long int, long int)'
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
  // range value:
  switch (range) {
  case 0:    // Sensor getting more wet
  case 1:    // Sensor getting wet
    Serial.println("Rain Warning");
  case 2:    // Sensor dry - To shut this up delete the " Serial.println("Not Raining"); " below.
    Serial.println("Not Raining");
  delay(1000);  // delay between reads;;


~~~~~~~~~~~~~~~~~~~~~~~~~this is the water flow sensor coding~~~~~~~~~~~~~~~~

int statusLed    = 13; // byte stores an 8-bit unsigned number, from 0 to 255.
int sensorInterrupt = 0;  // 0 = digital pin 2
int sensorPin       = 2;

// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
float calibrationFactor = 4.5;

volatile byte pulseCount;  

float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;

unsigned long oldTime;

void setup()
  // Initialize a serial connection for reporting values to the host
  // Set up the status LED line as an output
  pinMode(statusLed, OUTPUT);
  digitalWrite(statusLed, HIGH);  // We have an active-low LED attached
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitres  = 0;
  oldTime           = 0;

  // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

 * Main program loop
void loop()
   if((millis() - oldTime) > 1000)    // Only process counters once per second
    // Disable the interrupt while calculating flow rate and sending the value to
    // the host
    // 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() - oldTime)) * pulseCount) / calibrationFactor;
    // Note the time this processing pass was executed. Note that because we've
    // disabled interrupts the millis() function won't actually be incrementing right
    // at this point, but it will still return the value it was set to just before
    // interrupts went away.
    oldTime = 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;
    unsigned int frac;
    // 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("\t");       // Print tab space

    // Print the cumulative total of litres flowed since starting
    Serial.print("Output Liquid Quantity: ");        
    Serial.print("\t");       // Print tab space
    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;
    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

Insterrupt Service Routine
void pulseCounter()
  // Increment the pulse counter
SoftwareSerial EspSerial(2, 3); // RX, TX

int sensorPin       = 2;

How many things do you have going on pin 2?

@cattledog wait i try shift the one (2,3) to (0,1). Then run again and feedback to you as soon as possible.

@cattledog If i used tx as pin 2 and rx as pin 3 then whr my attach interrupt pin can be placed?

Interrupts may be attached to any GPIO pin, except GPIO16.

I using cytrom esp8266 wifi shield. So any idea about it?

If the shield is connected to a Uno beneath and the interrupt code is running on the Uno and not the esp8266 then you should probably use 2 or 3 as the interrupt pin, and move the communication between the uno and esp8266 to different pins.

Then whr the pins can i move to? For software serial...

I think that with the UNO, you can use any two pins for software serial.

@cattledog Can list down the pins?
How about any suggestions from other ppl?

cytron esp8266 wifi shield
pins can i move to? For software serial...

This is what I see on the cytron website

  • UART/Serial communication pins can be configured/selected via mini jumpers:
    • USB - Direct to Arduino USB to Serial pin for flashing ESP8266 directly
    • D0/D1 - Hardware Serial pin of Arduino UNO and CT-UNO
    • D2, D3, D8, D9, D10, D11, D12, D13 - Software serial pins

I'd stay away from 13 because of the led, but there are jumpers to select D8,D9,D10,D11, or D12.

Hi i try using pin 10 and 11 , it works!!!!! Thanks a lot, now i trying to combine it with water flow sensor on my arduino uno pin 2 and feedback to you asap

@kyle_gee, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with your project. See About the Installation & Troubleshooting category.

Ok, noted!

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