Have you ever used flashcards or CDs to learn a foreign language? To learn a new word, you read it from a card or listen to a native speaker say it in the new language, then you get an English translation. I think you can take a similar approach to learning G code, the programming language that controls CNC routers.
G-code is simple, and every machine uses it. Commercial machines come with fancy software that adds some proprietary code to every program, but the basic commands are always the same. I’ll translate some G-code into English so you can see how it works.
Software for CNC Programming
You don’t have to learn G-code to create CNC programs. There’s software that will write the code for you, but it can be expensive. If you learn a few G-code commands, you’ll be able write your own programs without spending a dime. You can create a CNC program in any text editing application. All you have to do is type your code in plain text and save the file with the right extension (.nc or .gcode). There are other extensions that will work, but these two are the most common. Load the file in your CNC control software and run the program.
CNC Phrases
Whether you want to cut a straight line or an arc, drill a hole or route a pocket, you need to use the appropriate command. Think of G-code commands like phrases in a foreign language. Here’s a list of CNC phrases to get you started.
- G0: Move at full speed to a specific point
- G1: Cut a straight line
- G2: Cut a clockwise arc
- G3: Cut a counterclockwise arc
- G40: Cut right down the center of the line
- G41: Cut to the left of the line by the tool’s radius
- G42: Cut to the right of the line by the tool’s radius
- G20/G21: Coordinates are in inches/Coordinates are in millimeters
Syntax
You tell a CNC machine what to do by typing a command, followed by coordinates. If you want to cut a straight line, your code might look something like this:
G1 X3 Y0
In English, this means Cut a straight line that ends at 3” in the X-axis and 0” in the Y. Check out these G-code commands, followed by their English translations.
- G0 X10 Y0: Move full speed to 10” in the X-axis and 0” in the Y-axis.
- G1 X15 Y15: Cut a straight line ending at 15” in the X-axis and 15” in the Y-axis.
- G2 X1 Y1 R0.5: Cut a clockwise arc with a ½” radius ending at 1” in the X-axis and 1” in the Y-axis.
As you can see, some commands require additional parameters. The G2 command above is followed by a parameter for the arc’s radius. You’ll see the parameter P below, which is used with tool radius compensation. Check out this link for more information about G-code parameters.
Tool Radius Compensation
In all of the above examples, the router bit will be centered over the X and Y coordinates. When this is true, the final size of whatever you’re cutting will be smaller than the specified dimensions by the diameter of the bit. To fix this, you have to use a G41 or G42 command, which initiates tool radius compensation. Take a look at these bits of G-code and their English translations.
- G41 P0.125: Compensate for the router bit’s radius by cutting 1/8” to the left of the specified X and Y coordinates.
- G42 P0.125: Compensate for the router bit’s radius by cutting 1/8” to the right of the specified X and Y coordinates.
Example Program
Let’s look at the code required to cut the 4” x 4” part shown above. If this part is 3/4″ thick, we’ll need to make several shallow passes to cut all the way through the material. The following code will give us a 1/8″ deep cut. To get to full depth, we’d simply repeat the code several times, adding -.125 for each pass to the Z coordinate in the second line. The first line of code is what’s called a lead-in move, which gives us room to initiate tool radius compensation.
G0 X-1 Y-1
G1 Z-0.125 (add -.125 to this line for each pass)
G42 P0.125
G1 X0 Y0
G1 X4
G1 Y3
G3 X3 Y4 R1
G1 X0
G1 Y0
G1 Z0.2
G40
Here’s how this translates to English:
- G0 X-1 Y-1: Move at full speed to -1” in X and -1” in Y.
- G1 Z-0.125: Plunge 1/8” into the material.
- G42 P0.125: Compensate for the router bit’s radius by cutting 1/8” to the right of the specified coordinates.
- G1 X0 Y0: Cut a straight line ending at 0″ in X and 0″ in Y.
- G1 X4: Cut a straight line ending at 4” in the X.
- G1 X3: Cut a straight line ending at 3” in the Y.
- G3 X3 Y4 R1: Cut a counterclockwise arc with a 1” radius, ending at 3” in X and 4” in Y.
- G1 X0: Cut a straight line ending at 0” in X.
- G1 Y0: Cut a straight line ending at 0” in Y.
- G1 Z0.2: Retract the bit so that it’s .20” above the material.
- G40: Turn off tool radius compensation.
When using tool radius compensation, think about the direction of the cut. If you’re moving clockwise, cut to the left of your coordinates. For counterclockwise cuts, compensate to the right. You may need to experiment to get the hang of tool compensation.
Speaking/Writing G-code
Practice using the G0, G1, G2, G3 and G40/41/42 commands by writing some simple programs. You can download a G-code viewer here that will let you see the results of your code. Try programming in millimeters by writing G21 in the first line. When you know these commands, you know enough G Code to create all sorts of CNC programs.
Sign up for Build a CNC Router from Popular Woodworking University for complete plans, drawings and instructions for building your own CNC machine. You’ll learn even more about G-code.
Here are some supplies and tools we find essential in our everyday work around the shop. We may receive a commission from sales referred by our links; however, we have carefully selected these products for their usefulness and quality.