fbpx

What is G-Code? Basics for Beginners [2023]

What is G-Code? Basics for Beginners [2023]

What is G-Code? Basics for Beginners [2023]

G-code is the most popular computer numerical control programming language

These codes are generated automatically by computer software that takes in the design file and converts it into the corresponding G-codes.

But what exactly is G-code and why is it important to learn about G-code?

G-code controls how the tool is going to create the desired part in the safest and most efficient way.

In this article, I have discussed G-codes in detail, including some important commands that are used.

You will be able to develop an understanding of how G-code works, how it is structured, and why is it important for a CNC operator to have knowledge about G-code programming.

What is G-code?

XYZ movements on a CNC Mill
XYZ movements on a CNC Mill

G-code (Geometric code) is a programming language by which a computer controls the movements of a CNC machine or a 3D printer. It defines the parameters like RPM of the spindle, feed rate, coordinates of the toolpath, tool information, fan speed, nozzle and bed temperature, etc.

These parameters vary depending on whether you are working with a CNC machine or a 3D printer.

A CNC programmer is responsible for programming a CNC machine by using either computer software or manually writing the entire G-code for the process.

Generally, G-codes are automatically generated with the help of CAM software, but you can also use G-code editors to tweak and troubleshoot your program.

Every line (also known as a block) of the code tells the machine to perform a specific function, and shapes are created by compiling all these lines of the code together.

A block of G-code can keep running an operation on a loop unless it is instructed to stop or is replaced by another operation. These codes are known as modal codes.

For example, distance mode codes (G90 and G91) are modal codes that select a particular coordinate system for programming the movements of the tool.

G90 code activates the absolute coordinate system and all the commands executed after that will follow the absolute coordinate system unless the programmer activates the incremental coordinate system (G91).

Whereas, non-modal codes are executed only in the block in which they occur, e.g., G04 (dwell).

Structure Of G-code

G-code to engrave "Mellowpine" logo
G-code and toolpath visualization to engrave "Mellowpine" logo (Source: NC Viewer)

A block of G-code starts and ends with the "%" symbol, and the controller will ignore everything after the closing "%".

The space after the closing "%" can be used to add comments to your program.

The first block of information in a G-code is the code number. The code number is enclosed in parenthesis followed by a semi-colon, so the machine knows not to process that block.

The next block is for the operator and contains information like tool number and description of the part. This block is also in parenthesis and is not processed by the machine.

A good G-code must have some essential blocks in a specific order to create the part safely and efficiently.

A basic G code for beginners consists of some basic blocks in the following order:

  • Start
  • Safety block to ensure the machine is in safe mode
  • Loading the tool
  • Turning the spindle ON
  • Turning the coolant ON
  • Moving the spindle above the part
  • Working on the part
  • Turning the coolant OFF
  • Turning the spindle OFF
  • Moving the spindle to a safe location
  • Ending the process.

Like functions in other programming languages, G-code has something called Canned Cycles.

G-code provides a special function called Canned Cycles which can simplify and shorten your code by performing complex operations in a few lines.

Compared to a canned cycle program, a regular G-code would be lengthy and difficult to follow.

You can comment out a block of your code by using the symbol /. This is also called block delete.

You can use this when you want the machine to ignore a block of code without deleting it from your program.

Sample G-code

The example below provides a better understanding of the structure of a G-code.

Here I have considered the first few blocks of G-codes used to engrave the "Mellowpine" logo and explained the function of each line in this sample code.

Block numberCodeDescription
01N01 G21 Unit selection to mm.
02N02 G90Absolute positioning.
03N03 G01 Z2.5400 F2540Move to clearance level (2.5400mm in the Z-axis) at a speed of 2540mm/min.
04N04 G01 X6.2817 Y-4.4417 F2540Move to coordinates (6.2817mm, -4.4417mm) in the X and Y-axis at a speed of 2540mm/min.

Sample G-code

Every block of this G-code starts with an N-code, which defines the block number, and does not play any role in the operation of the CNC machine.

In block number 01, G21 is a dimensional code that sets the units to mm. It is a modal code and will be applicable throughout the code unless the programmer activates an alternative dimensional code.

In block number 02, G90, which is also modal, activates the absolute positioning system for programming. This means all measurements will be calculated with respect to the pre-defined reference (datum) point.

In block number 03, the toolpath moves to a safe clearance level of 2.54mm along the Z-axis, above the workbench.

The next block 04, is to move the tool to its initial position of 6.2817mm, -4.4417mm in the X and Y directions.

In blocks 03 and 04, the F-code sets the traversing speed of the tool. Since the units are set to mm, the spindle moves at 2540mm/min to the given coordinates.

Some Important G-code Commands

Plane selection in G-code
Plane selection in G-code

In the table below, I have mentioned a list of G-codes commonly used for CNC machining.

CommandsInstruction
G00 Rapid positioning: Spindle moves at maximum speed
G01Linear Interpolation: Spindle moves in a straight line at a fixed speed with start and end points defined
G02Circular interpolation clockwise: Spindle moves in an arc in a clockwise direction with start and end points defined
G03Circular interpolation anti-clockwise: Spindle moves in an arc in an anti-clockwise direction with start and end points defined
G20/G21Unit selection: G20 - Inches, G21 - Millimeter
G17, G18 & G19Plane selection: G17- XY-plane, G18- XZ-plane, G19- YZ-plane
G28Return to reference (datum) position. You can also set an intermediate point to avoid a possible collision.
G90 & G91G90- Absolute positioning (measurements taken with respect to pre-defined reference point), G91- Relative positioning (measurements taken with respect to the current position)

Important G-code commands

What is M-code?

M-codes are part of a CNC program that controls the miscellaneous operations of a CNC machine or a 3D printer.

It must be noted that a block of code can only contain one M-code command.

A typical G-code for a CNC program consists of a combination of G-codes and M-codes.

Some Important M-code Commands

Coolant ON in CNC machine
Coolant ON in CNC machine

In the table below, I have mentioned a few regularly used M-code commands.

CommandsInstruction
M00Stop the program
M02 End of program
M03Rotate spindle clockwise
M04Rotate spindle anti-clockwise
M05Stop the spindle
M06Tool change
M08Coolant ON
M09Coolant OFF

Important M-code commands

M-Code Commands For A 3D Printer

3D Printer from ANYCUBIC
3D Printer from ANYCUBIC

A machining operation of a 3D printer is significantly different from other CNC machines.

This is because 3D printing is an additive manufacturing technology, i.e., it creates a part by adding material instead of removing it.

3D printers work by melting material and depositing it layer by layer on the workbed.

It consists of a hot nozzle, heated to the required melting temperature, depending on the material used for the printing operation.

A unique characteristic of 3D printers is the need for support material in certain cases.

This is used where the part has an overhang and needs to be supported to prevent breaking.

Usually, the material used for support is different from the part material and can be easily removed after completing the print.

After completing the print, the part can be taken off easily by heating the workbed to melt the base layer of the workpiece, thereby eliminating the risk of damaging the part.

Hence, 3D printers have significantly different M-codes that control these special functionalities like starting and stopping the heating process, setting the required temperature, adjusting the fan speed, etc.

CommandsInstruction
M104Start heating the extruder
M109Wait till extruder reaches set temperature
M140Start heating the bed
M190Wait till bed reaches set temperature
M106Set fan speed

Some of the most commonly used M-codes for a 3D printer

Additional CNC Program Commands

Apart from G-codes and M-codes, there are additional commands that define other system parameters.

F-codes define feed rate, i.e. at what speed the tool is going to move to the next coordinates.

Depending on what G-code command (G20 or G21) is used, units of feed rate will be selected, i.e. mm/min or in/min.

Every tool has a number assigned to it, and T-codes are used when an automatic tool changer is used.

The spindle RPM required for a machining process is defined with the help of S-codes.

N-codes are used to denote the block number. These codes are solely for the ease of programming and do not affect the machining process.

How Does G-Code Work And Why Do You Need To Familiarize with G-codes?

CNC machining generally consists of various phases where the designer created the design and computer software (CAM) translates the design into corresponding G-codes.

These G-codes are fed into the controller that converts it into electrical signals to operate the stepper or servo motor drivers.

There may be an additional amplifier stage between the controller and the motor drivers to prevent any electrical noise from affecting the command signal.

Although computer software can automatically convert SVG files to G-code, it is important to familiarize yourself with G-code programming to tweak or troubleshoot the code when needed.

Sometimes the automatically generated code may not be optimized for safety and efficiency. In those cases, you can make certain changes to the code to suit your requirements.

Frequently Asked Questions

Is G-code Programming Difficult To Learn?

G-code is a logical language that consists of a handful of commands that are repeatedly used. If you have an understanding of the working of a CNC machine, it will not be hard for you to learn G-code programming.
Creating complex geometries by using G-codes requires practice and experience. However, you can use CAM software that automatically creates the G-code for such complex designs.

Should you perform a test run of your G-code before attaching the part?

Yes, you should always perform a test run of your G-code before attaching the part into your machine to check whether the toolpath is what you expected it to be. If the toolpath is not correct, it may result in a collision, thereby damaging the workpiece or the tool. You can also use dedicated software that run a simulation to verify the toolpath.

Are M-code commands the same for all CNC machines?

No, M-code commands are not the same for all CNC machines, unlike G-codes, which are universal. M-codes are customizable, and different CNC machines may have different M-code commands. You need to go through the machine manual to know those commands.

What are some of the best G-code editing software?

Some of the best G-code editing software are NC Viewer, G-Wizard Editor, CIMCO Edit, G-code Q'n'dirty, Notepad++, and Visual Studio Code.

About John

Hey I'm John. I write about Manufacturing, Metalworking, CNCs and Lasers at Mellowpine. If you have any questions related to CNCs or Lasers, I'd be happy to answer them. Reach me at mail@mellowpine.com

DIY Profile
John

Hey I'm John. I write about Manufacturing, Metalworking, CNCs and Lasers at Mellowpine. If you have any questions related to CNCs or Lasers, I'd be happy to answer them. Reach me at mail@mellowpine.com

Comments

The comments are closed.