Hello Guest it is January 21, 2022, 09:06:03 PM

Author Topic: Pokeys57CNC PWM outputs in Mach4  (Read 3038 times)

0 Members and 1 Guest are viewing this topic.

Pokeys57CNC PWM outputs in Mach4
« on: October 02, 2018, 03:02:50 PM »
I'm interested in using a DRO to control a PWM output in Mach4, but it seems to have limited functionality compared to Mach3.

Here's what it looks like in Mach3:

But in Mach4 I just have enable/disable and no options:

 Are these options hidden somewhere else, or are they not there anymore? Is there any other way to control a pwm output from gcode in Mach4? I'd like to be able to control pin 18 from gcode so that I can change the intensity of my laser, so sharing the spindle output is less desirable. Thanks
Re: Pokeys57CNC PWM outputs in Mach4
« Reply #1 on: October 04, 2018, 11:57:02 AM »
I got a response from PoLabs support that controlling those outputs needs to be done with custom code. I'm going to try it with a custom M-code that adjusts the registers. I'll post back here if it's successful.
Re: Pokeys57CNC PWM outputs in Mach4
« Reply #2 on: October 12, 2018, 04:42:38 PM »
I was able to control it with a custom macro, BUT... it's slow... too slow to be switched during a program and not coordinated with the machine's movement. I went in a totally different direction and now I'm controlling the On/Off of the laser using the B axis DIR pin with the intensity controlled by my custom macro. It would be sweet if there was some way to synchronize the PWM signal with the machine movement, but it doesn't seem possible with PoKeys. If anyone has an alternative approach I'd love to hear it.
Re: Pokeys57CNC PWM outputs in Mach4
« Reply #3 on: October 12, 2018, 05:49:42 PM »
have you given any thought to a Frequency-to-Voltage converter, there are such ICs, and tend to be used in instrumentation and communication applications.
They work on the idea that if you present a square wave signal of 1kHz say it might produce 1V whereas if you present it with a 2kHz signal it will output 2V.

The reason I make the suggestion is that the signal (excluding the acceleration ramp up and de-acceleration ramp down) are variable frequency signals.
Thus if your B axis has a tuning of 1000 steps per unit then and a max velocity of 60 units per minute:

G1 B1000 F60 would for the duration of the move (excluding ramp up/ramp down) of 60 X 1000 =60,000 pulses per minute or 1Khz.
G1 B1000 F30 would cause a pulse rate of 500Hz.

If you had a V-to-F converter IC on your B axis output you would have a variable voltage set by the axis feedrate.

Note that this strategy doesn't require any special support from your controller....it just outputs what it believes are a simple pulse stream indentical to
that required to drive a stepper for instance.

If memory serves I believe it might be the C25 or maybe the C62 BoBs which use this idea to generate an analogue voltage for a VFD rather than the more traditional

My wife left with my best friend...
     and I miss him!
Re: Pokeys57CNC PWM outputs in Mach4
« Reply #4 on: October 12, 2018, 07:21:44 PM »
Very interesting... I had not thought of that, but it seems like a good option. The laser expects a PWM signal instead of analog, but I'm sure there's a way to output that with a similar approach. Now you've got me wanting to try the step signal as a crude PWM output. I'm guessing that would limit me to 50% duty cycle, but at the moment the laser is a bit too powerful so that might work.
Re: Pokeys57CNC PWM outputs in Mach4
« Reply #5 on: October 12, 2018, 09:30:22 PM »
you say the laser expects PWM not analogue. When PWM  is passed through a lowpass filter it becomes an analogue voltage.
Thus it is my expectation that the laser ACCEPTS PMW and uses that to generate an analogue voltage to do its work.

May I suggest an experiment. If your laser is expecting a 5V PWM signal then using a battery or small DC power supply
and a potentiometer generate a varying 0-5V signal.

If you are dead set on using PWM is the spindle speed output. I assume your machine does not have a spindle? If so the PWM signal which
is normally used to control the speed of a VFD would be available for use. Have a look at the spindle API's is API.chm.
This is an example and may be useful:
LUA Syntax:
rc = mc.mcSpindleSetCommandRPM(
      number mInst,
      number RPM)

Set the commanded RPM for the spindle.

My wife left with my best friend...
     and I miss him!
Re: Pokeys57CNC PWM outputs in Mach4
« Reply #6 on: October 15, 2018, 12:09:58 PM »
I just tested the laser with an analog input and it stays off until over 2 volts and then turns on full. So I think the PWM is actually used to pulse the laser diode.

I also have a spindle and could share the PWM signal from there, but I can't find a way to coordinate spindle output with movement (any macro stops the machine momentarily)

But... I've got another idea: I'm doing to build a custom axis with a microcontroller that uses the step/dir signals to increase/decrease the PWM duty. So G1 B0 would be off, and G1 B0.255 would be full power. I'll report back with the results.

Re: Pokeys57CNC PWM outputs in Mach4
« Reply #7 on: October 15, 2018, 02:17:09 PM »
you could also generate PWM from an analogue voltage, couple of transistors and an op amp.

My wife left with my best friend...
     and I miss him!
Re: Pokeys57CNC PWM outputs in Mach4
« Reply #8 on: October 16, 2018, 11:35:32 AM »
The custom driver worked perfectly. The laser power is now the B axis. B0 is off and B0.0255 is full power. I used a arduino nano to build the driver. The code is very simple:

Code: [Select]
const byte laserPin = 3;
const byte stepPin = 2;
const byte dirPin = 4;
volatile int brightness = 0;
volatile int dir = 0;

void setup() {
  pinMode(laserPin, OUTPUT);
  pinMode(stepPin, INPUT);
  pinMode(dirPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(stepPin), step, RISING);

void loop() {
  analogWrite(laserPin, brightness);

void step() {
  dir = digitalRead(dirPin);
  if (dir == LOW) {
    if (brightness < 255) brightness++;
  else if (brightness > 0) brightness--;   

I ran a few jobs last night that were a couple hours long and it didn't lose a single step... but if you needed to re-zero the axis for a missed step you can just drive it to B1 and back to 0 since it ignores steps beyond the limits. The machine changes speed ever so slightly while changing the power since it's still doing coordinated motions, but it's way better than the spindle commands where it would literally stop for a half second and burn a hole.
Re: Pokeys57CNC PWM outputs in Mach4
« Reply #9 on: May 22, 2020, 04:47:40 PM »
Watch here: https://www.youtube.com/watch?v=P_vKWz55DkU
I've made a video how to control a Servo Motor with PWM ;-) You need to edit your screenset
regards cncprint