0
0
Ev-technologyHow-ToBeginner · 4 min read

How to Write G Code for CNC: Basic Guide and Examples

To write G-code for CNC, you create a text file with commands that control machine movements and actions using codes like G00 for rapid moves and M03 to start the spindle. Each line usually contains a command and coordinates or parameters to tell the CNC what to do step-by-step.
📐

Syntax

G-code commands are written line by line. Each line can include a G command (motion or function), coordinates (X, Y, Z), and other parameters like feed rate (F) or spindle speed (S). Lines usually start with a line number (optional) and end with a command to move or perform an action.

  • G00: Rapid positioning
  • G01: Linear interpolation (cutting move)
  • X, Y, Z: Coordinates
  • F: Feed rate (speed of cutting)
  • M03: Spindle on clockwise
  • M05: Spindle stop
gcode
N10 G00 X0 Y0 Z0
N20 M03 S1000
N30 G01 X50 Y50 F150
N40 M05
N50 G00 X0 Y0
💻

Example

This example shows a simple CNC program that moves the tool to the start, turns on the spindle, cuts a straight line, stops the spindle, and returns to start.

gcode
N10 G00 X0 Y0 Z5
N20 M03 S1200
N30 G01 Z-1 F100
N40 G01 X50 Y0 F200
N50 G01 X50 Y50
N60 G01 X0 Y50
N70 G01 X0 Y0
N80 M05
N90 G00 Z5
Output
Moves tool to safe height, starts spindle at 1200 RPM, plunges down to cut depth, cuts a square path, stops spindle, and lifts tool.
⚠️

Common Pitfalls

Beginners often forget to set the spindle speed or feed rate, causing tool damage or poor cuts. Another mistake is not moving the tool to a safe height before rapid moves, risking crashes. Also, missing M05 to stop the spindle can be unsafe.

Always check coordinate units (mm or inches) and machine zero points before running code.

gcode
Wrong:
N10 G00 X0 Y0 Z-5
N20 M03
N30 G01 X50 Y50

Right:
N10 G00 X0 Y0 Z5
N20 M03 S1000
N30 G01 Z-1 F100
N40 G01 X50 Y50 F200
📊

Quick Reference

CodeMeaningExample
G00Rapid move (non-cutting)G00 X10 Y10
G01Linear cut moveG01 X20 Y20 F150
M03Spindle on clockwiseM03 S1200
M05Spindle stopM05
FFeed rate (cutting speed)F100
SSpindle speed (RPM)S1000
X, Y, ZCoordinatesX50 Y50 Z-1

Key Takeaways

Write G-code line by line with commands and coordinates to control CNC movements.
Always set spindle speed (S) and feed rate (F) to avoid tool damage.
Use safe Z heights for rapid moves to prevent crashes.
Include spindle start (M03) and stop (M05) commands properly.
Check units and machine zero before running your program.