← Open Viewer
← All Articles Motion Coordinates 3D Printer M-Codes

Essential G-Code & M-Code Commands Every Machinist Must Know

Whether you run a CNC mill, a lathe, a 3D printer, a laser cutter, or a syringe pump bioprinter β€” you are running G-Code. This reference covers the most important G and M codes, with clear explanations and real-world code examples for each. Bookmark this page: it is the reference sheet you'll return to every time you need to quickly look up a command.

This is not an exhaustive standard β€” that would fill a book. Instead, we focus on the commands you'll actually use 95% of the time, ordered by the workflow you encounter them in. For a conceptual introduction to what G-Code is and how to read it, see our Beginner's Guide to G-Code.

Address Letters Quick Reference

Every G-Code word starts with an address letter. Here are the key ones:

Motion Commands

G0 β€” Rapid Positioning
Moves to target position at maximum machine speed. No cutting β€” used for travel moves. All axes move simultaneously. Does not guarantee a straight-line path at the full programmed feed rate β€” never use G0 while cutting.
G0 X100 Y50 Z5    ; Rapid to X=100, Y=50, Z=5mm (safe height)
G1 β€” Linear Interpolation (Cutting Feed)
Moves in a straight line at the programmed feed rate F. This is the primary cutting command. All axes move simultaneously at a synchronized rate to arrive at the target position together.
G1 X50 Y30 Z-3 F800   ; Cut in straight line to X50,Y30 at 3mm depth, 800 mm/min
G2 / G3 β€” Circular Arc Interpolation
G2 cuts a clockwise arc; G3 cuts counter-clockwise. Specify the end point (X, Y) and the arc center offset from the start point (I, J) β€” or use R for the arc radius. The G17/G18/G19 plane selection must be active (G17 = XY plane is the default).
; Full circle (start = end, center offset I,J from start):
G2 X0 Y0 I25 J0 F600  ; CW full circle, radius=25mm

; Quarter arc using radius:
G3 X50 Y50 R50 F600   ; CCW 90Β° arc, radius=50mm
G4 β€” Dwell (Pause)
Pauses program execution for a specified time. Useful for spindle speed stabilization before a cut, or waiting for a drill to clear chips at the bottom of a hole.
G4 P1000    ; Pause for 1000 milliseconds (1 second)

Coordinate System Commands

G17 / G18 / G19 β€” Plane Selection
Selects the working plane for arc interpolation. G17=XY (default and most common), G18=XZ, G19=YZ.
G17   ; Select XY plane (default for most vertical mills)
G20 / G21 β€” Units
G20 = inch mode. G21 = millimeter mode. Always set explicitly at the start of any program to avoid disasters from unit confusion.
G21   ; Set millimeter mode β€” always include this line!
G28 β€” Return to Home (Reference) Position
Commands the machine to move to the machine's hardware reference/homing switches. On 3D printers this is typically the front-left-bottom corner. On CNC mills it is the machine zero position. Can be followed by axis letters to home specific axes.
G28              ; Home all axes
G28 X Y          ; Home only X and Y axes
G90 / G91 β€” Absolute / Incremental Mode
G90: all coordinates measured from the current work zero (absolute). G91: all coordinates measured from the current position (incremental/relative). Default is G90 in most programs.
G90              ; Absolute mode β€” X50 means go to X=50mm from zero
G91              ; Incremental mode β€” X10 means move +10mm from current position
G90              ; Switch back to absolute
G92 β€” Set Position (Zero Offset)
Redefines the current position as the specified coordinate without physically moving any axis. Commonly used on 3D printers to set the current position as zero after manual moves or to adjust for a new origin.
G92 E0           ; Reset extruder position to 0 (standard before printing)
G92 X0 Y0        ; Declare current position as X=0, Y=0
G54–G59 β€” Work Coordinate System Offsets (CNC)
Activates one of six programmable work coordinate systems. The operator measures the part's datum relative to machine zero and enters those offsets in G54 (or G55–G59 for multiple setups/fixtures). This lets the programmer write code relative to the part, not the machine.
G54   ; Activate Work Coordinate System 1 (most common)
G0 X0 Y0 Z5   ; Rapid to part origin at safe height

Canned Cycles (Drilling & Boring)

G81 β€” Simple Drill Cycle
Drills to depth Z, then retracts to the R-plane (retract height). Repeat at each X,Y position until G80 cancels the cycle.
G81 X10 Y10 Z-20 R5 F200   ; Drill at X10,Y10 to Z=-20, retract to Z=5
X50 Y10                    ; Same cycle at X50,Y10
X50 Y50                    ; And again at X50,Y50
G80                        ; Cancel canned cycle
G83 β€” Peck Drill Cycle
Drills in incremental depth steps (Q) with full retract to R-plane between each peck, clearing chips. Essential for deep holes in aluminum or steel.
G83 X20 Y20 Z-40 R5 Q5 F150  ; Peck drill: 5mm pecks to 40mm depth

3D Printer–Specific G-Codes

G29 β€” Auto Bed Leveling
Triggers the auto bed leveling (ABL) routine. The printer probes multiple points on the bed and creates a mesh compensation map. Must be run after G28 (home).
G28    ; Home first
G29    ; Run auto bed leveling

Essential M-Codes

M3 / M4 / M5 β€” Spindle Control (CNC)
M3: spindle ON clockwise. M4: spindle ON counter-clockwise. M5: spindle OFF. The S word sets speed in RPM.
M3 S12000   ; Spindle ON CW at 12,000 RPM
M5          ; Spindle OFF
M104 / M109 β€” Hotend Temperature (3D Printers)
M104 sets the target temperature without waiting. M109 sets temperature and waits until it is reached before continuing. Always use M109 before starting a print.
M104 S215      ; Start heating hotend to 215Β°C (doesn't wait)
M109 S215      ; Set hotend to 215Β°C and WAIT until reached
M140 / M190 β€” Bed Temperature (3D Printers)
Same wait/no-wait pattern as M104/M109, but for the heated print bed. PLA typically prints on a 60Β°C bed; ABS requires 100–110Β°C.
M140 S60       ; Start bed heating (doesn't wait)
M190 S60       ; Set bed to 60Β°C and wait
M106 / M107 β€” Part Cooling Fan (3D Printers)
M106 turns on the part cooling fan (0–255 PWM speed). M107 turns it off. Slicers control this automatically but it's useful for manual overrides in start/end scripts.
M106 S255   ; Fan full speed
M107        ; Fan off
M8 / M9 β€” Coolant (CNC)
M8 turns on flood coolant. M9 turns off all coolant. Some controllers use M7 for mist coolant.
M8   ; Coolant ON
M9   ; Coolant OFF
M30 β€” Program End
Ends the program, rewinds to start, and returns control to the operator. Always the last line of a CNC program. On 3D printers, slicers use M84 to disable steppers instead.
M30   ; End of program

A Complete 3D Printer Startup Template

Here is a well-structured start script for an FDM 3D printer running Marlin firmware, showing how all these codes work together in sequence:

; === START SCRIPT ===
G21                  ; Millimeter units
G90                  ; Absolute positioning
M82                  ; Extruder absolute mode
M104 S210            ; Start hotend heating (non-blocking)
M140 S60             ; Start bed heating (non-blocking)
G28                  ; Home all axes
G29                  ; Auto bed leveling
M109 S210            ; Wait for hotend temperature
M190 S60             ; Wait for bed temperature
G92 E0               ; Reset extruder position
G1 Z0.2 F300         ; Move to first layer height
G1 X10 Y10 F3000     ; Move to purge start
G1 X70 Y10 E8 F800   ; Purge line
G92 E0               ; Reset extruder after purge
; === END START SCRIPT β€” PRINT BEGINS ===

Tip: Use GCodex to load any G-Code file and visually verify that every motion command produces the expected toolpath before running on your machine. Catch missing retracts, wrong depths, and over-travel before they cause real damage.