G-Codes & M-Codes: The Differences
What Are They?
G- and M-code are two specific types of programming used to control machines. They work side-by-side to command a machine but aren’t exactly the same.
Taking a step back, such commands are produced by a compiler program (e.g. a slicer for 3D printing), which translates an input source like a 3D model into machine code. G- and M-code commands, specifically, are written with a number to specify the exact command, such as “G20” or “M500”. Commands can also have other characters, like “G1 X0 Y0 Z0” (where X, Y, and Z refer to the axes of a printer).
In this article, we’ll be looking a little closer at the differences between G- and M-code. First, we’ll review origins and basic meanings, and then we’ll move on to more specific aspects, including how they relate to 3D printing.
The Big Picture
Before we really get into the differences between G- and M-code, it’s important to note that the term “G-code” is most often used as the common name for the CNC programming language RS-274. In this programming language, commands are categorized alphabetically, with different letter commands signifying a different type of operation.
In other words, each letter of the alphabet controls one or a few aspects of a machine’s operation. For example, commands starting with ‘S’ (also called S-code commands) have to do with speed, whereas M-code covers miscellaneous functions. That said, not all RS-274 commands have alphabetical associations. For example, K-code controls the Z offset, and L-code controls the number of repetitions.
Even though G-code is just one of 26 different command categories, the word “G-code” is often used to represent the entire RS-274 language. For this reason, M-code can be thought of as a type of G-code, even though, technically, they’re on the same level.
Now that you know some background information, let’s get into some specifics!
Under the Microscope
Although G- and M-code commands are used together and function similarly, there are some key differences between them. The first (and obvious) difference stems from their names.
Although not officially stated, the ‘G’ in “G-code” is often said to stand for “geometric”. This may be because G-code mostly controls a machine’s positioning, movement, and the geometric shape of the part being made relative to the workpiece (i.e. hot end, spindle, etc.).
Under this definition, G-code can be seen to perform a wide range of essential functions. On a 3D printer, this even includes the extruder feed rate, which may not seem intuitive until you consider that the extruder is controlled in the same way as any of the axes: via a stepper motor.
M-code, on the other hand, covers “miscellaneous” functions, though some also refer to them as “machining functions”. This includes, among other things, tool speed (e.g. of an extruder or spindle).
M-code is the action command for the unique parts of an automated machine. For example, M3 sets a machine’s spindle motion to the clockwise mode.
Now that you know the differences between G-code and M-code, you might be wondering, what are they even used for? As we said at the beginning of this article, you’ve likely unknowingly interacted with these commands, especially when working with automated machines.
G- and M-code commands are used in 3D printers, CNC machines, and other automated additive or subtractive manufacturing machines. Even if you don’t see it, many devices use G- and M-codes to function, but this programming is hidden by simpler interfaces like touchscreen menus and CAM software.
An example of this is setting your 3D printer to its home axis. This may seem like a straightforward menu option, but in fact the printer has to be directed by code. The printer will move all three axes to a certain point, in a specific way, at a precise speed, and if you set temperatures, that’s another command, too.
For every action performed by a machine, there’s at least one G- or M-code command. When you 3D print, CNC, or mill an entire object, thousands of G- and M-code commands are processed and carried out by your machine.
There isn’t really a common case where one type of code would be used alongside the other. However, if you needed to control just a machine’s motion system without any tool head, then G-code could possibly be used without M-code.
Overall, G- and M-code are similar because they’re both used in automated machines, but they do have their differences in which actions they correspond to.