# Pulse Counter

Hello everyone, I am a mechanical engineering student working on a project that involves an ultrasonic sensor that is used to detect the amount of gas that flows through an enclosed tube. After a certain volume of gas has flowed through the tube, a pulse is emitted and the number of pulses emitted is kept track of.

i.e: 1 pulse = 10 cubic feet of gas; if the pulse counter is at 6 then ~60 cubic ft of gas has been consumed

Unfortunately, I had zero prior knowledge of Arduino programming before this project and am trying to learn what I can in the limited amount of time I have. I’ve looked through a few example sketches surrounding pulse counters but they are a bit confusing(at least to me). I’m not asking for someone to do the sketch for me but if you could point me in the right direction I would grateful.

Background information:

• Our team has decided to use HC-SR04 ultrasonic sensors.
• Arduino UNO or MEGA microcontroller

My attempt at pseudo-code:

// Q = volume of gas used
// T = time
// V = velocity of gas(constant)
// A = cross-sectional area of tube(constant)

Q = V * A * T //as time elapses, Q value will increase

P = 0 //pulse counter starts at 0

if Q >= 10

P = P + 1

T = 0 //resets time and Q to 0

end

Hopefully my pseudo-code is understandable and reasonable. If you have a better suggestion, please feel free to let me know! Until then, I’ll keep on researching.

• Mike

We generally suggest that you buy an Arduino and work your way through the simple examples provided with the IDE (software development package) to learn the language and special features of the Arduino.

The HC-SR04 measures distances to a reflecting object. It is hard to imagine how it could be used to measure gas flow.

Strykk:
Hello everyone, I am a mechanical engineering student working on a project that involves an ultrasonic sensor that is used to detect the amount of gas that flows through an enclosed tube. After a certain volume of gas has flowed through the tube, a pulse is emitted and the number of pulses emitted is kept track of.

When I read your description and look at your pseudo code I find I have no idea whether you are trying to create a program to generate the pulses or to count the pulses that are generated by something else?

Try again with the description

...R

It does rather look like the objective is to build a system to deliver pulses based on the volume of gas that has flowed through the tube. Presumably some other device will receive and process those pulses.

As has already been said, it is difficult to see how an ultrasonic sensor could provide useful data here. Mouser has a selection of gas flow sensors which may be more suited to this application:

Here’s an idea for a 30 milliSecond pulse generator triggered by a counter, if you need a pulse shorter than 2 mS you should use micros() instead of millis().

``````unsigned long tStart, pStart;
byte pulseLength = 30, cnt;
unsigned int pulses;
const byte pulsePin = 13;
void setup()
{
Serial.begin(9600);
pinMode(pulsePin,OUTPUT);
}

void loop()
{
if(millis() - tStart >= 1000)
{
tStart = millis();
cnt++;
Serial.println(cnt);
}
if(cnt >= 10)
{
cnt = 0;
pulses++;
pStart = millis();
Serial.print("pulses =  ");
Serial.println(pulses);
}
digitalWrite(pulsePin,millis() - pStart < pulseLength);
}
``````