# Maze Using CNC M &G Code

15,577

50

4

Published

## Introduction: 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.

## 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.

## Step 2: Preliminary Drawing

Before starting to code, some preliminary steps are needed. First you need to get some grid paper out and measure off the outline of the block of wood using 1/4 of an inch increments. For example, the block of wood is 3.5 x 6 inches.

Steps:
Draw any design on grid paper to get the points. Try to keep the ends of the lines and center of the circles on a grid so that everything is easier to calculate.

set the bottom left corner to be 0,0 . find the X Y coordinates of every point that is needed for the CNC to follow the path including the center of the of circles and arcs.

## Step 3: Table of Absolutes

All the points found for the picture need to be ordered from start to finish the way they will be cut. See the picture.
Arcs are the most confusing. On the table of absolutes, the line with the start of the arc should also have the distance from the start to the center of the arc in the I and J values. Then the next line should have the coordinates to the end of the arc. When coding the center of the arc is on the same line as the end of the arc, but the coordinates are the same as here. It will be more clear after I give example of the code.

## Step 4: Start Code Example

This is my example of start code. Everything after a ( is a note and will be ignored.

G90 (set to absolute coordinates
G70 (set to inches
M06 T1 (set to tool #1, don't have tool changer
G92 X0.0 Y0.0 Z1.0 (set home position
M03 S2000 (turns spindle on with a speed of 2000 rpm

## Step 5: Body Code

This is the body code to the map, I only had enough time to do one drill bit at a time so instead of tool changing in the middle of my code, I have different files for the different parts of the board. It also helps with organization to have the different parts in their own file so it is easier to find mistakes. I labeled some of the points to help correlate with the drawing and table of absolutes.

The notes explain the line above them

G00 X0.75 Y1.5 Z0.25
(point 1 not touching yet, G00 is a fast movement to the coordinate (.75, 1.5, .25)
G01 Z-0.125 F8.0
(start cutting into material, G01 is a cutting code it says to go down so Z is -.125 at a speed of 8
G02 X0.5 Y1.75 I0.0 J0.25
(first arc from the first point to (.5, 1.75) with the center of the arc (0, .25) from the starting point
(notice that because there is no change in Z we can omit it
G01 Y2.0
(POINT 3, when switching to drawing a line G01 needs to be there or it gets confused
(all it says is Y2.0 because X and Z are assumed to be the same
X.75
(It assumes the same machining code until another one is given, it is still doing straight cuts at a speed of 8 moving to X=.75
X.5
Y2.75
G02 X0.75 Y3.0 I0.25 J0.0
(arc from previous point (.5, 2.75) to (.75, 3) with a center point (.25, 0) from start point.
G01 X5.25
(State G01 again because back to straight line, and the point is going to X=5.25 with constant Y and Z
G02 X5.5 Y2.75 I0.0 J-0.25
(arc from the previous point to (5.5, 2.75) with the center of the arc (0, -.25), (down 1/4 inch) from the start
G01 Y0.5
(POINT 10, G01 again because switching back to straight line
Y1.0
X5.0
X5.5
Y1.75
X4.25    (POINT 15
Y1.5
Y1.75
X3.75
Y1.5
Y1.75   (PT 20
X3.5
X3.75
Y2.5
X4.5
Y2.25
Y2.5
X5.0
X3.0
Y2.25
Y2.5   (PT 30
X1.0
X1.25
Y2.25
Y2.5
X2.25
Y1.75
X2.75
X2.5
Y1.5
Y1.75   (40
X1.75
Y1.5
X.75
Z0.25
G00 X0.0 Y0.0 Z1.0
(take bit 1 inch above board  now the return to start code doesn't have to worry about cutting the board

## Step 6: End Code

Not much to it, turn off the drill, stop the code and end the job.

M05 (turns off the spindle
M02 (stop the code
M30 (end the job

## Step 7: Full Maze Code

Almost done now with a few less comments this is the full code I used to cut the map of the maze.

G90 (set to absolute coordinates
G70 (set to inches
M06 T1 (set to tool #1 don't have tool changer
G92 X0.0 Y0.0 Z1.0 (set home position
M03 S2000 (turn spindle on
G00 X0.75 Y1.5 Z0.25 (point 1 not touching yet
G01 Z-0.125 F8.0 (start cutting material
G02 X0.5 Y1.75 I0.0 J0.25 (first arc
G01 Y2.0   (POINT 3
X.75
X.5
Y2.75
G02 X0.75 Y3.0 I0.25 J0.0 (arc
G01 X5.25
G02 X5.5 Y2.75 I0.0 J-0.25 (arc
G01 Y0.5   (POINT 10
Y1.0
X5.0
X5.5
Y1.75
X4.25    (POINT 15
Y1.5
Y1.75
X3.75
Y1.5
Y1.75   (PT 20
X3.5
X3.75
Y2.5
X4.5
Y2.25
Y2.5
X5.0
X3.0
Y2.25
Y2.5   (PT 30
X1.0
X1.25
Y2.25
Y2.5
X2.25
Y1.75
X2.75
X2.5
Y1.5
Y1.75   (40
X1.75
Y1.5
X.75
Z0.25
G00 X0.0 Y0.0 Z1.0 (take bit 1 inch above board
M05 (turn off spindle
M02 (stop
M30 (end

## Step 8: Drilling Code

Most of this code is covered in the previous steps. What you want to notice here is how G81 and G80 works.

G90 (set to absolute coordinates
G70 (set to inches
M06 T1 (set to tool #1 don't have tool changer
G92 X0.0 Y0.0 Z1.0 (set home position
M03 S2000 (turn spindle on
G00 X0.25 Y2.0
(point 1 NOT TOUCHING
(notice first hole coordinate is the point before the G81
G81 Z-0.28 R0.25 F8.0 (drill goes into material at hole 1
X1.5 Y3.25 (hole 2
X4.25 Y3.25
X5.75 Y2.25
X4.25 Y1.5
G80
(holes go from one to the next until the G80
(the G80 stops the holes
G00 X0.0 Y0.0 Z1.0 (take back to home position
M05 (turn off spindle
M02 (stop
M30 (end

## Step 9: Name Code

Here is code i used to write Bill Reen if you need more examples. There is nothing special in the code besides lifting off the board between letters.

G90 (set to absolute coordinates
G70 (set to inches
M06 T1 (set to tool #1 don't have tool changer
G92 X0.0 Y0.0 Z1.0 (set home position
M03 S2000 (turn spindle on
G00 X0.25 Y0.25 Z0.25 (point 1 NOT TOUCHING
G01 Z-0.03 F8.0 (drill goes into material
Y1.25  (POINT 2
X0.5   (POINT 3
G02 X0.5 Y0.75 I0.0 J-0.25 (an arc from point 3 to x.5 y .75
G01 X0.25
Y0.25
X0.5
G03 X0.5 Y0.75 I0.0 J0.25 (an arc for bottom of B

G00 Z0.25   (LIFT OFF BOARD FAST
X1.0 Y0.25  (MOVE FAST TO BOTTOM OF I
G01 Z-0.03   (DRILL INTO BOARD
Y0.75       (TOP OF I

G00 Z0.25   (LIFT OFF BOARD FAST
X1.25 Y0.25  (MOVE FAST TO BOTTOM OF L1
G01 Z-0.03   (DRILL INTO BOARD
Y1.25       (TOP OF L1

G00 Z0.25   (LIFT OFF BOARD FAST
X1.5 Y0.25  (MOVE FAST TO BOTTOM OF L2
G01 Z-0.03   (DRILL INTO BOARD
Y1.25       (TOP OF L2

G00 Z0.25   (LIFT OFF BOARD FAST
X2.0 Y0.25  (MOVE FAST TO BOTTOM OF R
G01 Z-0.03   (DRILL INTO BOARD
Y1.25       (TOP OF R
X2.25
G02 X2.25 Y0.75 I0.0 J-0.25
G01 X2.0
X2.26
X2.5 Y.25

G00 Z0.25   (LIFT OFF BOARD FAST
X3.0 Y0.25  (MOVE FAST TO BOTTOM OF E1
G01 Z-0.03   (DRILL INTO BOARD
X2.75       (SIDE OF E1
Y.5
X3.0
X2.75
Y0.75
X3.0

G00 Z0.25   (LIFT OFF BOARD FAST
X3.5 Y0.25  (MOVE FAST TO BOTTOM OF E2
G01 Z-0.03   (DRILL INTO BOARD
X3.25       (SIDE OF E2
Y.5
X3.5
X3.25
Y0.75
X3.5

G00 Z0.25   (LIFT OFF BOARD FAST
X3.75 Y0.25  (MOVE FAST TO BOTTOM OF N
G01 Z-0.03   (DRILL INTO BOARD
Y0.75       (TOP OF N
X4.0 Y0.25
X4.0 Y0.75
Z1.0

G00 X0.0 Y0.0 Z1.0 (take the bit 1 inch above board
M05 (turn off spindle
M02 (stop
M30 (end

## Step 10: Test the Design

To test the design, run it through a CNC simulator. The one I used is for the Davinci CNC machines it is called Preview 2.0 but I can't find anything else about it and it may not work well for your machine. Any preview program should work. A nice free previewer is called CncSimulator, it shows the cnc machine and the bit in a 3d model, you can past the code straight into the code panel or code in the program. Read This Tutorial to get the most out of CncSimulator.

## Step 11: Machining the Design

Now that the design is tested it is time to machine it.
The basic idea of machining it is to open the program for the CNC machine that is on the computer. In the program should be a jog option so that the center of the bit can be placed on the corner of the board. This project doesn't need very tight tolerance so we don't need centering tools just eye ball where the corner is. Center the point to (0, 0, 0).

You should do a test run above the board before cutting into it. in a normal run we start with the bit an inch off the board, it is in the example code. However, for the test run we will have the machine jog to position z=1.5 from the board. Now the drill should not cut into the board and you can make sure the holes don't go to deep, hit anything, or that you have the axis backwards.

After you're satisfied with the practice run you can jog the bit down .5 inches or recenter it on the corner and jog it up an inch.

Remember to wear safety glasses even if there is a shield and to have a hand on the stop button in case of emergency.

## Step 12: CNC Results

Here is the board finished along with other boards designed with the same process.
next step is to put a small ball bearing in the maze and plexiglass on top so I can give as a birthday present.

## Step 13: Plexiglass

Cutting the plexiglass top can be done with a laser cutter or CNC machine but because there are only 2 lines to cut I will use a Band saw. Trace the board on the corner of the plexiglass then cut it out. Last step is to use some small screws to attach it to the board.

## Recommendations

• ### FeatureCAM for Lathes Class

566 Enrolled

• ### Planter Challenge

We have a be nice policy.

## Questions

have you ever tried using masterCAM?