Flow Sensor - Trigger output pin based on input pulses per sec

I’m in the progress of building my self a 40w laser. The Smoothieboard has performed well so far.

My question is if there is any software support for having a output pin set high or low depending on the number of pulses detected per second on an input pin? - I have bought a Flow sensor (not a flow switch) that measures the current flow rate of the water and reports it back in a specific number of pulses per gallon of flow.

The laser PSU has a input that needs to be pulled low for the laser to turn off. The recommendation is to have a flow SWITCH connected to this pin that pulls the pin low when there is a flow. However, the cheap flow switches that is a on/off switch have their mechanical issues where you need a certain amount of flow before the spring is pressed enough and the small amount of flow that is needed by the laser is not enough to trigger the flow switch.

By using a flow meter instead of a flow switch, the threshold value for the amount of flow desired can be handled in the software instead of in the spring tension of the flow switch.

I do have some plans on handling this in a ATTINY85, but doing this in the smoothieboard would be even better.

Thanks

Imported from wikidot

So I’m bad at electronics but it sounds to me like just wiring a large capacitor in parallel with the signal would essentially do what you want ( maybe a capacitor and a transistor ). I’m probably wrong though, so ask somebody competent :slight_smile:

About having Smoothie do the detecting, that’s not supported. It wouldn’t be too hard to add, but at the moment there just isn’t any code for that.

Cheers.

Maybe a capacitor would work to smooth out the signal so that the Laser PSU thinks that its a static logic level… However, if it can be incorporated into the Smoothieboard then i suppose that it also could trigger a stop or pause so that smoothie stops the cut if the water flow stops.

Regarding the code. I have written something general that would work in the arduino world. This code is not in any way connected/specific to a flow sensor. It only count pulses and if the number of pulses per second is below or above a threshold value then it will toggle the output pin.

// Declare Input/Output Pins
int sensorPin = 2;    //The pin location of the sensor
int outPin = 3; // The output pin

// Configurable variables
int pulsesExpected = 10; // Pulses expected during the last second to toggle the output

// Static variables
unsigned long oldTime;
int pulsesDetected;

void sensorInterrupt () //This is the function that the interupt calls
{
pulsesDetected++;
}

void setup() //
{
pinMode(sensorPin, INPUT); //initializes digital pin 2 as an input initialised,
attachInterrupt(0, sensorInterrupt, RISING); //and the interrupt is attached
}

void loop ()
{
if((millis() - oldTime) > 1000) // Only process counters once per second
{
detachInterrupt(0); // Disable interrupt while checking

    if (pulsesDetected > pulsesExpected)
     {
         digitalWrite(outPin, LOW); // If measured flow is above desired threshold, pull signal low
     }
    else
     {
         digitalWrite(outPin, HIGH); // If measured flow is below desired threshold, pull signal high
     }

    oldTime = millis(); // Reset time counter
    pulsesDetected = 0; // Reset pulse counter
    attachInterrupt(0, sensorInterrupt, RISING); // Enable Interrupt
}

}


I have no idea how the smoothie code is built. Is this something that can be incorporated? If it can, can the variables then be controlled thru the config file? I’m thinking about the variables for input pin, output pin, startup state, output_on_command, output_off_command etc.

It would be nice if this would be similar to the “switch” module with the addition that the threshold number of pulses per sec can be configured as well thru the config file.

What do you think?

Yep essentially what you’d want to do here, is modify the switch module to add this as an input method.

You can take a look here : https://github.com/Smoothieware/Smoothieware/blob/edge/src/modules/tools/switch/Switch.cpp and see if you understand how it works. If you do, modifying it should be fairly easy.

Thanks for the link. I hade a quick look on the code. If i understand it correctly then there are a couple of places that needs to be updated.

  1. Come up with a new name for the pin_behavior and define it like this
#define   input_pps_checksum           CHECKSUM("input_pps")
  1. Add a row that imports the PPS (Pulse per Second) threshold setting from config file
this->input_pps = THEKERNEL->config->value(switch_checksum, this->name_checksum, input_pps_checksum )->by_default("")->as_string();
  1. Add the logic to this part.
// TODO Make this use InterruptIn
// Check the state of the button and act accordingly
uint32_t Switch::pinpoll_tick(uint32_t dummy)
{
    if(!input_pin.connected()) return 0;
// If pin changed
bool current_state = this->input_pin.get();
if(this->input_pin_state != current_state) {
    this->input_pin_state = current_state;
    // If pin high
    if( this->input_pin_state ) {
        // if switch is a toggle switch
        if( this->input_pin_behavior == toggle_checksum ) {
            this->flip();
            // else default is momentary
        } else {
            this->flip();
        }
        // else if button released
    } else {
        // if switch is momentary
        if( this->input_pin_behavior == momentary_checksum ) {
            this->flip();
        }
    }
}
return 0;

}


So, what i could do is to add an if statement that checks if the input_pin_behavior == pulse and then enters the if statement and does updates the output and a variable with the timestamp.

However, if no pulse is detected within a specific time frame (for example 1 second) then the output should be switched to off state. I suppose that the code for monitoring the time stamp of the variable should be added to a loop or timer interrupt somewhere.

Should this loop or timer interrupt be added to switch.cpp or is there another location in which i can add it? My idea was to check the time stamp on the variable set in the IF statement and then if the timestamp is “older” then it should be based on the desired pulses per second then the loop/timer will clear the variable and switch the output.

Hey.

First, you want to avoid acronyms in config values as much as possible, just call it input_pulses_per_second

Then, it sounds like you have the right idea.

pinpoll_tick is called on a regular basis ( 100 times a second ), so it’s all you should need