Single pulse on digital output at the beggining of movement

Hello, I need to generate a single pulse of a variable duration at one digital output at the beggining of the movement. I have found that it is possible to generate a PWM signal, but a PWM is a continuos sequence of pulses, in my case I need to generate just one pulse per movement instruction. Each movement will have a different pulse duration.

Any proposal is appreciatted.

Kind regards

You’ll need to edit the source code, it’s going to be quite a bit of work, there’s no “out of the box” way of doing this.
Don’t hesitate to say if you need any help with that.


If you can get it to do a PWM of say 25% and trigger on your movement then maybe you can get it to output a specific pulse width( say 75% ) and then return back to 25% and use an Arduino to monitor this PWM and do the single pulse thing you desire.

Seems like a round-about way but if you already understand and have a way to do PWM the way you want but just need a way to sense the single PWM trigger, Arduino C coding is pretty straight forward.

Maybe …

Do something with the X end stop sensor.
Look for a transition from on to off as the carriage moves ??

Thank you Dougl, please correct me if I have not understood you correctly. I could set a PWM syncronized with the G movement, with the pulse duration I need and connect this PWM signal to an Arduino that it will replicate only the first pulse of the sequence of pulses. It will be necessary to send a Reset signal to the Arduino in order to prepare it for the next pulse, so at the end of the movement I will activate an output signal connected to the Arduino to reset it. I will have to take care with potential delays between the end of the PWM pulses generated during the movement and the Reset signal but it seems it will work and the coding of the Arduino will be quite simple.

Thank you Donkjr, I will appreciatte if you explain me a bit more about your proposal, I am new to Smoothie, I have been looking for the “X end stop sensor” and I do not have a clear idea of what it is.

Thank you Arthur, I have been looking around the documentation about a way to code this implementation. I have seen that it is possible to create modules (I have look at the example in the “What is a Module” web page), for example some code that it will be executed onto the “on_gcode_execute” function. Also I have seen that it is possible to call mBed functions. In the link to the mBed functions available in the Smoothiware web page I have found that it is possible to set a Timer and also to activate and deactivate digital outputs. Combining both functionalities I figure out that it could be possible to execute at the beggining of the G code a timer that it will drive a digital output. But I have not found an example of how implement mBed functions inside a Module and also what additional implications it could have. I would appreciate your help in order to understand how impliment these mBed functions inside the Module.

mBed is a library that is very old and we don’t use anymore, we transisionned to other HAL functions.
you have the right idea though with the notion of creating a module, and having it drive a pin. You can look at the switch module (for example, there are others) for ways to drive a pin.

how long does your pin need to stay high? that’s going to define exactly what you use to turn it off

I was thinking of a K40. After rereading I note that you did not disclose what machine you are working with, the conditions of the start of the movement, or what the pulse does.

I was trying to think of ways to do this outside of the smoothies code.

Most machines have some way of knowing the start of the vertical and horizontal coordinates. Usually with a switch (endstop). This switch could be monitored independently of the smoothie.

Unaware of the timing and synchronization needs, I was thinking the x movement could be detected as the carriage moves off the x endstop switch. From that point, a pulse can be created.

The vertical and horizontal end-stops could be monitored with a u-processor which syncs with the vertical and produces various pulse widths for each horizontal movement.

I have no idea if these vertical and horizontal switch outputs would be in sync with whatever the movement pulses are intended to do.

Arthur, I will need a pulse of around 1 ms at high level.

Donkjr, I am going to use an OpenBuilds CNC to apply a lubricant coating over different metal surfaces (different size and different amount of lubricant), the duration of the pulse is proportional to the amount of lubricant applied over the surface. I need to move the head of the CNC machine, where the nozzle is installed, applying pulses of spray at different positions over the metal surface (move to the start position, apply a pulse, move to a different position, apply another pulse and move again and so on) so I do not see how the end stops can be related with the different positions where I will apply the pulses and the duration of the pulses should be included in the code that specifies to movement to the different positions, I prefer not to have two programs with specific configurations data running into two separated machines. I consider to use an Arduino as Dougl has proposed because the program at the Arduino will be always the same program, the positions and the amount of lubricant (the duration of the pulse) will be only in the Smoothies program (the Gcode program).

1 Like

then you can get all the “example” code you need in the switch module: you’ll need the “slowticker” module to do the timing, and the Pin module to turn the pin on/off. if you can read and understand the switch module, you should have all you need to create your own module
don’t hesitate if you need any extra help

1 Like

Based on the usage you describe here, why not set up a switch module (like M106/M107, see docs) to control the lubricant thing, and just add M106 G4 P1 M107 before each move/each time you need some lubricant to be output? That would work with zero extra coding work, just need to modify your gcode files.

1 Like

Yup the endstop would not work, now that I understand the application better.

It would be nice if you could do this with Gcode.

Brainstorming …

I know that you can add a laser diode module to a CNC machine. This suggests that the carriage can be positioned and instead of a Z movement the laser is turned on/off.

I wonder can the laser module be replaced with a spray module that turns the sprayer nozzle on at the right position. Instead of laser on, it’s the spray head on!

I guess you would create an image in the driving software (g-code generator) that has a dot(s)/line at the right position in the x-y for the spray.

The spray module would convert the Laser PWM signal to a spray-on/off signal. Alternatively, you set the laser power to 100%.

If you wanted the spray to stay on a certain amount of the movement you would set a line in the source design.

If you wanted to compensate for the width of the spray you could set lines horizontally/vertically apart from each other in the source design.

1 Like

Arthur, I think you are right. Using the fan pin to drive the spray and specifying the duration using for example “G4 P1” to set a 1ms pulse will be fine. I understand that this solution only allows pulse duration of integer values, I mean 1, 2 or 3 ms, but not something like 2.5 ms. In case I would need some more time resolution could you please explain me what you mean with “Slowticker” module, I have been reading the Switch Module web page and I have not find any reference to “Slowticker”.

1 Like

Donkjr, thank you for your brain storming proposal. Yes, there is two ways to drive a spray nozzle, one is using single spots (my initial approach) and the other is applying a continuos spray modulated using a PWM signal (the nozzle only sprays when the PWM signal is high). For this second option, I am considering to use the same approach used to operate a laser with a PWM signal.

about slowticker, it’s a timer module the switch module uses. you’ll understand once you read the switch module source code

about the switch module itself, I expect it supports floating point values, you should test it out, it should work

1 Like

Now that we know the use-case, it moving XY motors to positions and needing a trig to activate a spray valve or pump I’m with Don that this is really a laser type operation even if used on a CNC machine without a spindle AND it’s a GCode issue. As Don pointed out, diode laser control is to move X and Y motors turning the diode laser 12V On or Off at the beginning of the motion and then changing the PWM signal based on GCode commands. This is how my Ortur/GRBL diode laser works and while Smoothieware is well used on CO2 lasers, it has to do mostly the same thing minus turning a 12V signal ON/OFF when the movement starts. ie it does laser control via a PWM value based on GCode commands.

So, if the laser output of Smoothieware is always a PWM signal once GCode processing starts it has to be at a consistent PWM pulse width(likely some min width) and when it received the S command.

M3 turns the laser on.
the following line moves to X76.402 to Y59.08 with laser power 50%(S0.5) and speed of 10mm/s(F600)
G1 X76.402 Y59.08 S0.5 F600
M5 turns the laser off

Again, if there is a steady low pulse width stream generated by smoothieware when the system is enabled, you can use something(like an Arduino) to look for this small PWM pulse and know the system is ON, then look for a PWM pulse width of 50% when you want to turn your sprayer on and do something on the first pulse of the S0.5 command. When the pulse changes back to minimum pulse width(M5) command then have the arduino turn off your sprayer or what ever.

You would now use any software which can generate gcode output for Smoothieware in laser mode . I just used LightBurn but any of the open source tools( Kiri:Moto(, etc ) could be used to set your spraying locations and the laser/spray power. Just use a dot in the drawing or lines, it should be your choice but lines will also move the sprayer if that’s your intent.

I was thinking that you just use the gcode to turn the sprayer on and off?
If you just want an on-off condition in the gcode set the power to 100% and their wont be a PWM cycle??

For example if you want to turn on the sprayer for 6" at a position 1" down the surface.
In the grcode generator (it can be any cad software) draw a line y= 1" down on the defined worksurface with the laser at 100%. Make that line x = 6" long. Run the job.

Unless the sprayer has to be adjustable I don’t see the need for messing with a PWM signal?

Thank you Dougl, the sequence of sentences you propose are fine to operate the system when I will apply lubricant contiuosly, regulating the dose based on the PWM pulse width. The idea to have a width pulse threshold to communicate to the Arduino the state of the system is also a good idea.

I am facing the first stage of a project to apply lubricant over a flat metal surface (only X, Y and Z movement), the next stage consits on applying lubricant over a 3D mold surface, where the nozzle has to points perpendicular to the surface along the movements, so I will need to implement two mechanical rotations on the head to change the orientation of the nozzle. Additionally I will need to develop a program to calculate automatically the movements (the gcode) and the dose of lubricant along the movements based on the 3D cad model of the mold and a specified amount of lubricant per unit of surface. The key point is to get an uniform distribution of the lubricant over the 3D surface.

Before starting to buy any hardware I am trying to assure that all the requirements can be achived using this board.

I am assuming that as this board supports up to 6 motors it will be able to implement without problems the two additional rotations that I will need at the second stage of the project…