What is G-code, and how do I write it?

G-Code is the backbone of the CNC machining works in modern times. There was a time when every machining work was done by putting in manual effort; now they are done by the commands of a programming language. The programming language is G-Code. It helps you to instruct the CNC about how to move and how to make the final product as per your demand from the raw workpiece. But people who are new to this CNC machining industry are often curious about what the G-Code is and how someone writes it. If you also have even one of the two questions in your mind, then let’s dive deep into it with us in this blog. 

What is G-Code?

G-code is the standardized programming language that instructs the CNC machine about its ways to complete the machining works. These commands guide all the actions of the CNC machine. The G-Code is a set of programs where every letter is followed by a numerical value. 

The “G” in G-code stands for “geometric” or “preparatory” commands, which are responsible for defining the machine’s movements. Other common codes include “M” for “miscellaneous” functions (like turning the spindle on/off or changing tools), “F” for “feed rate” (how fast the tool moves), “S” for “spindle speed,” and “T” for “tool selection.” Each line of G-code typically represents a single instruction, executed sequentially by the machine controller.

Common G-Code Commands

Let’s now dive into some of the most common G-Code commands that you will encounter while using the CNC machines: 

  1. G00 (Rapid Positioning): This command is used to move the machine to a specific location quietly. 
  2. G01 (Linear Interpolation): This command is used to move the machine in a straight line to a specific location.
  3. G02 (Clockwise Arc): This command serves the purpose of moving the machine in a clockwise arc to a specific location.
  4. G03 (Counterclockwise Arc): This command helps in moving the machine in a counterclockwise arc to a specific location.
  5. M03 (Spindle On): This command is used to turn on the spindle.
  6. M05 (Spindle Off): This command helps turn the spindle off. 

Here is a list of the main codes in a chart – 

Command type Example Codes  Description
Positioning G00, G01 Move tool rapidly or in a straight line
Circular Interp. G02, G03 Move tool in a clockwise/counter-clockwise arc
Units & Planes G20, G21, G17 Set units (inches/mm), select working plane
Tool & Spindle M03, S1000 Start spindle, set speed
Feed Rate F100 Set movement speed
Tool Offset G40-G44 Adjust tool length or diameter
Miscellaneous M00, M30 Program stop, end of program

Writing G-Code

Now, here are the steps that you need to follow to write a proper G-Code to make your CNC machine work in the right way:

  1. Select the CAD/ CAM software: To write a G-Code, you will have to select computer-aided design (CAD) and computer-aided manufacturing (CAM) software. The most popular options include SolidWorks, Fusion 360, and Mastercam. 
  2. Design your part: Then you will have to use the CAD software to create the design of the part that you want to create. You can create a 2D shape or a 3D model as per your requirements. 
  3. Generate G-Code: Once you have the design ready, use the CAM software to generate the G-Code for your machining works. This will also involve setting different parameters for the machining works like the toolpaths, feed rate, and spindle speed. 
  4. Edit and refine: It happens that sometimes the code generated has some kind of problems or errors, so you must recheck and refine it to ensure that the machining work is done correctly. 

Samples G-Code program

O1001

G21 G17 G90 F100; Set units to mm, XY plane, absolute, feed rate 100

M03 S1000; Spindle on, 1000 RPM

G00 X5 Y5; Rapid move to (5,5)

G01 Z-1; Lower tool to start cutting

G01 X5 Y15; Linear cut to (5,15)

G02 X9 Y19 I4 J0; Clockwise arc to (9,19), center offset (4,0)

G01 X23 Y19; Linear cut to (23,19)

G01 X32 Y5; Linear cut to (32,5)

G01 X21 Y5; Linear cut to (21,5)

G01 X21 Y8; Linear cut to (21,8)

G03 X19 Y10 I0 J2; Counter-clockwise arc to (19,10), center offset (0,2)

G01 X13 Y10; Linear cut to (13,10)

G03 X11 Y8 I2 J0; Counter-clockwise arc to (11,8), center offset (2,0)

G01 X11 Y5; Linear cut to (11,5)

G01 X5 Y5; Return to start

M05; Spindle stop

G28; Return home

M30; End program

Tips for writing a G-code:

Here are some additional tips from our side about how to write your G-Code to make sure it runs in one shot without any errors:

  1. You can use semicolons, to add comments for clarity. 
  2. Some machines have unique syntax so make sure you check that from the machine’s manual. 
  3. Use the CAM software or machine simulators to dry run the code once before running it on real hardware. 

Common G-Code Errors:

Here are also some of the most common G-Code errors that people make, and you should avoid:

  1. Writing the wrong syntax: Putting up the wrong syntax is one of the most common errors, so use the proper syntax and commands for the right machining. 
  2. Coordinate errors: You should make sure that you select the correct coordinates and units like millimeters or inches. 
  3. Feed rate and spindle speed errors: Make sure that you’re putting the right feed rate and spindle speed, as that error can lead to major accidents.

 

Conclusion

G-code is the fuel for CNC machines, they cannot run without the commands as per the G-code. So it is often very important to learn how to write the proper G-Code and run it in the right way to complete your machining operations perfectly. 

If you are someone who is planning to buy used CNC machines, then you can check our collection at MachineStation. If you can’t find the right fit there, then let us know your requirements in the Machinery Locator and we will try to help you out.