Skip to main content

Mach4 Advanced M-Code Reference

Chapter 1 : Advanced Macros

M-Code Descriptions

M19 - Spindle Orient

Command Spindle Orient with auto or manual cycle based on parameters

Parameters

  • S
    • Angle in degrees of the orientation

M5 or a stop spindle command will cancel the spindle orient

M34 - (Electronically) Gear Two Motors

Achieve electronic gearing between a master motor (Q) and a slave motor (S) at a given ratio (R).

Parameters

  • Q
    • MotorID of the "master" motor
  • S
    • MotorID of the "slave" motor
  • R
    • ratio of <master>:<slave> scale
    • a ratio of 1 will be the equivalent of slaving the two motors together in Mach 4
M35 - (Electronically) Ungear Two Motors

Remove the gearing between two (previously) geared motors.
The motors must have been previously geared together using M34.

Parameters

  • Q
    • MotorID of the "master" motor
  • S
    • MotorID of the "slave" motor
M38 - Turn On SSV (Spindle Speed Variation)

Vary the spindle speed to help with chatter issues while cutting

Parameters

  • P
    • RPM to very the speed by
  • E
    • Time interval that the speed is varied by

M38 is not available on all systems

M39 - Turn Off SSV (Spindle Speed Variation)

Turn off the spindle speed variation

M200 – M219: Control Default Outputs:

Turn outputs 0-9 on/off.

Parameters:

            --none

Example:

M200 (Output 0 On)

M201 (Output 0 Off)

M202 (Output 1 On)

...

M219 (Output 19 Off)


M220 – Turn on Output:

General macro for turning on any outputs from parameters passed into the M-Code

This allows you to control any of the signals in Mach. 

Write to a single output:

--A - Any Output Signal

--S - Output Signal Starting from Output#0

--D - Delay (Optional) ms

Examples:

M220 S2 D2000 (will turn on output signal #2 then wait 2000 ms)

M220 A2 (will turn on output signal #2 “X- lim”)

Parameters A or S must be used but not both at the same time.

Write to multiple outputs using output numbers:

--(Data:"1-10, 63") - Using a GCode Comment on the same line as the M-Code to turn on multiple outputs

--D - Delay (Optional) ms

Example:

M220 D1000 (This is a comment Data:"1-10, 63") turn on Outputs 1 through 10 and 63 and delay 1000ms

Write to multiple outputs using MachMotion Parameter names:

--(Data:{"DriveEnable", "HardwareEnable"}) - Using a GCode Comment on the same line as the M-Code to turn on multiple outputs that are mapped to parameters

--D - Delay (Optional) ms

Example:

M220 D1000 (This is a comment Data:{"DriveEnable", "HardwareEnable"}) turn on the outputs that are mapped to parameters "DriveEnable" and "HardwareEnable" then delay 1000ms

The text entered after "Data:" should be a valid lua table using {} and "" and then remember to close the comment at the end of the line

M221 – Turn off Output:

General macro for turning off any outputs from parameters passed into the M-Code

This allows you to control any of the signals in Mach.

Write to a single output:

--A - Any Output Signal (This is typically not used and the "S" value is used instead. The "A" is based on the outputs listed in the configuration, starting at the beginning and counting all the way through.)

--S - Output Signal Starting from Output#0

--D - Delay (Optional) ms

Example:

M221 S2 D2000 (will turn off output signal #2 then wait 2000 ms)

M221 A2 (will turn off output signal #2 “X- lim”)

Parameters A or S must be used but not both at the same time.

Write to multiple outputs using output numbers:

--(Data:"1-10, 63") - Using a GCode Comment on the same line as the M-Code to turn off multiple outputs

--D - Delay (Optional) ms

Example:

M221 D1000 (This is a comment Data:"1-10, 63") turn off Outputs 1 through 10 and 63 and delay 1000ms

Write to multiple outputs using MachMotion Parameter names:

--(Data:{"DriveEnable", "HardareEnable"}) - Using a GCode Comment on the same line as the M-Code to turn off multiple outputs that are mapped to parameters

--D - Delay (Optional) ms

Example:

M221 D1000 (This is a comment Data:{"DriveEnable", "HardareEnable"}) turn off the outputs that are mapped to parameters "DriveEnable" and "HardareEnable" then delay 1000ms

The text entered after "Data:" should be a valid lua table using {} and "" and then remember to close the comment at the end of the line

M222 – Wait for input:

This can be used to wait for clamps, doors closed or coolant up to pressure

Wait on Input using Signal numbers:

--A - Any Input Signal (This is typically not used and the "S" value is used instead. The "A" is based on the inputs listed in the configuration, starting at the beginning and counting all the way through.)

--C - Show Cycle Start dialog after timeout with message from signal description or GCode comment

--S - Input Signal Starting from Input#0

--T - Time-out (Optional) in seconds to create Alarm showing the signal description for GCode comment

--I - Invert Logic (Optional) (High Active = 0, Low Active = 1)

Example:

M222 S2 T5 (will wait up to 5 sec for Input#2 to go high.)

M222 A2 I1 (will wait indefinitely for Input Signal #2 to go low. Note, this is not Input#2, but the second input listed in the list of inputs.)

Wait on Input using MachMotion Parameter Names:

--(Data:{"DriveEnable"}) - Using a GCode Comment on the same line as the M-Code to wait on inputs that are mapped to parameters

--T - Time-out (Optional) seconds

--I - Invert Logic (Optional) (High Active = 0, Low Active = 1)

Example:

M222 T5 (Data:{"DriveEnable"}) - Will wait up to 5 sec for the signal that is mapped to "DriveEnable" to go high.

M222 I1 (Data:{"DriveEnable"}) - Will wait indefinitely for the signal that is mapped to "DriveEnable" to go low. 

M223 – Pass Input to pound var:

This can be used along with macro B commands (macro B commands require industrial license to run) to set up logical switch cases or programed safety based on an input on the machine. i.e for running lights out you could have the first line of gcode check for a material verification input and do a M00 stop and wait for an operator if no material present.

Parameters:

            --A - Any Input Signal

            --S - Input Signal Starting from Input#0

            --V - Mach Memory Address

            --I - Invert Logic (Optional)

Example:

M223 S2 V500 (Will set #500 to the state of input#2)

IF [#500 EQ 1] GOTO 5 (check for material)

M00 (Stop)

N5

(Run Job)

M30

%


M224 – Pass Output to pound var:

Parameters:

--A - Any Input Signal

--S - Output Signal Starting from Output#0

--V - Mach Memory Address

--I - Invert Logic (Optional)

Example:

M223 S2 V500 (Will set #500 to the state of output signal #2)

M223 A2 V500 (Will set #500 to the inverse of the state of output #2 “x-“)


M225 – Call PLC Sequence:

By calling M225 in a gcode file you can start a block of user ladder logic in your PLC system. This is used for automation and custom setups

Parameters:

--D = Data to send

--V = Data index

--L = Debugging (logging) default 0

--S = Seq number

--M = Device number (modbus) default 0

Example:

M225 S5 (Start Seq 5 in PLC)

M225 S24 V0 D*PocketNumber (Rotate the tool carousel to pocket PocketNumber. USE WITH EXTREME CAUTION!)


M226 – Wait for PLC Sequence:

Used in conjunction with M225 this allows you to wait for the user ladder logic to signal completion.

Parameters:

--L = Debugging (logging) default 0

--S = Seq number

--M = Device number (modbus) default 0

Example:

M226 S5 (Waits for Seq 5 in PLC to finish)


M228 – GoTo Park Positions:

Easy setup to command the machine to user defined positions. This allows you to use the same code in all your files for tool setter position and then change that position once in one place when you need to move your tool setter.

Parameters:

--P - Position

Or

--(Data:"Name of GoTo Position") - GCode Comment formatted with GoTo position name

Example using "P" Parameter:

M228 P1 (GoTo Tool Change position)

P1 - Tool Change

P2 - Tool Setter

P3 - Part Zero

P4 - Machine Zero

P5 - Park Position

Example using GCode Comment:

M228 (Data:"Park 6")

M228 (Data:"Name of GoTo Position")


M230 – Map motor to axis:

            --A - AxisID

            --M - MotorID

Note: To map multiple motors, just run the macro multiple times with the new motor ID you want to map. 

M231 – Un-Map motor from axis:

            --A - AxisID

            --M – MotorID

Note: "M-1" unmaps all motors on that axis.

M232 – Save, Delete, Restore motor mappings

            --S1 Saves the current mapping config

            --R1 Restore the last saved motor mapping

            --D1 Delete current motor mapping.


M233 – Activate Dual Tables

            --T1 Activate Table 1

            --T2 Activate Table 2

            --T3 Activate Table 3


M238 – Read user fields from the tool table:

General macro for reading the user fields in the tool table and writing them to pound variables

Parameters:

--V = Pound variable used for reading the value from the tool table

--(Data:"SpindleRotation") - Using a G-Code Comment on the same line as the M-Code to pass in the user field name from the tool table

Example:

M238 V550 (Data:"SpindleRotation") - Read user field "SpindleRotation" from tool table and write it to pound variable #550


M239 – Write to user fields in the tool table:

General macro for writing to the user fields in the tool table

Parameters:

--T = Value to be written to tool table

--(Data:"SpindleRotation") - Using a G-Code Comment on the same line as the M-Code to pass in the user field name from the tool table

Or

--I = Amount to add to the current value in the tool table

--(Data:"SpindleRotation") - Using a G-Code Comment on the same line as the M-Code to pass in the user field name from the tool table

Example:

M239 T1 (Data:"SpindleRotation") - Read current value in T parameter and write it to the user field "SpindleRotation"

M239 I1 (Data:"SpindleRotation") - Read current value in T parameter and add it to the current value in user field "SpindleRotation"

M240 – Create GMS Message with GCode Comment

Parameters

  • P - Type of GMS Dialog 1 = Alarm, 2 = Notice, 3 = Notification
M242 – Read MachMotion OEM Parameter:

General macro for reading MachMotion OEM Parameters and writing them to pound variables

Parameters:

--V = Pound variable used for reading the value from the OEM Parameter

--(Data:"DriveEnableDelay") - Using a G-Code Comment on the same line as the M-Code to pass in the name of the OEM Parameter

Example:

M242 V550 (Data:"DriveEnableDelay") - Read the OEM Parameter and write it to pound variable #550


M243 – Write to OEM Parameter:

General macro for reading pound variables and writing them to MachMotion OEM Parameters

Parameters:

--T = Value to be written to OEM Parameter

--(Data:"DriveEnableDelay") - Using a G-Code Comment on the same line as the M-Code to pass in the name of the OEM Parameter

Or

--I = Amount to add to the current value in OEM Parameter

--(Data:"DriveEnableDelay") - Using a G-Code Comment on the same line as the M-Code to pass in the name of the OEM Parameter

Example:

M243 T500 (Data:"DriveEnableDelay") - Read current value in T parameter and write it to the OEM Parameter "DriveEnableDelay"

M243 T500 (Data:"DriveEnableDelay") - Read current value in T parameter and add it to the current value in the OEM Parameter "SpindleRotation"

M244 - Macro for starting a Reciprocating cycle on OB axis

Parameters

  • Q - Axis ID (X = 0)
  • A - Position A
  • B - Position B
  • D - Dwell on ends
  • F - Feedrate (units per minute)
M245 - Macro for stopping a Reciprocating cycle on OB axis

Parameters

  • Q - Axis ID (X = 0)
  • S - S0 or no parameters means stop right away, S1 = Stop on A, S2 - Stop on B, S3 - Stop on A or B
M246 - Positioning OB Axis

General macro for Positioning Out of Band Axes with a Mcode.  Has the option to command the axis to move and not wait for it to complete.

Parameters

  • Q - Axis ID (X = 0)
  • D - Position to move axis to
  • E - (Optional) the default is wait for the move to complete.  E1 will command and return right away without waiting for the move to finish.
  • F - Feedrate (units per minute)
  • W - Wait for the current move to complete.  This will ignore the position passed to D. (W = 1)
M247 - Getting the position for the nearest rotation for a given angle

General macro for getting the nearest rotation in part coords or machine coords

Parameters

  • Q - Axis ID (X = 0)
  • A - Angle
  • V - Pound Var to return the value to
  • E - (Optional) E1 will return part coords E0 or nill will return Machine Coords

Example:

M247 Q(Axis) A(Angle) V(Returned Position) E1 (Part Coords) E0 or nill (Machine Coords)

M248 - Read Axis Torque and Write to Pound Var

General macro for reading Axis torque and write to pound var

Parameters

  • A - Axis ID (X = 0)
  • V - Pound Var used to write the status of GMS warnings to
M249 - Read GMS Warnings

General macro for reading GMS Warning and write state to pound variable

Parameters

    • V - Pound Var used to write the status of GMS warnings to
M250 Probe Disable Wireless
M251 Probe Enable Wireless
M252 - General macro for checking to see if a signal or I/O is mapped to a parameter
M253 - Macro for Moving an OB Rotary Axis at a constant feedrate

Parameters

  • Q - Axis ID (X = 0)
  • F - Feedrate (units per minute)
  • D - Direction (1 = CW, -1 = CCW)
M254 - Macro for Stopping an OB Rotary Axis at a constant feedrate

Parameters

  • Q - Axis ID (X = 0)
M900 - Auto Tool Setter Set Tool Length

            --Examples

                       --M900 (Data: "1, 5")

                       --M900 (Data: "2-5")

                       --M900 (Data: "6")

M901 - Auto Calculate and Set Tool Setter Position
M902 - Auto Calculate and Set Z Fixture Offset
M903 - Change Auto Tool Setter name
M904 - Performs an action on a DataTracker
M920 - Control Profiling/Logging Behavior

This macro allows you to change the state (usually on/off) of an enabled profiling category.

Parameters

  • L; the profiling context
    • 0: Motion Controller (default)
  • C: the profiling category to affect
    • 0: Mach 4 core motion data (requires the MotionDataProfiler plugin)
    • 100: motion data sent to the motion controller
    • 101: motion loop performance data
    • 102: motion controller verbose log
    • 110-119: motion data sent to the drives
      • 110: motor positions, velocities, statuses
      • 111: motor positions, velocities, statuses, and other data for probing
      • 112: motor positions, velocities, statuses, and other data for homing
  • S: the new state for the profiling category
    • 0: off
    • 1: on
    • 2: toggle (default)
  • W: how/whether to wait for the state change to take effect
    • 0: do not wait, issue the command to change state and return
    • 1: wait until the state change is effective
    • 2: do not issue the command, but wait until the specified state is effective
      (This is usually done
M1000 - Mandatory Program Stop Wait for Cycle Start

Parameters

  • C - C1 will show the Standard Cycle Start dialog with the text from the GCode Comment

Pauses a program at any point, leaving the spindle running. Any comment provided on the same line will be shown to the customer. The control will not go into an Idle state with this mcode.

M1001 - Optional Program Stop

Pauses the program at any point the same as M1000, if the optional stop switch is turned on. Any comment on the same line will be shown to the customer. The control will not go into an Idle state with this mcode.


Custom M-Codes:

Every machine is a little different and has different functions and options. Custom M-Codes allow the user to create simple programming calls for complex processes, activating or deactivating outputs, reading inputs, performing math, or whatever else the user and machine could require. These M-Codes are programmed in Lua and placed in the macros folder under each profile. Every profile can have its own unique set of M-Codes.  The file name must be the macro to be called.

Keywords: MCode, M Code, M-Code