You are on page 1of 4

Basics of CNC Part 1 – G Code

A program starts with various code to set the machine up – Inch/Metric, Absolute/Incremental coordinate system, etc. This varies a bit from machine to machine. After that, the fun starts (the machine starts moving). The main thing you should know about G-code is that each line is an instruction that “pulls” the machine from [wherever it is] to a new coordinate. Along with the new position, you can describe the shape of the path (linear or in an arc) and the speed at which to travel – a.k.a. feed rate. That's pretty much it. Of course that's never really true though is it? The fact is, there are tons of things and several ways you can tell a CNC machine to do something and it can become fairly complicated if you want it to. Luckily, most things can be accomplished with pretty simple code. Here are the basic commands in a nutshell. G0 (G zero, not “go”) This is the command for rapid position. With this command, the machine motors will travel as fast as they are set up to move toward whatever coordinate you attach to it. For example, if the machine is sitting at coordinate X3.0 Y2.0 Z-2.5 and you command: G0 X0 Y0 Z0 it will simply move as fast as the motors are allowed to spin, stopping at X0 Y0 Z0. This is a pretty uninteresting command but it will be used all the time. G1 Linear interpolation. This command will coordinate the motors to move the machine in such a way that it travels from [wherever it was] in a straight-line path to the new position. You also need to tell it how fast to travel along this path. i.e. machine is sitting at X0 Y0 Z0 and you command: G1 X4.0 Y4.0 F10.0 The machine will travel in a straight line at 45º toward the point (4,4) at a speed of 10 inches per minute. Note that there is no Z coordinate included here. That means that only X and Y will change and Z will stay wherever it was before the new command. No new info = no position change. Side note: Also note that when I specified zero above, I just wrote 0 with no decimal point. All controls understand what zero is and a decimal is never necessary. However, some controls interpret values differently – some read “4” to mean .0004 and some to mean 4.0 – for this reason, I always write a decimal point when specifying values other than zero so that it can be understood by any machine. It's just good practice. You can leave off the trailing zero (I usually do). Machine controls interpret 4. and 4.0 to mean the same thing, but just to be clearer I'll leave the trailing zero on for this presentation (unless I forget). So G0 and G1 are the two most basic motion commands.

Now to get a little more meaty. So the exact same command can be written: G3 X4.0) at 20 IPM G2 X3.0 Center Point method: You specify the arc's center point and the machine figures out the radius.0. Y and Z.what will happen with the following command? G3 X4.0 – will travel in a CW arc shallower than the one above and end at (3.0 J0 F20..0 (again. 1) You can just tell it a radius and it will figure out the rest. but the center point is an incremental distance from the starting point. it is sometimes easier to understand what's going on in your code if you go ahead and specify all values though whether they have to be included or not.0 F15.0 Y0 I1. With this method.0 .0 Y0 R3.0) G3 X4.0 Y0 I1. assuming the machine is starting out at X0 Y0 Z0) Until you get used to it.0) – trick question .0 J2.0 F20. You can also omit the Y value here as well as the previous examples since its value does not change.0 I2. starts and ends at zero. Either I or J can always be omitted if its value is zero. G2 X3.0 – will travel in a CW arc shallower than the one above and end at (3.0 – CCW semi-circle ending at (4.0 – CCW semi-circle ending at (4. but since it's something new to wrap your head around. You often need to mill arcs and circles. but it is limited to arcs of 180º or less.0) Note that J can be omitted here.0) at 20 IPM G2 X3. we will assume the machine is sitting/starting at the point X0 Y0 Z0 Radius method: You specify the radius and the machine figures the rest. There are a couple different ways to do it. i. In this case. This is ultimately simpler than dealing with a center point's absolute position. However. These are covered by the next two commands.0 Y0 R1.0 F20. J and K. Try to draw a sketch of what paththis code will make (again.0 F15.5 J0 F20. This is a little tricky at first. you command the end point as usual.5 F20.0 Y0 I2.0) G3 X4. 2) You can tell it the arc's center point and it will do any arc between 0º and 360º For the following examples. assuming machine starts at (0.0): G3 X4. it may be a little tricky until you get used to it. but you can get the hang of it pretty quickly.0 F20.0 – will travel in a CW semi-circle and end at (3. G2 – Clockwise circular/helical interpolation G3 – Counter-Clockwise circular/helical interpolation These commands will coordinate the motors of the machine to move in a smooth circular path.0 Y4.0 – will travel in a CW semi-circle and end at (3. omitting both of them will “error” the machine.0 Y0 R1.e..0 How about this one: G2 X0 Y0 I2. G2 X3.5 J-1.0 I2.0 F20.0 F20.0 Y0 R2. the incremental distances are described with the letters I. which correspond respectively to X.

In fact. lets just assume we need to make a threaded hole for an antique camera lens which is a little larger than 2” diameter and has really fine threads.1 (assume the top of the plate is Z0 – standard practice) G0 X-.2 I.75 Y0 Z-0. Lets walk through this interesting example.1 I.0 There! Now you have cut out a 2” diameter circle from the plate.0).0 ┘ G0 X0 Y0 G0 Z1. Note that because the cutter is 1/2” diameter. G2 X0 Y0 Z -. Cake. Bring the cutter close to the work.75 F10.0 ┐ G3 X-. The machine will coordinate the motors to move X and Y in an arc. First. you can use helical interpolation to cut threads. Think of it maybe as one revolution of a bolt thread.75 Y0 Z-0. use a standard end mill to cut a basic hole. Maybe you need a large threaded hole of a strange size in a plate to fit a camera lens to it. It not only involves trigonometry.75 Y0 Z-0. Pretty easy – just add a Z value (different than the starting Z point) and it follows a helical path. we can command a few lines of helical interpolation to rough out the hole. The actual dimensions get technical and you can look it up in a book or on the web.0 F15. and Z will move steadily toward its end point all along this arc path. Pull back out.5 I2. But to make it simple. but Z will move down 1/2” by the time it reaches the end point.0 (safely feed to just be touching the plate) G3 X-.Helical Interpolation: This is simply a circular interpolation with the addition of some Z motion.75 F10. Lets assume the hole needs to be 2” diameter and the center is at (0. Using a 1/2” diameter end mill. Lets assume our plate is 1/4” thick. Use circular interpolation to cut the hole.5”. (continued on next page) .0 ├ series of helix paths to cut out a circle G3 X-. G0 X0 Y0 Z. the circular path needed to make a 2” hole will be 1.0 This will be the same as the last “test” example on the previous page. with the correct cutter.75 Y0 (position to the edge of our hole) G1 Z0 F5. but also clearance issues and thread crest widths and all sorts of other thread form geometry. then use a 60º V-shaped cutter to make the threads. Move it to the edge of the hole we will cut.75 F10.3 I.

525 F10. G0 X0 Y0 Z.16 I.28 I. Remember our thread pitch is 25 TPI or .0 ┐ G3 X-.0 ├ series of helix paths to cut out our threads G3 X-.machsupport.0 │ G3 X-. Hopefully more fun to come in the future.040” (or 25 threads per inch). Our code will be very similar to what we used to cut the hole.1 G0 X-. Lets assume it swings a diameter of 1 inch.com/documentation.525 F10.025” deep) and our thread's pitch can be .525 Y0 Z-0. .php http://www.525 Y0 Z-0. Our finished diameter will be 2” + 2X the thread depth of .12 I.php We're just scratching the surface.0 There ya go. Looks much better than epoxy and duct tape too.525 Y0 Z-0. so 2. Cheap solution. You can download their user manual and see instructional videos on their site for lots more information.525” radius.525 Y0 Z-0. Most anybody in the DIY crowd will be using Mach3 for their control.525 F10. Well that's enough G-code for now.525 F10.04 I.525 F10.050” is our target diameter.525 F10.0 (feed to a point a little above our plate) G3 X-.040” per revolution.20 I.24 I.0 │ G3 X-.0 │ G3 X-.04 F5.0 │ G3 X-. so the path it needs to follow is a circle of 1.Now change the tool to a 60º fly cutter.525 Y0 Z-0. Our cutter is 1” diameter. Used two cheap tools to cut a weird threaded hole that you'd have to pay probably $300 to have some shop take a month to make a tap for you to cut it.050” diameter = .machsupport. We need it to cut a little into the sides of the hole to make threads (lets say .525 F10.025”.525 Y0 Z-0.0 │ G3 X-.com/videos.525 Y0 Z0 I.08 I. http://www.525 Y0 Z-0.525 Y0 G1 Z.525 F10.0 ┘ (last move to go a little past the bottom of the 1/4” plate) G0 X0 Y0 G0 Z1.