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's in this article?
What is G-code?
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.
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
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.
The block order is as follows:
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 coolant OFF, turning spindle OFF, moving spindle to a safe location, and 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.
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.
|01||N01 G21||Unit selection to mm.|
|02||N02 G90||Absolute positioning.|
|03||N03 G01 Z2.5400 F2540||Move to clearance level (2.5400mm in the Z-axis) at a speed of 2540mm/min.|
|04||N04 G01 X6.2817 Y-4.4417 F2540||Move to coordinates (6.2817mm, -4.4417mm) in the X and Y-axis at a speed of 2540mm/min.|
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
In the table below, I have mentioned a few regularly used G-code commands.
|G00||Rapid positioning: Spindle moves at maximum speed|
|G01||Linear Interpolation: Spindle moves in a straight line at a fixed speed with start and end points defined|
|G02||Circular interpolation clockwise: Spindle moves in an arc in a clockwise direction with start and end points defined|
|G03||Circular interpolation anti-clockwise: Spindle moves in an arc in an anti-clockwise direction with start and end points defined|
|G20/G21||Unit selection: G20 - Inches, G21 - Millimeter|
|G17, G18 & G19||Plane selection: G17- XY-plane, G18- XZ-plane, G19- YZ-plane|
|G28||Return to reference (datum) position. You can also set an intermediate point to avoid a possible collision.|
|G90 & G91||G90- 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
In the table below, I have mentioned a few regularly used M-code commands.
|M00||Stop the program|
|M02||End of program|
|M03||Rotate spindle clockwise|
|M04||Rotate spindle anti-clockwise|
|M05||Stop the spindle|
Important M-code commands
M-Code Commands For A 3D Printer
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.
|M104||Start heating the extruder|
|M109||Wait till extruder reaches set temperature|
|M140||Start heating the bed|
|M190||Wait till bed reaches set temperature|
|M106||Set 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.