How to Write G Code for CNC Lathe: Basic Syntax and Example
To write
G-code for a CNC lathe, you create a sequence of commands that control the tool movement, spindle speed, and coolant. Each line typically starts with a G or M code followed by coordinates or parameters. For example, G01 X10 Z-5 F0.2 moves the tool linearly to position X=10, Z=-5 at feed rate 0.2.Syntax
G-code commands for CNC lathe usually include:
- G-codes: Define motion types (e.g.,
G00for rapid move,G01for linear cut). - M-codes: Control machine functions (e.g.,
M03to start spindle clockwise). - Coordinates: Positions on axes, typically
X(diameter) andZ(length). - Feed rate: Speed of tool movement, given by
F.
Each line is a command executed in order.
gcode
N10 G00 X0 Z0 N20 M03 S1000 N30 G01 X10 Z-5 F0.2 N40 G00 X100 Z100 N50 M05 N60 M30
Example
This example shows a simple CNC lathe program that starts the spindle, moves the tool to cut a straight line, then stops the spindle and ends the program.
gcode
N10 G00 X0 Z0 ; Move tool to start position N20 M03 S1200 ; Start spindle clockwise at 1200 RPM N30 G01 X20 Z-10 F0.15 ; Cut line to X=20, Z=-10 at feed 0.15 N40 G00 X100 Z100 ; Rapid move away from part N50 M05 ; Stop spindle N60 M30 ; End program
Common Pitfalls
Common mistakes when writing G-code for CNC lathe include:
- Forgetting to start the spindle with
M03before cutting. - Using wrong coordinate signs or mixing up
XandZaxes. - Not setting feed rate
F, causing tool to move too fast or too slow. - Skipping safety moves like rapid retract
G00after cutting.
Always verify your code with a dry run or simulation before actual machining.
gcode
Wrong: N10 G01 X20 Z-10 F0.15 ; Cutting without spindle on Right: N10 M03 S1000 ; Start spindle N20 G01 X20 Z-10 F0.15 ; Then cut
Quick Reference
| Code | Meaning | Example |
|---|---|---|
| G00 | Rapid positioning (move fast) | G00 X0 Z0 |
| G01 | Linear interpolation (cutting move) | G01 X10 Z-5 F0.2 |
| M03 | Spindle on clockwise | M03 S1200 |
| M05 | Spindle stop | M05 |
| M30 | Program end and rewind | M30 |
| F | Feed rate (speed of cut) | F0.15 |
Key Takeaways
Start your CNC lathe program by setting spindle speed with M03 before cutting.
Use G00 for fast moves and G01 for cutting moves with feed rate F.
Always specify coordinates clearly for X (diameter) and Z (length) axes.
Include safety moves like retracting tool with G00 after cutting.
Test your G-code with simulation or dry run to avoid errors.