Learning G-code is much easier when you can see it in action. This example program will walk you through a simple G-code script that moves a CNC tool to cut a square. Along the way, we’ll explain each command so you can understand how it all works.
Objective
Create a square with sides of 50 mm, starting at the origin (0, 0), using a CNC machine. The tool will move to the starting position, cut the square, and then return to the home position.
Example G-Code Program
Here’s the complete program:
G21 ; Set units to millimeters
G90 ; Use absolute positioning
G17 ; Select XY plane for cutting
G00 X0 Y0 Z5 ; Rapid move to the starting position above the material
M03 S1000 ; Start spindle clockwise at 1000 RPM
G01 Z-1 F100 ; Lower the tool into the material
G01 X50 Y0 ; Cut to the first corner of the square
G01 X50 Y50 ; Cut to the second corner
G01 X0 Y50 ; Cut to the third corner
G01 X0 Y0 ; Return to the starting point
M05 ; Stop the spindle
G00 Z5 ; Raise the tool above the material
G28 ; Return to the home position
M30 ; End program
Step-by-Step Explanation
1. Setup Commands
G21
: Sets the units to millimeters. If your machine uses inches, replace this withG20
.G90
: Enables absolute positioning, meaning all coordinates are measured from the origin (0, 0).G17
: Selects the XY plane for cutting operations.
2. Move to the Starting Position
G00 X0 Y0 Z5
: Moves the tool rapidly to X=0, Y=0, and Z=5. The tool is positioned above the material, ready to start cutting.
3. Start the Spindle
M03 S1000
: Starts the spindle spinning clockwise at 1000 RPM. Adjust the speed (S1000
) depending on your material and tool.
4. Lower the Tool
G01 Z-1 F100
: Lowers the tool to a depth of -1 mm (cutting depth) at a feed rate of 100 mm/min. The tool is now ready to cut.
5. Cut the Square
G01 X50 Y0
: Moves the tool in a straight line to X=50, Y=0, cutting the first side of the square.G01 X50 Y50
: Moves to X=50, Y=50, cutting the second side.G01 X0 Y50
: Moves to X=0, Y=50, cutting the third side.G01 X0 Y0
: Returns to X=0, Y=0, completing the square.
6. End the Program
M05
: Stops the spindle.G00 Z5
: Raises the tool to a safe height above the material.G28
: Sends the tool back to the home position.M30
: Marks the end of the program.
Customizing the Program
You can modify this program for different shapes or dimensions:
- Change the Size: Adjust the
X
andY
coordinates to create rectangles or other shapes. - Depth of Cut: Modify the
Z
value to increase or decrease the cutting depth. - Feed Rate and Spindle Speed: Adjust
F
(feed rate) andS
(spindle speed) to suit different materials or tools.
360 Key Takeaways
- G-code programs are made up of commands like
G00
(rapid movement) andG01
(cutting movement). - Always include setup commands like
G21
(units) andG90
(positioning) at the start of your program. - Testing with simple shapes like squares helps you understand tool movements and program flow.
- Use comments (e.g.,
;
) to annotate your code and make it easier to read.
Conclusion
Writing G-code programs is a valuable skill for CNC operators, and starting with simple examples like this square-cutting program is a great way to learn. Practice tweaking the parameters, and you’ll quickly gain confidence in creating your own programs. Happy machining!