G-code Basics¶
Understanding G-code helps you troubleshoot issues, optimize performance, and customize Rayforge output for your specific machine.
What is G-code?¶
G-code is a standardized programming language for CNC machines (including laser cutters). It tells the machine exactly where to move, how fast to move, and what to do (laser on/off, power level, etc.).
Key points:
- Text-based - Human-readable commands
- Line-by-line - Machine executes one command at a time
- Standardized - Based on ISO 6983 and RS-274 standards
- Dialect variations - Different firmware (GRBL, Marlin, etc.) have slight differences
Example G-code:
G21 ; Set units to millimeters
G90 ; Use absolute positioning
G0 X10 Y10 ; Rapid move to position (10, 10)
M4 S500 ; Turn laser on at 50% power
G1 X50 Y10 F3000 ; Linear move to (50, 10) at 3000 mm/min
M5 ; Turn laser off
How Rayforge Generates G-code¶
Rayforge converts your designs into G-code through a multi-step process:
Generation pipeline:
Design (Workpieces + Layers)
|
v
Operations (Contour, Raster, etc.)
|
v
Toolpaths (Lines, arcs, points)
|
v
G-code Commands (GRBL dialect)
|
v
Output File (.gcode)
What Rayforge does:
- Analyzes your design - Extracts geometry from workpieces
- Applies operations - Determines cut/engrave paths
- Optimizes toolpaths - Reorders paths, minimizes travel
- Generates commands - Converts paths to G-code
- Injects hooks - Adds user-defined macros at specified points
- Writes file - Outputs complete G-code ready for machine
G-code Command Structure¶
Command Format¶
Basic syntax:
Examples:
- G0
- G command, number 0 (rapid positioning)
- G1 X50 Y20 F3000
- G command with parameters X, Y, F
- M4 S500
- M command with parameter S
Line structure:
G1 X50.0 Y20.5 F3000 ; Move to (50, 20.5) at 3000 mm/min
^ ^ ^ ^ ^
| | | | |
| | | | +-- Comment (optional)
| | | +---------- F parameter (feed rate)
| | +---------------- Y parameter (Y coordinate)
| +--------------------- X parameter (X coordinate)
+------------------------ G command (linear move)
Command Types¶
Letter | Type | Purpose | Examples |
---|---|---|---|
G | Motion/Setup | Movement, positioning mode | G0, G1, G2, G3, G21, G90 |
M | Machine | Laser on/off, air assist, coolant | M3, M4, M5, M8, M9 |
S | Parameter | Spindle/laser power | S0-S1000 |
F | Parameter | Feed rate (speed) | F3000 |
X/Y/Z | Parameter | Coordinates | X50 Y20 |
I/J/K | Parameter | Arc center offsets | I10 J5 |
Essential G-code Commands¶
Motion Commands (G-codes)¶
G0 - Rapid Positioning¶
Purpose: Fast movement without cutting (laser off)
Format: G0 X<x> Y<y> [Z<z>] [F<speed>]
Example:
When Rayforge uses it: - Moving between cut paths - Returning to origin - Positioning before cutting
G1 - Linear Interpolation¶
Purpose: Controlled linear movement (cutting/engraving)
Format: G1 X<x> Y<y> [Z<z>] F<speed>
Example:
When Rayforge uses it: - Cutting straight lines - Engraving raster lines - Any controlled movement with laser on
G2 / G3 - Arc Interpolation¶
Purpose: Circular/arc movements
Format: G2/G3 X<x> Y<y> I<i> J<j> F<speed>
- G2 - Clockwise arc
- G3 - Counter-clockwise arc
- I, J - Arc center offsets from start point
Example:
When Rayforge uses it: - Cutting circles and curves - Rounded corners - Optimized smooth curves
G4 - Dwell (Pause)¶
Purpose: Pause for specified time
Format: G4 P<seconds>
Example:
When used: - Allowing laser to burn at a point (rare) - Custom macros for material settling
Setup Commands¶
G21 / G20 - Units¶
Purpose: Set measurement units
- G21 - Millimeters (Rayforge default)
- G20 - Inches
Example:
G90 / G91 - Positioning Mode¶
Purpose: Set absolute or incremental positioning
- G90 - Absolute (coordinates are absolute positions)
- G91 - Incremental (coordinates are relative movements)
Example:
G90 ; Absolute mode
G0 X10 ; Move to position X=10
G0 X20 ; Move to position X=20
G91 ; Incremental mode
G0 X10 ; Move 10mm in +X direction
G0 X10 ; Move another 10mm in +X direction (now at X=30 total)
Rayforge default: G90 (absolute mode)
Machine Commands (M-codes)¶
M3 / M4 - Spindle/Laser On¶
Purpose: Turn laser on
- M3 - Spindle mode (power varies with speed)
- M4 - Laser mode (constant power, GRBL 1.1+)
Format: M3/M4 S<power>
Example:
Rayforge uses: M4 for predictable laser power
M5 - Spindle/Laser Off¶
Purpose: Turn laser off
Format: M5
Example:
When Rayforge uses it: - End of each cut path - Between operations - End of job (safety)
M8 / M9 - Coolant/Air Assist¶
Purpose: Control air assist or coolant
- M8 - Air assist on
- M9 - Air assist off
Example:
Parameters¶
S - Spindle/Laser Power¶
Format: S<value>
Range: 0-1000 (GRBL), 0-255 (some firmwares)
Example:
F - Feed Rate (Speed)¶
Format: F<speed>
Units: mm/min (or inches/min if G20)
Example:
Modal: Feed rate persists until changed
G1 X10 F3000 ; Set speed to 3000
G1 X20 ; Still uses 3000 (no F needed)
G1 X30 F1500 ; Change speed to 1500
Rayforge G-code Structure¶
Typical File Structure¶
Complete example:
; ==========================================
; Generated by Rayforge
; Job: test-project
; Date: 2025-10-03 15:30:00
; ==========================================
; === PREAMBLE ===
G21 ; Set units to mm
G90 ; Absolute positioning
; === JOB START HOOK (user-defined) ===
$H ; Home machine
M8 ; Air assist on
; === LAYER 1: Engrave Layer ===
; Layer Start Hook (if defined)
G0 X10.000 Y10.000 ; Rapid to start position
M4 S400 ; Laser on at 40% power
G1 X50.000 F3000 ; Engrave line
G1 Y50.000 ; Engrave line
G1 X10.000 ; Engrave line
G1 Y10.000 ; Engrave line
M5 ; Laser off
; Layer End Hook (if defined)
; === LAYER 2: Cut Layer ===
; Layer Start Hook (if defined)
G0 X20.000 Y20.000 ; Rapid to start position
M4 S900 ; Laser on at 90% power
G1 X60.000 F800 ; Cut line at slower speed
G1 Y60.000
G1 X20.000
G1 Y20.000
M5 ; Laser off
; Layer End Hook (if defined)
; === JOB END HOOK (user-defined) ===
M5 ; Ensure laser is off
M9 ; Air assist off
G0 X0 Y0 ; Return to origin
; === END OF JOB ===
Components Explained¶
1. Header Comments¶
; ==========================================
; Generated by Rayforge
; Job: test-project
; Date: 2025-10-03 15:30:00
; ==========================================
Purpose: - Identify the file - Track when it was generated - Document job name
2. Preamble¶
Purpose: - Ensure machine is in correct mode - Set up coordinate system - Prevent mode confusion
3. Job Start Hook¶
Purpose: - User-defined setup commands - Machine initialization - Safety preparation
Configured in: Settings > Machine > Hooks
4. Layer Operations¶
; === LAYER 1: Engrave Layer ===
G0 X10.000 Y10.000 ; Rapid to start
M4 S400 ; Laser on
G1 X50.000 F3000 ; Cut
M5 ; Laser off
Purpose: - Execute each layer's operations - Organized by layer order - Comments indicate layer names
5. Job End Hook¶
Purpose: - User-defined cleanup commands - Safety (laser off) - Return to known position
When to Edit G-code Manually¶
Good Reasons to Edit¶
-
Add pauses for material changes or inspection
-
Adjust power mid-job for testing
-
Fine-tune positions for alignment
-
Insert custom commands not supported by Rayforge UI
Bad Reasons to Edit¶
- Complex changes - Re-design in Rayforge instead
- Changing toolpaths - Error-prone, use Rayforge operations
- Major restructuring - Better to regenerate from source
Editing Tips¶
Before editing: - Backup the file - Keep original - Understand the commands - Don't guess - Test on scrap - Verify changes work
Common edits:
; Add pause before cut
G4 P2
; Reduce power for testing
M4 S100 ; Was S500, testing at 10%
; Shift entire job by offset
; Find: G0 X10
; Replace: G0 X15 (shift 5mm right)
Advanced G-code Topics¶
Modal Commands¶
Modal means a command stays active until explicitly changed.
Examples: - G90 - Absolute mode (modal, stays until G91) - F3000 - Feed rate (modal, stays until changed) - M4 - Laser on (modal until M5)
Implication:
G90 ; Set absolute mode (stays active)
F3000 ; Set feed rate (stays active)
G1 X10 ; Uses absolute mode and F3000
G1 X20 ; Still uses same mode and feed rate
G1 X30 F1500 ; Change feed rate, still absolute mode
Coordinate Systems (G54-G59)¶
What they are: Multiple work coordinate systems for advanced setups.
Commands: - G54 - Work Coordinate System 1 (default) - G55 - Work Coordinate System 2 - G56 - Work Coordinate System 3 - ... up to G59
Use case: - Multiple fixture positions on bed - Switch between jobs without re-homing
Rayforge support: Limited; primarily uses G54.
Offsets (G92)¶
Purpose: Temporarily redefine coordinate zero.
Format: G92 X<x> Y<y>
Example:
Caution:
- Can cause confusion
- Easy to forget offsets are active
- Clear with: G92.1
(GRBL)
Rayforge: Avoids offsets, uses absolute positioning instead.
Troubleshooting G-code Issues¶
Commands Rejected by Machine¶
Problem: Machine sends error or alarm.
Diagnosis:
- Check G-code dialect - GRBL vs Marlin vs Smoothieware
- Verify command syntax - Typos or invalid parameters
- Check firmware version - Some commands require newer firmware
Solutions:
- Use correct dialect in Rayforge settings
- Validate G-code with online checker (NCViewer, CAMotics)
- Update firmware if needed
Unexpected Movement¶
Problem: Machine moves to wrong positions.
Diagnosis:
- Check positioning mode - G90 (absolute) vs G91 (incremental)
- Check for offsets - G92 may be active
- Check coordinates - Out of range or incorrect
Solutions:
- Ensure G90 in preamble
- Clear offsets with G92.1
- Re-home machine
Laser Not Turning On/Off¶
Problem: Laser stays on or doesn't activate.
Diagnosis:
- Check M commands - M4/M5 present?
- Check S parameter - Power value correct?
- Check firmware laser mode -
$32=1
for GRBL
Solutions:
- Verify M4 S
before cuts - Verify M5 after cuts
- Enable laser mode in firmware settings
Best Practices¶
- Always include preamble - G21, G90 to set modes
- Always end with M5 - Laser off for safety
- Use comments - Document your changes
- Test on scrap - Before production runs
- Validate G-code - Use online simulators
- Keep backups - Of working G-code files
- Know your firmware - GRBL, Marlin, etc. differences
Related Pages¶
- G-code Dialects - Firmware differences
- Exporting G-code - Export settings and options
- Firmware Compatibility - Firmware versions and features
- Macros & Hooks - Custom G-code injection
- GRBL Settings - Controller configuration