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.