Instructables
Picture of Maze using CNC M &G Code
There are 2 ways to use a CNC machine. Model your design, and have the computer magically convert it to code that the CNC Machine can read but you can't, or learn the basics to CNC M & G code so that you know whats going on and can troubleshoot problems. It's like coding anything on the computer if you only rely on the magic program to code you're doing yourself a disservice.

Sure with advanced shapes you'll most likely rely on the magic box more, but this tutorial is like the hello world of CNC, the first step to learning any new coding language, the NOTEPAD step.

The machine used for this instructable is a Davinci from my school's shop.


 
Remove these adsRemove these ads by Signing Up

Step 1: Basic Code and Definations

Starting Code:
Some of the starting code is an either or code, inches or metric, absolute or incremental coordinate system. Then there is the code that tells the machine what to do like set the tool number (some machines have more the one tool at a time), turn the drill on, and drill speed.

G90 or G91 = Absolute or Incremental Coordinates.
Example: If there were an XYZ coordinate grid and you knew all the points as (X,Y,Z) absolute would work best. You would tell the machine the coordinates to point 1 then the coordinates of point 2, and the the machine would go from one to the next. On the other hand if you knew your first point was (0,0,0) and your next point was 5 inches on the X axis from there, then the next point was up an inch and 2 inches over on the Y axis using Incremental Coordinates would be fastest. You would give each point as a distance from the previous point.

G70 or G71 = inches or metric
G20 or G21 = inches or metric
Different companies adopt different code for their machines. These are the most common and some machines can use either. It is an easy thing for the magic program to mess up if you forgot to click the proper CNC machine in the settings. It works as it looks if you use G70 or G20 the machine will read the coordinates as in inches, if you were to measure the coordinates in inches and use a G71 or G21 it does not automatically convert to metric, it would read the coordinates as 2 mm instead of 2 inches.

G92 X# Y# X#
Set the home position at the current position, the # are variables I usually keep the home position with the bit an inch in the air so that they're easy to change out. My G92 line looks like G92 X0.0 Y0.0 Z1.0

M06 T#
This is the bit number. Some machines can use different bits and switch them without human input. The machine I learned on does not have multiple bits. However, I still use it because it's good practice and allows the CNC to pause one last time before starting cutting (I always find my mistakes after pressing START).
 
M03 S#
Sets the spindle, drill bit, speed. The Davinci CNC has a variable speed knob on the router that can be changed as the part is cut so it doesn't recognize this command either.

G43 H#
Tool Length Composition tells the machine how long each bit is. It is not needed for just one bit.

Body Code:
This code holds the majority of the project. Here will be every drilled hole, every straight line and arc. The body of code can get very long and complex. For example, every switch from an arc cut to a straight line needs to be labeled.

G00 X# Y# Z#
Move at a rapid rate to the coordinate. Use when the bit is in the air, do not use for cutting.

G01 X# Y# Z#
Straight cut to the coordinate X# Y# Z# from the previous position.

G02 X# Y# Z# I# J# K#
Clockwise circle from the previous position to X# Y# Z#. The center of the circle is I# J# K#. The center of the circle is ALWAYS in Incremental Coordinates from the previous position. See more on this later.

G03 X# Y# Z# I# J# K#
Counter clockwise circle that follows the same rules as G02.

G81 Z# R# F#
Drill cycle, have the bit above the hole position before using code. Z# is the depth of the hole, R# is how high above the part the drill bit goes after cutting the hole so it can move to the next hole without touching anything, and F# is still the speed of the cut. It makes for quick drilling of multiple holes because it includes the down and up part of the drill cycle.

G83
Deep drill cycle that includes pecking, lifting the drill out so the chips don’t clog the hole.

G80
Ends the drill cycle. Between G81 or G83 and the G80 code all that is needed for a hole is the X# Y# location. It makes for quick drilling of multiple holes. G81 and G83 includes the down and up part of the drill cycle so at every coordinate between starting the G81/G83 and the G80 there will be a hole drilled.

Finish Code:
Almost done! This code includes returning to home so that the machine doesn’t have to be zeroed between parts, turning off the spindle, and stopping the machine.


G28
Return to home so that the machine is back at zero. Using the G20 is a shortcut to having to look up the home position.

M05
Turns the spindle off.

M02
Stop the machine. Some machines want both the M02 and M03 code to be fully finished

M03
End, now everything is over ready for next part.


Before starting here are some tips:
While coding you should keep your caps lock on, the machines don't always recognize the lower case the same as a capital.
All the coordinates should include the decimal place ex: 0.0 because some machines are picky and get confused
The preview program might show the code working but because the preview works for more than one machine, it is less picky.
0's and O's look about the same in note pad, you can change the default font to make it easier to tell apart.
 
have you ever tried using masterCAM?
W1LL7 (author)  TwistedJack1 year ago
I'm learning featureCAM now.
lots of great info here! does this apply to other cnc machines besindes the davinci?
W1LL7 (author)  amandaghassaei1 year ago
Mostly. Different companies use a little different syntax and some are more picky then others but this should do the trick on most of the machines. For example I included 2 different codes for inches so if one doesn't work the other should.
G70 or G71 = inches or metric on older machines
G20 or G21 = inches or metric on newer machines