If you’re venturing into the world of CNC and CAM manufacturing, or are just looking into beginners CNC kits, you may well have heard of the term “G-code”. G-code is a programming language for computer numerical controlled (CNC) machines. In this short guide, we’ll introduce you to the basics of G-code, and how to start using it. (Spoiler alert: unlike some other programming languages, you won’t have decipher the meaning of all the letters and digits of G-code to use CNC; you only need to choose the right CAM program to generate it for you!)
How is G-code used in CNC machining?
CNC machinists use G-code to instruct CNC machines where and how to move. The code dictates which direction the machine should move in, how fast it should move, how deep it should cut, and so on. The instructions are described using Cartesian coordinate locations (e.g. two units left and three units up.)
To begin with, an initial block of material will be loaded into the machine. Then, following the instructions given by the G-code, the cutting tool cuts away material from the block to produce the finished product.
G-code is one of the most widely used programming languages used to control automated machine tools. Most CNC machines execute G-code, although other CNC languages exist, such as Heidenhain, Mazak, and other proprietary formats.
CNC machinists can either write G-code from scratch, modify existing G-code, or generate G-code using CAM software. CAM software can generate G-code from either images or CAD files. In today’s extensive CAD industry, there are also CAD editing programs that automatically convert CAD files into G-code. There are also richly-featured G-code editors that can be used to simulate G-code, or to translate G-code into conversational CNC.
How to read and write G-code
A typical line of G-code is quite cryptic for newcomers – it takes years for a CNC machinist to master the language. Whilst the entire language is referred to as G-code, technically speaking a ‘code’ refers to just a single instruction in the language. Each ‘code’ consists of a letter address and a number, and gives a specific instruction to the machine.
Most lines of G-code will begin with the letter G – hence why the language got the name! This is because the letter G signifies preparatory codes. They tell the machine which kind of motion is required, or which offset values to use. The codes beginning with G are therefore almost always found at the start of a line of G-code. G00, for example, tells the machine to move at maximum speed, while G02 tells it to move in a clockwise, circular motion.
Not all G-codes start with the letter G!
Whilst codes beginning with G are extremely common, all 26 letters of the alphabet are used in G-code. S, for example, defines speed, while F defines feed rate. There are some other important basics to know, such as:
- The letter X controls the horizontal position or X-axis of the machine
- The letter Y controls the vertical position or Y-axis of the machine
- The letter Z controls the depth or Z-axis of the machine
- The numbers next to these letters determine the distance moved by the machine
You can view a full list of standard G-codes here. Be aware, however, that codes vary by machine type, make, and model.
G-code File Types supported by Scan2CAD
There are dozens of known G-code file extensions. Which file type your CNC machine will support depends on its setup, as well as the make and model. Scan2CAD supports three of the most popular G-code file types – .CNC, .NC and .TAP. The latest release of Scan2CAD, v9, allows you to directly export a vector image file to G-code file format. Now, you can convert scanned sketches and images to vector using Scan2CAD, before saving it as G-code. These files can then be transferred to a CNC machine and used for production.
The CNC Export dialog in Scan2CAD provides you with a range of options:
- G-code Bezier options – cubic splines (G-code G05), arcs (G-code G02/G03) or polylines (G-code G01). If you select polylines, Bezier curves will be broken into straight-line segments
- Arc and circle rotation – clockwise or anti-clockwise
- Z-settings – various parameters relating to the cutting of the exported vectors
- Scale settings – the relation between vector points and a distance in real-life – e.g. two points on a vector image could represent an inch
- Easy G-codes every machinist must know
- How to go from CAD, Image or STL to G-code
- CNC Programming with G-code for Beginners