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:
- G β Preparatory function (motion type, mode setting)
- M β Miscellaneous function (machine control: spindle, coolant, program end)
- X, Y, Z β Linear axis coordinates
- A, B, C β Rotary axis coordinates (rotation around X, Y, Z respectively)
- I, J, K β Arc center offsets (used with G2 and G3)
- F β Feed rate (mm/min or in/min)
- S β Spindle speed (RPM) or laser power
- T β Tool number (tool change)
- E β Extruder position (3D printers)
- R β Arc radius (alternative to I/J/K) or cycle retract height
- N β Line (block) number (optional, for reference)
- P, Q β Dwell time or cycle parameters
Motion Commands
G0 X100 Y50 Z5 ; Rapid to X=100, Y=50, Z=5mm (safe height)
G1 X50 Y30 Z-3 F800 ; Cut in straight line to X50,Y30 at 3mm depth, 800 mm/min
; 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 P1000 ; Pause for 1000 milliseconds (1 second)
Coordinate System Commands
G17 ; Select XY plane (default for most vertical mills)
G21 ; Set millimeter mode β always include this line!
G28 ; Home all axes
G28 X Y ; Home only X and Y axes
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 E0 ; Reset extruder position to 0 (standard before printing)
G92 X0 Y0 ; Declare current position as X=0, Y=0
G54 ; Activate Work Coordinate System 1 (most common)
G0 X0 Y0 Z5 ; Rapid to part origin at safe height
Canned Cycles (Drilling & Boring)
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 X20 Y20 Z-40 R5 Q5 F150 ; Peck drill: 5mm pecks to 40mm depth
3D PrinterβSpecific G-Codes
G28 ; Home first
G29 ; Run auto bed leveling
Essential M-Codes
M3 S12000 ; Spindle ON CW at 12,000 RPM
M5 ; Spindle OFF
M104 S215 ; Start heating hotend to 215Β°C (doesn't wait)
M109 S215 ; Set hotend to 215Β°C and WAIT until reached
M140 S60 ; Start bed heating (doesn't wait)
M190 S60 ; Set bed to 60Β°C and wait
M106 S255 ; Fan full speed
M107 ; Fan off
M8 ; Coolant ON
M9 ; Coolant OFF
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.
GCodex