Electron outputing random data on Serial Monitor?

I have an electron that I have connected to a water flow hall effect sensor.

I’m not sure what’s going on, because even when nothing is connected to the board (other than USB and Antenna) I get output out of the serial monitor.

I wanted to play with sleep/wake so I’m playing with pressing a button to force the device to wakeup from sleep or go to sleep.

Attached is the code, as well as sample output from Serial Monitor. Any ideas?

From serial Monitor

Flow rate: 39.8L/min  Total Liquid 556938mL/Sec
Continuous seconds of flow:  840
This is the wakeup pin value: 1
Flow rate: 39.8L/min  Total Liquid 557602mL/Sec
Continuous seconds of flow:  841
This is the wakeup pin value: 1
const int statusLed        = D7;
const int sensorPin        = D1;
const int sensorInterrupt  = D1;
const int wakeup           = D3;

float calibrationFactor = 4.5;

volatile byte pulseCount;

float flowRate;
unsigned int flowMilliLitres;
unsigned int TotalMilliLitres;
bool flag;
bool leakdetected;
int buttonstate;
int contflow; //number of seconds that flow has been more than 0
unsigned long oldTime;

void setup()
{

  // Initialize a serial connection for reporting values to the host
  Serial.begin(9600);

  // Set up the status LED line as an output
  pinMode(statusLed, OUTPUT);
  digitalWrite(statusLed, LOW);

  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, LOW);

  pinMode(wakeup, INPUT);
  digitalWrite(wakeup, LOW);


  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  TotalMilliLitres  = 0;
  oldTime           = 0;
  contflow          = 0;
  flag              = false;
  leakdetected      = false;
  buttonstate       = 0;

attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}

/**
 * Main program loop
 */
void loop()
{
   if((millis() - oldTime) > 1000)    // Only process counters once per second
    {
    unsigned int frac;

    detachInterrupt(sensorInterrupt);

    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
    flowMilliLitres = (flowRate / 60) * 1000;
    TotalMilliLitres += flowMilliLitres;

    oldTime = millis();

    if(pulseCount!=0)
    {
      Serial.print("Flow rate: ");
      Serial.print(int(flowRate));  // Print the integer part of the variable
      Serial.print(".");

      frac = (flowRate - int(flowRate)) * 10;
      Serial.print(frac, DEC) ;      // Print the fractional part of the variable
      Serial.print("L/min");

      // Print the number of litres flowed in this second
      Serial.print("  Total Liquid ");             // Output separator
      Serial.print(TotalMilliLitres);
      Serial.print("mL/Sec");
      Serial.println();

      //Print how long we've seen liquid
      Serial.print("Continuous seconds of flow:  ");
      Serial.print(contflow);
      Serial.println();
      contflow=contflow+1;


      buttonstate = digitalRead(wakeup);
      Serial.print("This is the wakeup pin value: ");
      Serial.println(buttonstate);
      digitalWrite(statusLed, HIGH);
     }

    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

  }



void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;

}

So, is the serial output you show with or without the pulse counter? You should not get any output with nothing connected, since all the print statements are inside the if(pulseCount!=0) clause, and pulseCount = 0.

Exactly, that's why I'm confused. I don't have anything connected to the board, so pulsecount should be zero.

I just pasted the latest from the serial output, you can see continuous flow is still incrementing.

Flow rate: 39.8L/min Total Liquid 2950299mL/Sec
Continuous seconds of flow: 4446
This is the wakeup pin value: 1
Flow rate: 39.8L/min Total Liquid 2950963mL/Sec
Continuous seconds of flow: 4447
This is the wakeup pin value: 1

I tried your code, and I got no output at all for 5 or 10 minutes, but now it is, so I think it's a floating pin problem. Try setting the sensorInterrupt pin to INPUT_PULLUP or INPUT_PULLDOWN depending on how your pulse counter works (if it goes to ground when it counts, then you need INPUT_PULLUP, otherwise, INPUT_PULLDOWN).

1 Like

I tried both, and still get random output.

The interesting thing is, when I reset the board, there is no output, however even if I attach an empty wire to D1 (not connected to anything on the other end), the output starts spewing data.

When I set it to INPUT_PULLDOWN, I'm not getting any output (at least not yet, after 12 minutes or so). You should also remove the line after that, where you do digitalWrite(sensorPin, LOW); I don't know if that could cause a problem, but, in any case, it's not necessary.

Hmmm...so I did that, no output. But when I connect the data pin from my water flow sensor, I start getting the output again.

// Initialize a serial connection for reporting values to the host
Serial.begin(9600);

// Set up the status LED line as an output
pinMode(statusLed, OUTPUT);
digitalWrite(statusLed, LOW);

pinMode(sensorPin, INPUT_PULLDOWN);

pinMode(wakeup, INPUT);

Isn't that what you expect, that you'll get data when you connect the sensor? Do you mean you don't get the data you expect?

1 Like

I mean I just touch the wire to pin D1 and data starts spewing. No data should be flowing as the water sensor isn’t actually moving.

Once I remove the wire completely from the bread board, the data continues. I left it overnight and it’s still going this morning.

You mean just a wire that's not connected to anything else, or a wire attached to your flow sensor? I haven't been able to duplicate this when I run your code on a Photon. If I have D1 set to INPUT_PULLDOWN, I don't get any data output even if I attach a wire to D1.

Once pulseCount is not zero any longer, the data should keep coming even if pulseCount just stays at 1, so this is what I would expect to see from your code (the flow rate shouldn't be changing, but the total liquid and continuous seconds of flow will keep incrementing).

1 Like