Taking PWM input and giving the logic level output

The sensor can stop in either state, output high or output low, you should only care about the time when transitions happen, nothing else.

MarkT: The sensor can stop in either state, output high or output low, you should only care about the time when transitions happen, nothing else.

How am I going to take care about time ?

TomGeorge:
I worry about datasheets that don’t know the difference between frequency and pwm.
You meter outputs pulses, the FREQUENCY of the pulses indicates flow rate.
So you need your sketch to measure frequency to give you a flow rate.

I hope the OP now understands why I suggested that he post a link to the datasheet, and why, prior to seeing it, everyone has been wasting time.

I assume now that the facts have been revealed he will have no trouble figuring out a solution - just count the number of pulses during a second.

…R

saifkazi: How am I going to take care about time ?

By measuring it, of course.

One of the more common functions for a microcontroller.

OK, the datasheet is confusing as it refers to "Hz" when it actually means "pulses per second" and the stupid datasheet fails to explicitly detail the most important parameter which is 492 pulses per litre. Now it quotes 10 litres per minute as corresponding to 82 pps, and a maximum of 30 litres per minute which would be 246 pps. You count this easily by polling the input in your main loop and incrementing a count each time the input goes high and then low again.

You also poll for the millis() count and decide what your measuring interval is - perhaps a second, or perhaps a minute - so that you determine when the millis() value has increased from the value at the start of your count, by the specified interval.

Note - this is a purely digital concern - it has absolutely nothing to do with PWM or analog functions.

TomGeorge: When do you want the pump to turn On and Off, at a particular volume or flow rate?

It's simple when there is liquid flowing I want the motor to turn on and when the liquid stop flowing I want the motor turn off

So simple thing I can do here is take pulses from flow meter as interrupt and can trigger the pin . so I have to count that pulses against time am I correct? But I am not able to get the sketch in my head?

saifkazi: But I am not able to get the sketch in my head?

The simplest way is probably to create in Interupt Service Routine (ISR) that increments a variable for every pulse.

Then once every second get the value of the count and compare it to the previous value

The ISR can be as simple as

( before setup() )

volatile unsigned long pulseCount;

( in setup() )

attachInterrupt(0, pulseCounter, RISING);

and the ISR code

void pulseCounter() {
   pulseCount ++;
}

Then, in your main code, every second (or whatever time you want)

noInterrupts();
  newCount = pulseCount;
interrupts;
countDifference = newCount - prevCount;
prevCount = newCount;

...R

Robin2: The simplest way is probably to create in Interupt Service Routine (ISR) that increments a variable for every pulse.

Then once every second get the value of the count and compare it to the previous value

The ISR can be as simple as

( before setup() )

volatile unsigned long pulseCount;

( in setup() )

attachInterrupt(0, pulseCounter, RISING);

and the ISR code

void pulseCounter() {
   pulseCount ++;
}

Then, in your main code, every second (or whatever time you want)

noInterrupts();
  newCount = pulseCount;
interrupts;
countDifference = newCount - prevCount;
prevCount = newCount;

...R

Ohh thank you very much that will help me for sure. What if I want to set threshold point ?

Robin2: Then, in your main code, every second (or whatever time you want)

noInterrupts();
  newCount = pulseCount;
interrupts;
countDifference = newCount - prevCount;
prevCount = newCount;

I don't get this codes can u plus explain me more about this?

saifkazi: I don't get this codes can u plus explain me more about this?

The variable pulseCount is regularly incremented as the pulses happen and you don't want to interfere with that.

When you want to use the value just take a copy of it and store it in newCount

The interrupts() and noInterrupts() temporarily stop the interrupts while the copy is being made in case the value changes during that time - remember that an unsigned long is 4 bytes so it cannot all be read at once.

Assuming you get the value of pulseCount every second then the value in countDifference will be the number of counts during the most recent second.

Does that help?

What if I want to set threshold point ?

Please explain further

...R

@Paul__B I assume you have worked out those 492pulses/L from the datasheet. Doesn't that mean 984 logic level changes per liter.

Not a good programmer, but I would try using a pinchange interrupt. And measure the time inbetween interrupts with millis. Maybe someone can write a few short lines for this guy, to get him started. Leo..

edit Just saw this has already been done in the other post from this guy. G_ crossposting wastes people's time. http://forum.arduino.cc/index.php?topic=333818.15

Wawa: I assume you have worked out those 492pulses/L from the datasheet. Doesn't that mean 984 logic level changes per liter.

I did. (Eventually - it was not immediately obvious as you will observe.)

It does, but maximum is quoted at 500 ml per second.

Wawa: Not a good programmer, but I would try using a pinchange interrupt. And measure the time in between interrupts with millis.

And what would be the benefit of using an interrupt and increasing complexity of the code?

Please do not try and tell me that there is any difficulty with counting 492 events per second with an Arduino. Or indeed 492.

Robin2: The simplest way is probably to create in Interupt Service Routine (ISR) that increments a variable for every pulse.

Why?

At a specified maximum of 492 transitions per second, polling will be vastly simpler.


Actually, I may take that back somewhat.

If you use interrupts, you can use delay() instead of bothering about millis(), so I suppose that would be somewhat simpler. :grinning:

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

  • Moderator

@Paul__B
I was more thinking about minimum flow.
Datasheet states 8hz at 1L/min.
I don’t know if OP wants the pump to turn on with a much smaller flow, e.g. < 0.1L/min.
And how fast the reaction time has to be.
Detecting logic level changes would be twice as fast/precise.
Something that might not be needed.

I would like to know the purpose of this project.
I am guessing this is to raise the pressure in a low pressure hot water system, only when hot water is used.
Here in New Zealand a lot of hot water cylinders are low pressure.
A lower hot water pressure can give problems with showers and mixing taps.
Leo…

Wawa:
I was more thinking about minimum flow.
Datasheet states 8hz at 1L/min.
I don’t know if OP wants the pump to turn on with a much smaller flow, e.g. < 0.1L/min.
And how fast the reaction time has to be.
Detecting logic level changes would be twice as fast/precise.

Quite so. And all this leads to the other considerations for the sketch.

The code presumably would need hysteresis, both in the time domain and the pulse (transition) rate.

There presumably is a threshold for just how much flow is of significance; you presumably do not want the pump to start unnecessarily, and it is almost certainly undesirable for it to “surge” on and off rapidly, so you have to implement a minimum “on” and “off” duration and determine how all these factors should interreact as the pump starting and stopping affects the flow being sensed.

Wawa:
Here in New Zealand a lot of hot water cylinders are low pressure.
A lower hot water pressure can give problems with showers and mixing taps.

Sure can!

Wawa: Why do you want a low pass filter. Don't. This is a digital sensor.

They want(ed) to as I suggested it... This was done as they said the sensor gave proportional output as PWM, so instead of having to go through all the pain and misery of learning how to measure duty cycles from a signal of indeterminate initial phase and potentially wavering carrier frequency, I thought just low pass filtering the PWM directly then ADC then threshold would be the shortest/simplest route to success. Ah, but what did I know ::)

Well, of course, it wasn't PWM. :roll_eyes:

Just pulses needing to be counted.

Just a version of the "XY Problem".

Rules of the game:

  • Query the real problem.
  • Mandate the datasheet.

No datasheet? Sorry, no answers.

I hope the OP realises this...

Much of the thread now only serves to confuse readers.

1:1: I hope the OP realises this...

Wherever he has gone to ...