What G-code is and what it is for
G-code is the language a Computer Numerical Control (CNC) machine understands. Each line of the program tells the control what movement to make, at what speed and against what references. When you load a program into a CNC lathe or milling machine, what the machine reads is an ordered list of instructions written almost always in this language, also known as G-code or ISO code.
The idea is simple: you describe the path and the cutting conditions, and the machine executes them with an accuracy that would be impossible to repeat hundreds of times by hand. Learning the basic G-codes is one of the first serious steps if you are thinking about how to become a machinist and want to go from watching the machine to programming it.
Many programs today are generated by CAM from the 3D model, but being able to read and correct the code by hand is still essential. A good operator spots an error on the screen before the tool is driven into the part.
The difference between G-codes and M-codes
In the same program you will see two families of instruction mixed together, and it pays not to confuse them:
- G-codes (preparatory functions): they control movement and geometry. They tell the machine how to travel: in a straight line, in an arc, rapid or at feed rate, in which coordinate system, and so on.
- M-codes (auxiliary or miscellaneous functions): they control machine functions and accessories. They start or stop the spindle (M03/M04/M05), open or close the coolant (M08/M09), change the tool (M06) or end the program (M30).
The shop-floor mnemonic: G moves, M switches things on and off. If any of the terminology is unfamiliar, have a look at the glossary of machining terms.
The most used G-codes
You do not need to memorise the hundreds of codes that exist. This handful will get you through most beginner jobs. Note that some codes vary slightly between controls (Fanuc, Siemens, Heidenhain, Haas and so on), so always check your machine's manual.
Basic moves: G00 and G01
- G00 – Rapid positioning: moves the tool to a point at maximum speed. Used to approach or retract in free air, never to cut. Careful: the rapid path is not always a straight line between two points.
- G01 – Linear interpolation: a straight move at the feed rate (F) you program. This is the one for cutting.
Arcs: G02 and G03
- G02 – Clockwise circular interpolation: traces an arc clockwise.
- G03 – Counter-clockwise circular interpolation: the same arc, the other way round.
The radius is defined with the R word or with the centre coordinates (I, J, K). Mixing up G02 and G03 is a classic: the arc comes out "backwards" and the part is ruined.
Units: G20 and G21
- G21 – Programming in millimetres. The norm in Europe.
- G20 – Programming in inches. Common on drawings from the English-speaking world.
Starting a program in the wrong units is one of the most expensive mistakes there is: a 25 dimension can become 25 inches, which is 635 mm.
Coordinates: G90 and G91
- G90 – Absolute coordinates: every position is measured from the work offset. The most intuitive way to start.
- G91 – Incremental coordinates: every move is measured from the previous position. Useful for repeating patterns, but easy to get in a muddle with.
Machine reference: G28
G28 sends the axes to the machine reference point (home). It is used at the start or the end of a program and at tool changes, to leave the table in a safe and known position.
Cutter radius compensation: G40, G41 and G42
The cutter has a diameter, so the centre of the tool does not coincide with the profile of the part. Radius compensation corrects that offset automatically:
- G41 – Compensation to the left of the direction of travel.
- G42 – Compensation to the right of the direction of travel.
- G40 – Cancels compensation. It always has to be closed before you finish.
It also lets you adjust sizes: if the part comes out slightly oversize, you change the radius value in the offset table without touching the program.
Canned cycles: G81 and G83
Canned cycles condense repetitive operations into a single line. The drilling ones are the first you will learn:
- G81 – Simple drilling: goes down, drills to depth Z and comes back up. Ideal for shallow holes.
- G83 – Deep drilling with pecking: goes down in steps and retracts on each one to break and clear the chip. Essential in deep holes if you do not want to break the drill.
Summary table
| Code | Function | When it is used |
|---|---|---|
| G00 | Rapid in free air | Approaching / retracting the tool |
| G01 | Straight line at feed | Machining straight lines |
| G02 / G03 | Clockwise / counter-clockwise arc | Radii and curves |
| G20 / G21 | Inches / millimetres | At the start of the program |
| G28 | Go to reference | Start, end and safe changes |
| G40 / G41 / G42 | Radius compensation | Profiling with size adjustment |
| G81 / G83 | Drilling cycles | Simple and deep holes |
| G90 / G91 | Absolute / incremental | Setting the dimensioning system |
A worked example
A simple milling fragment in millimetres and absolute coordinates. What matters is that you follow the logic line by line:
G21 G90— we work in millimetres and in absolute.G28— machine reference, so we start from a known place.M06 T01— change to tool 1.M03 S1200— start the spindle at 1200 rpm.G00 X0 Y0— position in rapid over the work offset.G00 Z5— rapid down to 5 mm above the part.G01 Z-3 F100— enter the part down to Z-3 at a feed of 100 mm/min.G01 X50 F250— cut a straight line to X50.G00 Z25— retract the tool in free air.M05— stop the spindle.M30— end of program.
Notice the pattern: first you set the conditions, you approach in rapid, you cut with G01 at a controlled feed, and you retract. That skeleton repeats in nearly every program.
Typical beginner mistakes
- Mixing units: forgetting the G21/G20, or changing them halfway through a program.
- Cutting with G00: using the rapid thinking it is a working feed. The machine does not forgive.
- Confusing G02 and G03: the arc comes out inverted.
- Not closing compensation (G40): it leaves the machine in a state that ruins the next operation.
- A badly set work offset: the program may be perfect, but if the origin is wrong, everything shifts.
- Feeds and speeds by eye: work them out from the material and the tool rather than copying them blindly.
How to practise without breaking anything
The best way to build confidence is to write code and watch it run without risking the machine or the tooling:
- CNC simulators: many controls have a simulation mode on the machine itself, and there are desktop and online simulators where you see the path before you cut.
- Dry run mode: running the program with no part, or with the machine locked, to check the movements.
- Writing simple parts by hand: a square, a circle, a row of holes. Programming it yourself fixes the concepts far better than copying.
If you are weighing up a career path, it helps to understand properly what a machinist is and how programming combines with work on the CNC lathe and milling machine. It is a trade in demand with good prospects.