Guide to CAD for CNC: What is G-code?

Updated Oct 6, 2018
CAD Guide G Code

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?

What is G-code 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.

G-code sample

An example of a CNC editor, where CAM programmers can edit G-code before manufacturing

What other technologies make use of G-code?

CNC machining isn’t the only process that uses G-code. The same programming language is also used to direct 3D printers. The difference here is that instead of cutting into a block of material, a 3D printer follows G-code to slowly build up material. This technique is also known as additive manufacturing, and can be distinguished from CNC machining, which is a subtractive process. This is because 3D printing adds material on, while CNC machining takes material away.

Another technology that makes use of G-code is photoplotting. A photoplotter is a machine that exposes an image onto media; usually high-contrast monochromatic photographic film. The process is primarily used to create printed circuit boards (or PCBs) and IC packaging.

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.

G-code sample

Try and read this snippet of G-code! It’s a simple drawing of straight lines

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
Scan2CAD CNC Export Settings Dialog Box

The CNC Export Settings dialog box in Scan2CAD. There are four different settings which can be defined.

Recommended reading:

scan2cad advert for free trial