There are many g-codes that are helpful to know (particularly when configuring a dual endstop setup), but they are scattered across the site and buried in the Marlin G-code Index. I figured it would be handy to have them all on one page.
This list is by no means 100% complete or accurate (at this point), and does not address the MP3DP or the LowRider at this point. Please speak up if you have something to add and I will edit this post to reflect your addition. If something is not listed here, you will probably find it if you dig in the Marlin G-code Index.
Feel free to copy, remix, and use this content wherever you want.
[I encourage MPCNC veterans to pick this document apart. Despite my efforts, I am sure to have screwed up some piece of info somewhere.]
Compatibility
This guide was made using Ryan’s Marlin-based MPCNC firmware. This information may not work on custom firmware flashed onto the device.
I have used OctoPrint as my g-code sender while testing all this info. If you are using Repetier Host or any other g-code sender, please let me know if any of this info does not work for your program. There shouldn’t be any surprises though (I hope!)
All measurements are in mm unless otherwise specified.
Cautionary Notes
A few words of warning before we begin:
- Exercise caution when executing any g-code command, especially for the first time. Unexpected results can and do happen.
- Ensure your toolhead is not touching the table when making any movements. Always do a z-raise first.
- G-code movements may ignore software endstops. Make sure your commands do not cause the machine to attempt movement past it’s own boundaries.
Commenting Your G-Code
The semicolon character “;
” is used to indicate the end of a command. Any text following after a semicolon (and before a new line) will be ignored by the software, which makes it a handy place for comments. This is particularly useful when making batch scripts.
Notes
- A line that begins with a semicolon will be treated as a comment.
- G-code instructions do not require a semicolon (in Marlin, at least). They are only necessary for commenting.
- Related: To display messages on the LCD screen, use the
M117
command. (e.g.M117 Hello world!
)
G-Code Examples
The following are all legitimate g-codes because of the placement of the semicolon, and should not cause an error:
G0 F300; Set feedrate to 300 mm/min
; G0 Z1000 (This line will do nothing because it comes after the semicolon.)
G0 Z0; Move Z-axis to origin
G0
/G1
- Linear Move
Move continuously in a straight line until reaching your input coordinates.
Parameters
X[pos] - X position (e.g. X5, X5.2, X-7)
Y[pos] - Y position (e.g. Y5, Y5.2, Y-7)
Z[pos] - Z position (e.g. Z5, Z5.2, Z-7)
F[rate] - Feedrate, ie. movement speed (default is in mm/min) (e.g. F1800)
Notes:
- Any combination of input parameters can be used or omitted as needed.
- Although
G0
andG1
are functionally identical, the Marlin docs say thatG0
is designated for rapid travel (“non-print”) moves. In general, just useG0
to be on the safe side in case anything changes in the future. - When the machine is powered on, the current coordinates of the toolhead is designated as X0 Y0 Z0. (The LCD will show flashing question marks until the axes have been homed.)
- Movements can be entered to a precision of 0.1mm [Adjustments in the range of 0.01 mm appear to have no effect and do not engage the steppers. Please correct me if this is wrong.]
- The feedrate will be preserved until changed by another g-code or the machine is powered off.
- Keep your feedrate low when doing z-moves so you don’t crash into the table too quickly.
G-Code Example:
Go to coordinates (X5, Y7.2, Z0) at a speed of 2100 mm/min:
G0 X5 Y7.2 Z0 F2100
G28
- Auto Home (X/Y/Z axis)
Perform a homing procedure on one or more axes. Requires endstops and/or a touch plate to send the homing signal. (X/Y homing uses endstops, Z homing uses a touch plate.)
Parameters
R - Distance to raise the Z-axis before homing (e.g. G28 R5 X Y)
X - Home the X-axis (e.g. G28 X)
Y - Home the Y-axis (e.g. G28 Y)
Z - Home the Z-axis (e.g. G28 Z)
Notes:
- Entering
G28
with no parameters will home all 3 axes one-by-one. (You probably don’t want to do this.) - Parameters can be combined to level two or more axes one-by-one (e.g.
G28 X Y
orG28 X Y Z
) - Newer versions of MPCNC firmware have the R parameter set automatically but it can be overridden if desired. It is a good idea to include an R parameter that allows for safe clearance in your homing operations.
- Don’t use
G28 Z
without a touch plate underneath! Your Z-axis will force itself into the table until you power off the machine! - After homing the Z-axis, use
G92 Z[touch plate thickness in mm]
to set the true Z-origin of your workpiece. (For a 1.5 mm thick touch plate, this will beG92 Z1.5
.) Then useG0 Z5 F300
to allow for clearance so you can remove the touch plate and clamp. Then use . - For more details about setting up endstops, see Ryan’s page on the subject.
G-Code Example:
Home the X and Y axes, but first raise the Z-axis by 5mm:
G28 R5 X Y
G92
- Set Position
Set the current position in firmware. Useful when zero’ing your coordinates before starting a job.
Parameters
X[pos] - X position (e.g. X5, X5.2, X-7)
Y[pos] - Y position (e.g. Y5, Y5.2, Y-7)
Z[pos] - Z position (e.g. Z5, Z5.2, Z-7)
Notes
- If you do not use endstops, you can simply jog your machine to the desired starting position for your cut, then enter
G92 X0 Y0 Z0
;. This will zero every axis.
G-Code Example
Reset all coordinates to zero:
G92 X0 Y0 Z0
M106
- Enable Cooling Fan/Set Fan Speed
Manually enable cooling fan(s) and set speed.
Parameters
P[index] - Fan index (labeled on electronic board, e.g. my cooling fan is on P2.)
S[speed] - Speed (range 0-255), will default to 255 if S parameter is not provided
Notes
- This feature can also be configured to run automatically by editing the firmware before flashing.
- You should be able to add the g-code to your CAM software’s post-processor. You can also add it to your g-code files manually.
- Some fans may not function properly at any speed other than the maximum speed. Don’t use the
S
parameter if this happens to you, just use the fan at full speed.
G-Code Example
Set the fan on the FAN2 header to full speed:
M106 P2 S255
M107
- Disable Cooling Fan
Manually disable cooling fan(s).
Parameters
P[index] - Fan index (Same as M106)
G-Code Example
Disable the fan connected to the FAN2 header:
M107 P2
M119
- Endstop Status
Reports whether each endstop is open or closed. Useful when configuring endstops, or when debugging homing issues.
Parameters: None
Notes
- For dual endstops, X1 will use the X-MIN endstop, and X2 will use the X-MAX endstop (labeled on the board, as seen in this picture). Same deal for the Y axes.
- When using a touch plate, use the Z-MIN endstop header on your board.
- For more info on configuring endstops, check out Ryan’s page here.
G-Code Example
M119
M666
- Software-based Endstop Offset
Ryan’s dual endstop page is the go-to guide for using M666 to square your X and Y axes:
Auto Square, Dual EndStops
The unfortunately designated M666 lets you test your offset to quickly calibrate your machine. Using the command M666 X0.72 would offset the X1 stepper 0.72mm away from it’s endstop, Y! can also be used. To verify your current settings during calibration just an M666 will show the current offsets. Once the correct offset are found you should input them into your firmware.
This is to simplify the actual placement of the endstops themselves, you only need to get them such that the X1 or Y1 endstop is at or slightly behind where it needs to be within a few Millimeters is best.
1-You can verify how square your axis is by measuring the diagonals of the largest rectangle you can draw in your build area. The larger the more accurate…but also harder to measure. I only have 6″ calipers but I was more accurate with a tape measure at my 440mm available diagonal.
2-Measure the diagonals to the X1 and Y1 endstops blocks.
3-Offset the endstop that has the short dimension by the amount it is short or just a hair over. So if the X1 diagonal was 1mm short you would offset X1 by 1mm. M666 X1. Tip – If it is more than 2mm off move the stop block, each belt tooth is 2mm.
4-Draw a fresh one to verify. If that is correct you should edit your firmware to make this change permanent in configuration.h Or add it to all of your Gcode.
5-Enjoy!
Care should be taken to use as little pen pressure as possible with the finest tip possible to get the most accurate results and a pen mount with some give should be used, example pen mount.
My trials and tribulations figuring this out….Feel free to make fun of me. Forum link.
In short, this command is used to set an offset on your X2 stepper. For example, a 0.5 mm offset will, after homing, move your X2 stepper over by +0.5mm and call that the zero point. This is used to ensure the Y-axis is square to the X-axis (i.e. exactly 90 degrees).
Parameters
X[adj] - X-axis adjustment
Y[adj] - Y-axis adjustment (Not used in Ryan's instructions)
Notes
- After dialing in your offset, use the command
M500
to save your settings in the EEPROM. If you don’t do this, yourM666
values will be reset to zero when the machine is powered off. - To check your current
M666
offsets, use the commandM503
. - If you decide to reset your offsets to zero, you can either use the
M502
command or enterM666 X0 Y0
. M666
offsets affect only your X2 and Y2 endstops, not X1 or Y1. Note that Ryan’s procedure does not use the Y parameter.- Instead of using
M666
, you can just move your endstops if you don’t want to do this in software. Use whichever method suits you.
G-Code Example
Set the X2 offset to 0.5mm:
M666 X0.5
Honorable Mentions
The following g-codes ended up being useful in some scripts I made, and generally seem useful to know about. However, I feel the ones listed above are more important, and I didn’t want to clutter up the list. Each item below contains a link to the Marlin G-code Index, and should have all the info you need.
G4
- Dwell
G90
- Use absolute coordinates for positioning
G91
- Use relative coordinates for positioning
M18/M84
- Disable Steppers
M117
- Display Message on LCD Screen
M211
- Enable/Disable Software Endstops
M300
- Play Tone (Beep)
M502
- Factory Reset EEPROM
M503
- Report EEPROM Settings
M504
- Validate EEPROM
[Again, please let me know if anything is missing, wrong, or needs clarification. I hope it helps someone to have all this info in one place!]
This work is licensed under a Creative Commons Attribution 4.0 International License.