How to Write G Code (basics)




Introduction: How to Write G Code (basics)

About: skills usa gold at regional and state level cnc turning, 2 years in trade school and 1 year on the job working with turning centers :)

The basics of writing G code and some M code

Step 1: Write the Introduction

Start the first line with a '%' then press enter. The next line should start with a capital 'o'. If the character there is a zero the machine will throw an error code. After the capital O and with no spaces there should be 4 or 5 letters. This is called the program name.

Step 2: Saftey...Second?

Once the program has a name its time to think about safety, the third line. It will be in rapid so we program 'G00' this tells the machine that it should move as fast as it can or is programmed to. Following that I have:
G90, for absolute position mode.
G80, to cancel any previously used canned cycles.
G28, to rapid to the home position.
G17, to select the x, y circular motion field.
G20, to select the inch coordinate system. (G21, to select metric)
G40, to cancel cutter compensation.
G49, to cancel the cutter height compensation.
Now this leaves us safely at the machine home position.

Step 3: Get Your Tools Ready

This step is for people lucky enough to have an automatic tool changer (ATC). First type in the letter 'T' and follow it with a number. This should look like this 'T01' or possibly just 'T1'.
On cnc lathes it will be trailed by an off set number like 'T0101'. This calls up tool number 1 and applies off set 01 to it, before a tool change you'll need to program 'T0100' to clear the offest before the next tool. Haas lathes dont use the offset number, instead they use the mill format 'T01' and 'T1'.
So far we've brought the tool changer to position #1 but the tools not in the spindle yet. M codes tell the machine what to do, M01 is an optional stop, M03 starts the spindle. For this we'll use M06, this specifies that the machine will trade the tool it currently has with the tool in the currently called up ATC position.
On the lathe M06 spins the turret to that position.
The line after this is where to put an M01 if you dont have a turret or ATC.

Step 4: Getting Closer

Here's where I leave the lathe alone and focus on mills. Specifically lets look at G41/G42 and G43/G44
G41 and G42 are similar in their uses but not in their functionality. G41 is tool radius off set to the left of the center. G42 is to the right. They position the tool so that on contouring operations any number of end mills can be used without rewriting the program and doing the math yourself. All you have to do it tell the machine 'D01' or whatever tool number needs the off set.
G43 and G44 offest the length of the cutter the positive(G43) and negative(G44) directions. This is for multiple set ups using different cutters or the same cutter on a different cutting plane. Primarily G43 will be used. This needs a height command such as 'H01' or whatever tool number needs the offset.

Step 5: Common Commands

G00, rapid move to a position
G01, linear cut from current position to programmed X, Y, Z
G02, moves the cutter along a clock wise arc until the current position is the new programmed position
G03, moves the cutter along a counter-clock wise arc until the current position is the new programmed position
G04, just like G01 but with a dwell command
All these commands are modal meaning that the machine will keep going rapid until a different one is programmed or will keep cutting unless G00 is called.

Step 6: Explore!

There are many cool commands, including canned cycles. Im too lazy to explain all of them because there are more than 180 G codes and machine depending there are 100 or more M codes. Look up more for yourself or get a machine manual for your machine. With Cnc machines the options and opportunities are endless!

Step 7: If I Had a 3D Printer

Firstly I'd like to say that if this taught anyone interested in cnc machining something new I'm happy I could help. Secondly if you liked it please like this ible.
So now if Iwon a 3D printer I would create prototypes of things I have created in my head and post them on here to share what I know and hopefully learn a bit from others.



    • Creative Misuse Contest

      Creative Misuse Contest
    • Metalworking Contest

      Metalworking Contest
    • Fix It! Contest

      Fix It! Contest

    18 Discussions


    Question 2 months ago

    How to make g code for 2d cnc wire bending.

    can someone please email this document? i don't have premium access..

    can someone send the this pdf via email? i dont have premium :/

    you are not lazy. you are looking business that's way don't explain about us. anyway thank you.

    I have a question. Currently in school and still learning so im still green. Got thrown a little curve ball in class. Been learning programming on a Mori Seki lathe. And our instructor(who is now out tell january and understands lathe programing better then his adjuncts) decided to give us a program to write for a sl-10 haas lathe. Kinda understanding it but he had a specific insctruction thats making this program suck. Need to write a g03 line for a g71 roughing cycle from the face of the part that contours into a R.031 x.250 z.250 then goes into another R.031 and on into the rest of the program. Any suggestions on that first radius from the face of the part?

    1 reply

    you know actually a strange thing with Haas controllers is for some reason every once in a while in a g02/03 move it likes the raduis over sized. I'd bet you're running into a "non-monotonous path in pq block", right? I don't remember why this is a thing I just remember we had one project in school that this happened on.

    i want to make a chess piece on a 3 axis hass milling machine. i have a production plan but will you write me a programme

    1 reply

    sorry I won't write it for you however I will look over code you write and tell you if it looks good. Also I could point you towards a program called Discriminator NC, it interprets your G code and plots it on an X,Y,Z coordinate. If I remember right its good for 4 axis machining. Keep in mind I am 100,000 times better on CNC lathes than mills.


    3 years ago

    feel free to ask any cnc programming questions and I'll either edit the ible or answer right in the comments!

    7 replies

    What does the last picture say. Thanks. :)

    the last picture says exactly what the second to last picture says only the last line is repeated with a G03 instead of G02

    sorry I know I really have no business with a camera :P

    naw it's the cameras fault if anything (Your camera skills are fine). Thanks for the help too this is a great instructable.

    I am currently a turret operator and I'm looking to start writing my own programs so I can advance in my job. What is the best way to start out learning to write my programs? Is there a book you would recommend?

    Not shure where to add this,I have a chinese derk top CNC machine and I want to learn how to opperate it,Mostly carving and engraving Hoping you can help please

    sorry I must not have seen your comment.
    for engraving I would use a cad/cam there are many free and open source cad/cams. I would typically use g and m for more simple geometries. unless you plan on making simple things its usually more rewarding to use a cad/cam

    I would recommend a trade school, that's where I was taught. we read from a few different books I would recommend two books, the manual for your machine and "CNC programming" by Peter Smid.

    Good question Omer I myself would use
    G47 P0 (Text to engrave)
    G47 is a canned text cycle and P0 is a literal text setting
    everything inside the brackets will be engraved text height width and spacing would be determined by settings on your machine controller. If you require more control you can loon more into the G47 command or G and M it with G01 G02 and G03.