{"id":3987,"date":"2024-10-09T22:28:14","date_gmt":"2024-10-09T22:28:14","guid":{"rendered":""},"modified":"2024-10-10T17:37:11","modified_gmt":"2024-10-10T17:37:11","slug":"g-and-m-codes","status":"publish","type":"post","link":"https:\/\/wp.unionfab.com\/ja\/g-and-m-codes\/","title":{"rendered":"G &#038; M Codes: The Secret Language of CNC Machines"},"content":{"rendered":"<p>Discover G and M codes in CNC programming. Learn how they control machine operations, improve precision, and simplify manufacturing.<\/p>\n<h2 id=\"07351698-dbb9-4b9a-80b3-bbed6b8bc90f\" data-toc-id=\"07351698-dbb9-4b9a-80b3-bbed6b8bc90f\">Introduction<\/h2>\n<p><strong>G and M codes<\/strong> are essential in <a target=\"\" rel=\"noopener noreferrer nofollow\" class=\"fake_link link\" href=\"https:\/\/www.unionfab.com\/services\/cnc-machining\">CNC<\/a> (Computer Numerical Control) programming, helping machines perform precise tasks and movements.<\/p>\n<p>This guide explains what G and M codes are, why they matter, and how they are used in different industries.<\/p>\n<h2 id=\"43f94dca-1494-4e1f-8cb1-ab3068f520ae\" data-toc-id=\"43f94dca-1494-4e1f-8cb1-ab3068f520ae\">What are G and M Codes?<\/h2>\n<p><strong>G and M codes<\/strong> are the basic programming languages for CNC machines. The codes automate the processes by giving the CNC machine a standardized set of step-by-step instructions to follow without human intervention. This allows precise and repeatable cutting of complex 3D parts.<\/p>\n<ul>\n<li>\n<p><strong>G codes<\/strong> control the machine&#8217;s movements.<\/p>\n<\/li>\n<li>\n<p><strong>M codes<\/strong> manage extra functions, like turning the spindle or coolant on and off.<\/p>\n<\/li>\n<\/ul>\n<p>\u2800These codes work together to ensure accurate and efficient machining operations.<\/p>\n<h3 id=\"d4f122b2-aa38-447e-a81d-a9b72616d382\" data-toc-id=\"d4f122b2-aa38-447e-a81d-a9b72616d382\">Common G Codes List<\/h3>\n<table style=\"min-width: 50px\">\n<colgroup>\n<col>\n<col><\/colgroup>\n<tbody>\n<tr>\n<th colspan=\"1\" rowspan=\"1\">\n<p>G Code<\/p>\n<\/th>\n<th colspan=\"1\" rowspan=\"1\">\n<p>Description<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>G00<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Rapid positioning<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>G01<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Linear interpolation (controlled feed rate)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>G02<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Circular interpolation (clockwise)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>G03<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Circular interpolation (counterclockwise)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>G04<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Dwell (pause for a specified time)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>G17<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Select XY plane<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>G18<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Select ZX plane<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>G19<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Select YZ plane<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>G20<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Input in inches<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>G21<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Input in millimeters<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>G28<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Return to home position<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>G90<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Absolute programming<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>G91<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Incremental programming<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<figure><img decoding=\"async\" src=\"https:\/\/ufc-dtc-cms.oss-accelerate.aliyuncs.com\/blog\/20241009\/183029_2ryx95otm.png\" alt=\"\" title=\"\" style=\"width: auto; display: block; margin: 0 auto;\" url=\"https:\/\/www.unionfab.com\/order#\/anonymous_new_shop_quote\/?origin=MKblog\" onclick=\"window.open('https:\/\/www.unionfab.com\/order#\/anonymous_new_shop_quote\/?origin=MKblog', '_blank')\"><figcaption class=\"wp-element-caption\"><\/figcaption><\/figure>\n<h3 id=\"3abfa664-aaf6-4641-9881-9fb9e615b268\" data-toc-id=\"3abfa664-aaf6-4641-9881-9fb9e615b268\">Common M Codes List<\/h3>\n<table style=\"min-width: 50px\">\n<colgroup>\n<col>\n<col><\/colgroup>\n<tbody>\n<tr>\n<th colspan=\"1\" rowspan=\"1\">\n<p>M Code<\/p>\n<\/th>\n<th colspan=\"1\" rowspan=\"1\">\n<p>Description<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>M00<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Program stop<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>M01<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Optional stop<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>M02<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Program end<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>M03<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Spindle on (clockwise rotation)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>M04<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Spindle on (counterclockwise rotation)<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>M05<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Spindle stop<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>M06<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Tool change<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>M08<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Coolant on<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>M09<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Coolant off<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"1\" rowspan=\"1\">\n<p>M30<\/p>\n<\/td>\n<td colspan=\"1\" rowspan=\"1\">\n<p>Program reset and end<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"54de3888-10f6-4341-9d2c-c83f76936d24\" data-toc-id=\"54de3888-10f6-4341-9d2c-c83f76936d24\">Key Difference Between G and M Codes<\/h3>\n<p>The main difference between G and M codes is their function.<\/p>\n<ul>\n<li>\n<p><strong>G codes<\/strong> control how the machine moves, like the cutting path and speed.<\/p>\n<\/li>\n<li>\n<p><strong>M codes<\/strong> handle other machine functions, like turning the spindle or coolant on and off.<\/p>\n<\/li>\n<\/ul>\n<h2 id=\"97c7e9eb-a344-41c6-afa1-c271335d9ad6\" data-toc-id=\"97c7e9eb-a344-41c6-afa1-c271335d9ad6\">Why are G and M Codes Important for CNC Machining?<\/h2>\n<p>G and M codes are essential in CNC machining for several reasons:They give exact instructions, allowing for highly accurate manufacturing.<\/p>\n<ul>\n<li>\n<p><strong>Precision<\/strong>: They provide precise instructions that enable highly accurate manufacturing.<\/p>\n<\/li>\n<li>\n<p><strong>Efficiency<\/strong>: Automating movements and operations reduces the need for manual intervention, speeding up production.<\/p>\n<\/li>\n<li>\n<p><strong>Flexibility<\/strong>: CNC programs can be easily modified to accommodate changes in design or specifications.<\/p>\n<\/li>\n<li>\n<p><strong>Repeatability<\/strong>: G and M codes ensure that parts can be produced with consistent quality across multiple runs.<\/p>\n<\/li>\n<\/ul>\n<h2 id=\"69efb7a5-343b-4583-9c6f-1f568191ba0a\" data-toc-id=\"69efb7a5-343b-4583-9c6f-1f568191ba0a\">How to Read and Write G &amp; M Code Programs<\/h2>\n<h3 id=\"5dbf5d2a-a5e2-45b7-adc4-8b0881610f7a\" data-toc-id=\"5dbf5d2a-a5e2-45b7-adc4-8b0881610f7a\">Basic Structure of a CNC Program<\/h3>\n<p>A CNC program starts with a program number (e.g., N001), followed by G and M codes to control the machine&#8217;s operations.<\/p>\n<p>Each line represents one command, with the order of operations specifying tool movements, pauses, and setting changes.<\/p>\n<h3 id=\"c92a688c-f4be-46ab-b837-1691e2cb6ad8\" data-toc-id=\"c92a688c-f4be-46ab-b837-1691e2cb6ad8\">Best Practices for Authoring Programs<\/h3>\n<h4 id=\"ffe9004f-1fcc-455b-9174-528991a5869d\" data-toc-id=\"ffe9004f-1fcc-455b-9174-528991a5869d\">Use Modular Macros<\/h4>\n<p>Create reusable subroutines for repetitive tasks to avoid writing the same code repeatedly.<\/p>\n<h4 id=\"1a69cf95-68f1-4178-8792-63ae44f200c2\" data-toc-id=\"1a69cf95-68f1-4178-8792-63ae44f200c2\">Simulate First<\/h4>\n<p>Run your program in CAD\/CAM software to catch errors before using the machine.<\/p>\n<h4 id=\"ffe693e2-2361-4589-a960-f6ce25160cbc\" data-toc-id=\"ffe693e2-2361-4589-a960-f6ce25160cbc\">Test Gradually<\/h4>\n<p>Start with simple tests to check motions, then add complexity step by step.<\/p>\n<h4 id=\"a70acfd5-736f-4a2c-a86b-120128822f94\" data-toc-id=\"a70acfd5-736f-4a2c-a86b-120128822f94\">Apply Descriptive Comments<\/h4>\n<p>Include clear comments to explain the purpose of each section, making the code easier to understand later.<\/p>\n<h4 id=\"2a71d4fc-d5d7-47b0-ae9f-1e33e5a77c14\" data-toc-id=\"2a71d4fc-d5d7-47b0-ae9f-1e33e5a77c14\">Keep Code Neat<\/h4>\n<p>Use consistent formatting\u2014like spacing and indentation\u2014to make the code easier to read and troubleshoot.<\/p>\n<h2 id=\"f26f179a-027a-42cc-a1c2-6aa2de0e91f9\" data-toc-id=\"f26f179a-027a-42cc-a1c2-6aa2de0e91f9\">Troubleshooting Common Issues with G &amp; M Codes<\/h2>\n<ul>\n<li>\n<p><strong>Unexpected Tool Movements<\/strong>: These happen when G code commands or tool offsets are incorrect. Double-check your code and tool offset settings.<\/p>\n<\/li>\n<li>\n<p><strong>Tool Crashes<\/strong>: Tool crashes occur from incorrect tool paths or lack of clearance. Make sure the tool paths avoid obstacles and have enough space.<\/p>\n<\/li>\n<li>\n<p><strong>Inconsistent Cuts<\/strong>: Caused by incorrect feed rates, tool wear, or machine vibrations. Check the feed rate, inspect tools for wear, and ensure the machine is stable.<\/p>\n<\/li>\n<li>\n<p><strong>Error Messages<\/strong>: Pay attention to any error messages on the machine\u2019s display and consult the manual for solutions.<\/p>\n<\/li>\n<li>\n<p><strong>Program Errors<\/strong>: Syntax issues like missing symbols or invalid commands can stop the program. Review the code carefully and use a code editor to spot errors.<\/p>\n<\/li>\n<\/ul>\n<h2 id=\"9d5d283a-c6bb-4a93-9c40-6188ba1fedd8\" data-toc-id=\"9d5d283a-c6bb-4a93-9c40-6188ba1fedd8\">Conclusion<\/h2>\n<p>G-codes and M-codes are important in CNC (Computer Numerical Control) machining. They allow CNC machines to efficiently manufacture complex metal parts through automated cutting and manufacturing according to the G and M code programs.<\/p>\n<h2 id=\"007dc6a5-f426-4290-9c35-68f32a1a376c\" data-toc-id=\"007dc6a5-f426-4290-9c35-68f32a1a376c\">Precision CNC Machining Services at Unionfab<\/h2>\n<p><a target=\"\" rel=\"noopener noreferrer nofollow\" class=\"fake_link link\" href=\"https:\/\/www.unionfab.com\/\">Unionfab<\/a> is a leading manufacturing company that offers CNC machining services according to customer specifications and drawings. We utilize a variety of CNC machine tools, including 3 axis, 4 axis and 5 axis milling machines and lathes.<\/p>\n<p><a target=\"\" rel=\"noopener noreferrer nofollow\" class=\"fake_link link\" href=\"https:\/\/www.unionfab.com\/contact-us\">Contact us<\/a> today to discuss your next CNC machining project and how we can help optimize your manufacturing process.<\/p>","protected":false},"excerpt":{"rendered":"<p>Discover G and M codes in CNC programming. Learn how they control machine operations, improve precision, and simplify manufacturing.<\/p>","protected":false},"author":2,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[],"tags":[],"class_list":["post-3987","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/wp.unionfab.com\/ja\/wp-json\/wp\/v2\/posts\/3987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.unionfab.com\/ja\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.unionfab.com\/ja\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.unionfab.com\/ja\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.unionfab.com\/ja\/wp-json\/wp\/v2\/comments?post=3987"}],"version-history":[{"count":0,"href":"https:\/\/wp.unionfab.com\/ja\/wp-json\/wp\/v2\/posts\/3987\/revisions"}],"wp:attachment":[{"href":"https:\/\/wp.unionfab.com\/ja\/wp-json\/wp\/v2\/media?parent=3987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.unionfab.com\/ja\/wp-json\/wp\/v2\/categories?post=3987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.unionfab.com\/ja\/wp-json\/wp\/v2\/tags?post=3987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}