Mach4 Advanced M-Code Reference

Chapter 1 : Advanced Macros

M-Code Descriptions

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 9 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.)

--S - Input Signal Starting from Input#0

--T - Time-out (Optional) seconds

--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)

 

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

 

M231 – Un-Map motor from axis:

            --A - AxisID

            --M – MotorID

 

M232 – Save, Delete, Restore motor mappings

            --S1 Saves the current mapping config

            --R1 Restore the last saved motor mapping

            --D1 Delete current motor mapping.

 

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"

 

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"

 

M900 - Auto Tool Setter

            --Examples

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

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

                       --M900 (Data: "6")

 

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

Back to top