Learning G and M codes for CNC lathe is like learning a new language. G and M codes are essential parts of a CNC programmer’s toolbox, which you will learn in time.
If you’re finding it challenging to learn these codes, then don’t worry. This simple guide is made just for you. In this blog, you will learn what G and M codes are, a list of these codes, the difference between G and M codes, tips for beginners to write G and M codes and many more. So, keep learning!
1) What Are G and M Codes?
“In computer numerical control (CNC) machining, the G-code and M-code are the programming languages used to command machines to perform specific tasks.”
Are you learning CNC programming? Well, G and M codes are the first things you should know about. They’re instructions for the machine to operate; it is all that gives direction on how to move, when to start or stop.
Broadly speaking, all CNC programs consist of G and M codes. Each one has its specific function. Some are responsible for moving the tool while others control various parts of the machine. So, when these codes are used together, they guide a machine to operate an entire process.
Additionally, Koonze offers dependable CNC solutions, thus making programming more efficient and accurate than ever before.
- G Codes: Movement Commands
G-codes command the machine to make the movements. It guides how and where to show the motion. For instance, G00 means rapid positioning, G01 means straight line cutting, while G02 and G03 refer to circular movements.
Hence, the tool can be moved with accuracy using these codes. While milling, drilling or turning, the tool will not deviate from its path as long as it follows the right G code. Furthermore, they can control speed, direction and cutting plane.
- M Codes: Action Commands
Well, M codes control the action commands. They could be used to turn on or off the spindle, activate the coolant or stop a program. For example, M03 turns on the spindle, whereas M05 stops it in its tracks. Additionally, the coolant system is turned on by M08.
Thus, these actions support the cutting process as mentioned above. For example, they would not move a tool but would help in making operations safer and smoother.
2) A List of G and M Codes for CNC Machining
Alright! G and M codes are the basic elements of any CNC program, and once you learn them, coding becomes much more straightforward. Now let’s take a look at some of the most frequently used G codes and M codes list. These are quite easy to remember, and they are of great daily use in machining.
i) Commonly Used G Codes in CNC Machining
G stands for Geometric or General codes. These are the travel guides for a CNC cutting tool. They decide where the tool goes, how fast it moves, and even what path it follows. Let’s start with the G codes list:
Code | Name | Function |
G00 | Rapid Positioning | Moves the tool quickly to a position without cutting. Saves switching time. |
G01 | Linear Interpolation | Moves the tool in a straight line with cutting. |
G02 | Circular Interpolation (CW) | Moves the tool in a clockwise circular path. Ideal for round cuts. |
G03 | Circular Interpolation (CCW) | Moves the tool counter-clockwise in a circle. Used for curved shapes. |
G20 | Programming in Inches | Sets the unit system to inches. Common in the U.S. |
G21 | Programming in Millimetres | Sets the unit system to millimetres. Used in metric-based systems. |
G28 | Return to Home Position | Sends the tool back to the machine’s home/original position. |
G90 | Absolute Positioning | Positions the tool based on a fixed zero reference point. |
G91 | Incremental Positioning | Positions relative to the current tool position. Adds to the last move. |
ii) Commonly Used M Codes in CNC Machining
M codes (Miscellaneous codes) in CNC machining control the action of the machine. These are used to switch things on and off. Let’s discuss the list of some commonly used M codes for the CNC Lathe. Let’s start with m codes list:
Code | Name | Function |
M00 | Program Stop | Pauses the program until manually resumed. Used for checks or tool changes. |
M01 | Optional Stop | Stops the program only if the optional stop is activated. |
M02 | End of Program | Ends the program. Machine stops and waits for next input. |
M03 | Spindle On (Clockwise) | Turns the spindle clockwise. Default cutting direction. |
M05 | Spindle Stop | Stops the spindle rotation. |
M08 | Coolant On | Turns on the coolant during machining. |
M09 | Coolant Off | Turns off the coolant. |
M30 | Program End and Reset | Ends the program, resets machine, stops spindle and coolant. |
3) The Difference Between G and M Codes
Well, as a beginner, you should also know the difference between G and M codes for a CNC machine. Although these two appear together in most programs, their functions are quite different from each other. Let us take a simple comparison to discuss their roles.
Feature | G Codes | M Codes |
Purpose | Tool movement | Machine actions |
Role in Program | Cutting and path control | Start/stop support functions |
Moves Tool? | Yes | No |
Instruction Type | Geometric | Functional |
Usage Frequency | Very frequent | As needed |
Example Codes | G00, G01, G02 | M03, M05, M08 |
When Used | During tool paths | Before or after the movement |
i) Purpose
G Code: The G codes CNC are important in controlling the movement of a tool. They direct the path, speed and direction of the tool. Hence, all cutting motions depend on G-codes.
M Code: Moreover, these codes are important in managing machine functions. These codes help in starting the spindle or turning on the coolant. Therefore, these actions support the tool while it moves.
ii) Tool Movement
G codes: These codes are used to guide tool movement through straight lines, curves or angles. G-codes are responsible for every motion in machining; they plan its path.
M codes: Conversely, cnc M code do not move tools but only control what a machine does. So, you can consider them as a machine’s buttons.
Koonze’s high-precision models are examples of such CNC solutions that guarantee accurate and repeatable commands of these code instructions, even in complex manufacturing runs.
iii) Type of Instructions
G codes: Well, G codes give commands on geometrical aspects. The directions given to machines on how they should navigate through space are all about direction and shape.
M codes: On the other hand, M codes are used to command functional aspects. These may be turning a system on or off, among others.
iv) The Frequency of Use
G codes: When you have a CNC program, G codes will be found in almost all the lines. As such, they are important to be included throughout the cutting process.
M codes: Unlike G codes, M codes are not as frequent. Instead, they come about when a specific function needs to be turned on or off. However, this can take place when processing is at its beginning, middle or end.
v) Execution Timing
G codes: Just like a clock that moves continuously, the G codes are also working consistently during the machinery operation. Thus, it controls the machinery process from start to stop. However, it is most important for any cutting operation.
M codes: On the other hand, cnc M codes operate at specific times. They are activated to perform a task, then stop.
4) Sample CNC Program with G and M Codes
Seeing an example of G codes and M codes for CNC programming can further clarify things. A sample program aids in learning the interrelation of both codes and their functions in running a machine. There is no need to worry, it is simpler than it looks!
So, let us go through a straightforward CNC milling program together.
- Example Code for Drilling a Hole
%
O1001 (Drilling Program)
G21 (Set units to millimetres)
G90 (Set absolute positioning)
G00 X0 Y0 (1. Rapid move to the hole’s X,Y position)
M03 S1000 (2. Start spindle clockwise at 1000 RPM)
M08 (3. Turn on coolant – recommended)
// — Correct Drilling Method Begins —
G81 Z-10 R5 F200 (4. Execute drilling canned cycle)
// Z-10: Final depth of the hole
// R5: Retract plane (safe Z height)
// F200: Feed rate in mm/min
G80 (5. Cancel canned cycle)
// — Correct Drilling Method Ends —
M05 (6. Stop spindle)
M09 (7. Turn off coolant)
G28 (8. Return to machine home position)
M30 (9. End program and reset)
What Each Line Does
- G21 is used to operate the unit in millimetres. This is common in metric-based tools.
- G90 code is used to tell the machine to use absolute positioning, which means all positions relate to a certain reference point.
- With the G00 X0 Y0 code operates the tool is placed very quickly to the starting position without cutting.
Now we can turn on the necessary components:
- M03 S1000 starts the spindle at 1000 revolutions per minute. Hence, this is needed before cutting begins.
- G00 Z5 prepares the tool to get ready by positioning it just above the material level.
- G01 Z-10 F200 lowers the drill at a rate of 200 mm/min, which is the drilling portion of the operation.
Drilling is done when:
-
- G00 Z5 returns the tool to a safe position, which ensures that the tool is not exposed to error during movement.
- M05 stops the spindle.
- M09 disables any coolant that may have been previously activated.
- G28 positions the tool back to the home location, which is helpful for the next setup.
- M30 sets the program to default and ends it, then resets the cycle for the next start.
Why This Matters
Alright! The program as a whole shows how G-codes manage movement and M-codes control actions of the machine. So, both are required to complete a task. The combination of both codes provides precision, safety, and CNC lathe machining efficiency.
5) Tips for Beginners Writing G and M Code
When you first encounter G and M codes, the sea of numbers and letters looks huge, yet a steady step-by-step plan soon turns it from a headache into a pleasant puzzle to solve. Be it a CNC lathe or a different mill, practical tips and small notes let you grasp the logic, reduce mistakes, and slowly watch your confidence grow.
On days the codes feel too dense or technical, do not hesitate to call in a trusted machining shop- they can tackle the nitty-gritty, give you accurate parts, and free you to work on bigger ideas. Let those experts sweat the details while you keep your attention on design, quality, and whatever else drives your project forward.
- Understand What Each Code Means
The prime tip is to learn the definition and purpose of the common G and M codes. Now, you know well, G codes are used to execute movements while M codes are used for machine operations such as switching the spindle on or off.
- Begin with Simple Programs
Another tip is to start with basic operations, such as drilling a hole or cutting a straight line. Hence, short programs help you achieve small benchmarks and reinforce your self-assurance over time.
- Follow the Correct Sequence
Well, you should ensure that your code always starts with the spindle on, the tool in a raised position, the cutting sequence, raising the tool, moving, etc. Therefore, if you follow the correct sequence, then the chances of making mistakes are minimised.
- Use Comments in Your Code
For clarity, every line of code should have an explanation next to it. For example, next to M03, you can write (Start Spindle). Well, this makes your program structured and organised.
Koonze models have a built-in code verification system which helps new users not making expensive mistakes before a job is started.
- Check Your Code Before Running
Furthermore, you should go through your code step by step. Even the smallest carelessness could cause damage to the machine or the part.
- Practice with a CNC Simulator
Keep in mind! You should run your code in simulator mode first. It displays the actions of the machine without actually using real components. It’s a risk-free way to learn and understand.
- Keep a Code List Nearby
Always keep a printed version of G and M codes with you. This aids in fast command checks and quick access. Yes, even experts use it often.
6) Conclusion
Alright! G and M codes are important for accurate and dependable machining with CNC machines. However, they give clear directional orders for tools and machines. Well, mastering the art of CNC programming will allow you to write efficient programs that yield desirable outcomes.
Koonze Model is always ready to serve you with fast, ISO‑certified CNC services including milling, turning, quick-turn prototyping, and more. These are all supported by professionals. So, are you really looking for top-quality parts?