G-code is the language your CNC milling machine understands. Every movement, every change in spindle speed and every tool change is controlled by G-code commands. Even though CAM software generates the G-code automatically, a basic understanding is worthwhile for troubleshooting, fine-tuning and simple manual programming.
What is G-code?
G-code (also RS-274) is a standardised control format for CNC machines. A G-code program consists of lines (blocks), each containing one or more commands. Each command begins with a letter followed by a number.
The Most Important G Commands (Geometry)
| Command | Function | Example |
|---|---|---|
| G0 | Rapid traverse (fast positioning) | G0 X100 Y50 Z10 |
| G1 | Linear move at feed rate (machining) | G1 X200 Y100 F1000 |
| G2 | Circular arc, clockwise | G2 X50 Y50 I25 J0 |
| G3 | Circular arc, anticlockwise | G3 X50 Y50 I25 J0 |
| G17 | Select XY plane | G17 |
| G20/G21 | Inches / millimetres | G21 (metric) |
| G28 | Reference move (home) | G28 |
| G90 | Absolute coordinate system | G90 |
| G91 | Relative (incremental) system | G91 |
The Most Important M Commands (Machine)
| Command | Function | Example |
|---|---|---|
| M3 | Spindle ON (clockwise) | M3 S18000 |
| M5 | Spindle OFF | M5 |
| M6 | Tool change | T1 M6 |
| M8/M9 | Coolant ON / OFF | M8 |
| M30 | Program end and reset | M30 |
A Simple G-code Example
This program mills a 50 x 30 mm rectangle to a depth of 3 mm:
G21 G90 (Metric, absolute) G0 Z5 (Safe height) M3 S18000 (Spindle on, 18,000 rpm) G0 X0 Y0 (Start position) G1 Z-3 F500 (Plunge to -3 mm) G1 X50 F1500 (Move right) G1 Y30 (Move up) G1 X0 (Move back left) G1 Y0 (Move back to start) G0 Z5 (Retract) M5 (Spindle off) M30 (Program end)
G-code and CAM Software
In practice, you rarely write G-code by hand. CAD/CAM software such as Fusion 360, Estlcam or VCarve generates the G-code automatically from your 3D models. Even so, a basic understanding is indispensable:
- Troubleshooting: When the machine does something unexpected, a look at the G-code helps
- Fine-tuning: Adjust feed rates and spindle speeds directly in the code
- Simple tasks: Facing, slotting or drilling can be programmed manually in a short time
Tips for G-code Beginners
- Use a G-code simulator (e.g. NCViewer.com) before testing on the machine
- Always run new programs as a dry run first (without material)
- Pay attention to the correct unit: G21 for millimetres, G20 for inches
- Use comments in brackets ( ) for documentation
Frequently Asked Questions
Do I have to learn G-code to operate a CNC milling machine?
No, CAM software generates the G-code automatically. A basic understanding of the most important commands (G0, G1, M3, M5) is very helpful for troubleshooting and fine-tuning, however.
What is the difference between G0 and G1?
G0 is rapid traverse: the machine moves to the target position at maximum speed (positioning). G1 is the working move at a defined feed rate (F value): this is where milling actually takes place.
Is G-code the same on all CNC machines?
The basic commands (G0, G1, G2, G3, M3, M5) are standardised and work on practically all CNC milling machines. Some extended commands and macros may differ depending on the controller.

0 comments