# 3D-Printing G-Code ## What Is G-code? G-code is a text-based programming language that is used to control the movements of CNC machines including 3D printers. It essentially provides instructions to the machine on how it should move, what actions it should take. These instructions can include specific coordinates for the printer head, information for printing speeds, and other critical data. Each command starts with either G or M depending on whether it is related to movement (G) or machine control (M). Although there are many different commands, their format generally follows the same structure, here are a few examples: * Move to position X = 10, Y = 20, and Z = 30. ```gcode G0 X10 Y20 Z30 ``` * Set the extruder temperature to 200℃, and wait until the temperature is reached. ```gcode M104 S200 ``` ## Serial Interface The serial interface is a way of communication that sends data one bit at a time over a **single communication line**. It is commonly used for connecting devices such as printers, scanners, and microcontrollers to a computer. ## Connecting Printer via Serial Interface [**Printrun**](https://www.pronterface.com) ![GitHub Repo stars](https://img.shields.io/github/stars/kliment/Printrun) Printrun is a popular open-source software that provides an interface for communication with 3D printers. It offers a set of tools that simplifies the process of controlling and operating 3D printers, making it easier for users to interact with their machines. ## Commonly Used G-Code ### G0, G1: Linear Movement Both commands move the extruder from point A to point B in a linear movement. By convention, the G0 command moves the head without extruding material. G1 command is used for movements that include extrusion. All G0 and G1 commands must include the final positions for the `X`, `Y`, and `Z` coordinates. G1 must also include the amount of extrusion to be performed during the move. The commands can also include the feed rate with parameter `F`. `G1 X45 Y45 Z1 F1000 E1` This command will move the arm in a straight line to the final coordinates X = 45mm, Y = 45mm, Z = 1mm at a feed rate of 1,000mm/min while extruding 1mm of material. You can omit these parameters when they have not changed since the last move. In other words, if only one axis has changed, you can keep the same value for the others. For example, `G1 Y50` would move the printer head 50mm in the Y direction while ignoring X and Z because they haven't changed. ### G02-G03: Circular Interpolation Also called arc or circle move, the circular interpolation adds a clockwise (G02) or counterclockwise (G03) arc move to the planner. By default, the arc move starts at the current position and ends at the given coordinates (XYZ), pivoting around a center point given by an X offset `I`, Y offset `J`, or radius offset `R`. Users can choose which form of the command to use between axis (I and J) offset or radius offset. Mixing I or J with R will throw an error. This illustrates a counter clockwise arc, starting at `[9, 6]`. It can be generated either by `G3 X2 Y7 I-4 J-3` or `G3 X2 Y7 R5` ![g-code G3 command explained](https://note.rccn.dev/uploads/upload_d7ea9ca79ceaa26d1bd599d4df80c159.png) image source: [Marlin Firmware](https://marlinfw.org) ### G92: Set Current Position Sometimes, you may want to change or offset the location of your axes. This is where the G92 command comes in handy, enabling you to overwrite the position of any axis. One of the most popular uses of this command is overwriting the current filament position so that all future commands will become relative to this new value. Typically, this command is given right before a prime or retraction command or at the start of each new layer. For example, the `G92 E0` command sets the current filament position to 0. When followed by `G1 F800 E10`, the printer extrudes 10mm of filament at a feed rate of 800mm/minute, starting from the position set by the G92 command. ### G90: Absolute Positioning / G91: Relative Positioning The `G90` code is used at the beginning of a command queue to tell the printer to move to an exact XYZ coordinate. This command can be used on its own or together with `M82`, which sets the extruder axis `E` to absolute positioning. Slicing software uses `G90` positioning by default. `G91` command is similar to `G90`, but it refers to relative positioning. ### G28: Homing Routine When a homing routine is initiated, the machine moves towards specific sensor or switch (usually called endstop) located along the three axis and touches them to create reference coordinates for the machine's movement. The machine knows where each homing switch is placed and will stop once it reaches it. When starting up the printer, the machine doesn’t know where the head is positioned, so the firmware must trigger a homing procedure to establish a known position. It is a crucial step that must be executed correctly before any CNC operation can begin. In its default form without other parameters, G28 homes all axes. However, users can input specific commands, such as `G28 X` or `G28 Y`, to only home the X or Y axis, respectively. Homing is required before leveling the bed through the `G29` command, and it can also be required before other commands. ### G29: Bed Leveling Leveling the build plate is crucial to ensure the quality of your prints. After running a homing command, you can use `G29` to level the bed in one of the following ways: * **3-point** This automatic leveling type probes the bed at three points to reveal leveling issues. It then enables compensation and uses a rotating matrix to compensate for bed tilt. * **Linear** Similar to the 3-point, this automatic procedure enables compensation to adjust bed tilt. However, Marlin probes the bed in a grid rather than only three points. * **Bilinear** Another automatic procedure; Marlin probes the bed in a fixed number of points and generates a mesh that represents bed imperfections. * **Unified** This comprehensive procedure uses a set of resources to optimize bed leveling. Automated probing allows users to compensate the entire bed and fine-tune the system, modifying the mesh based on print results. This enhances adhesion for the first layer and prevents problems like the elephant foot, but the command can be complicated to use. * **Manual** This procedure enables users to measure Z height with a piece of paper or feeler gauge, then move Z coordinates to compensate for variations in bed height. :::info The Prusa Printers at Raccoon are set to use **Bilinear** ::: ### G60: Save Current Position The G60 command comes in handy whenever you want the printer to memorize the position of all axes for later recall. To do that, Marlin uses slots that users can input manually. All slots are initially set to 0,0,0, and if this is the position that you want to save, you can omit inputting anything (just use the G60 command on its own). ### G61: Return to Saved Position This command is used to recall the position saved with the G60 command. If you have omitted the slot, you can omit to input it in the G61 command too. Otherwise, the command could be given as G61 if you want to restore the previous position of all axes or follow it by the letter of the axis you want to restore. For example, G61 XY S0 restores to slot 0 position the axes X and Y without moving axes Z and E. ### M106-M107: Fan Control When printing with a filament that requires cooling, the M106 commands can help you set the fan speed and the number of fans you want to turn on if the printer has more than one. The M107 command allows you to turn off the fan. The M106 command requires you to input parameters for speed and fan number. S stands for speed; this number can range from 0 (off) to 255 (full speed). So, to set the fan at 50%, for instance, you must input 127.5. If the printer has more than one fan, you can input a P index that can be 0, 1, 2, etc., depending on the number of fans you have. ### M48: Probe Repeatability Test A 3D printer’s probe is an essential tool for calibration. If it fails to work properly, it won’t calibrate the bed and other parameters, with a negative impact on your prints. The M48 command enables you to test the probe for precision by performing a series of repeated actions. Using obtained data, Marlin produces a standard deviation that can help you calibrate this component if needed. ### M109: Wait for Bed Temperature When setting up the machine to print an object, you might have observed that the printer waits for the hotend to reach the temperature before enabling extrusion. This is achieved through the `M109` command. However, this command can also be handy if you want to switch from one type of filament to another or change the extrusion temperature mid-cycle. An `M109` command followed by `S` and temperature waits for the nozzle before extrusion only when heating. If followed by R and temperature, the printer will wait for the nozzle to reach the set temp regardless of whether it’s heating or cooling. Thus, if you want to switch from a higher temperature to a lower one, `M109 R<temp>` can help you do that. ### M114: Get Current Position The `M114` will tell you the current position of the active tool, including the position of the steppers. ## Alternative Slicing Common slicing techniques divide a 3D model into layers, resulting in a flat sequential pattern. However, some alternative slicing techniques may improve print quality and efficiency. Here are three examples of different approaches: * **Non-Planar Slicing** This technique takes an object with curved surfaces (like a sphere or cylinder) and slices it into shapes that conform to the object's surface. It results in smoother surfaces and more-efficient use of printing volume. ![](https://note.rccn.dev/uploads/upload_5d76416751b42576193c8c5ea579d24b.png) For more information regarding this topic, please visit [this article on All3DP](https://all3dp.com/2/non-planar-3d-printing-simply-explained/) * **Adaptive Slicing** This technique uses variable layer heights, which are determined automatically based on the model's curvature changes. Curved parts of a model can be printed with greater accuracy and detail, whereas flat areas or featureless sections can be printed faster at thicker layers. You can activate the adaptive layers feature in the "Experimental" settings section in Cura ![](https://note.rccn.dev/uploads/upload_0daa1c97ccaf8df4a31a3d90b843a7f9.png) * [Silkworm](https://www.food4rhino.com/en/app/silkworm) Silkworm is a plugin that translates Grasshopper and Rhino geometry into GCode for 3d printing. Silkworm allows for the complete and intuitive manipulation of the printer GCode, enabling novel printed material properties to be specified by non-solid geometry and techniques of digital craft. ![plugin silkworm](https://note.rccn.dev/uploads/upload_3f9d2396ec19990bdb3b25815ee7ab71.png) ## Appendix ### Marlin G-code Raccoon's Prusa printers use Marlin firmware, more information of available g-code could be found at [MarlinFW Docs](https://marlinfw.org/meta/gcode/). Please note that not every g-code is enabled on our machines. ### Alternative Ways to connect a Printer ### macOS You can also use the built-in Terminal application on Mac to connect to a serial port. In terminal ,type in `ls /dev/tty*` to show a list of available ports. Look for the port that matches the printer, it should be listed as something like `/dev/tty.usbmodem` or `/dev/cu.usbmodem`. Type in `screen <port> <baud rate>`, replacing `<port>` with the correct port name found in the previous step and `<baud rate>` with the baud rate of your 3D printer (usually 115200 or 250000). Once you are connected to your 3D printer, you can send commands to it through the Terminal for tasks such as setting temperatures, homing axes, and starting prints. It's worth noting that there are also dedicated serial terminal applications available for macOS such as CoolTerm, ZTerm, and Serial. These applications can sometimes have additional features beyond what is provided in Terminal. ### Windows For Windows, you can use several software for terminal to communicate with serial ports, including: * [**PuTTY**](https://github.com/github/putty) ![GitHub Repo stars](https://img.shields.io/github/stars/github/putty) A free open-source terminal emulator that supports various network protocols and serial communication. * [**RealTerm**](https://sourceforge.net/projects/realterm/) A powerful serial/TCP terminal program that can be used for debugging and testing of serial communication. * [**Tera Term**](https://ttssh2.osdn.jp/index.html.en) A free and open-source terminal emulator that supports serial ports and network connections. * [**Arduino IDE**](https://www.arduino.cc/en/software) An integrated development environment used to upload code to Arduino boards through COM or USB ports. * **CoolTerm([Win](https://coolterm.en.lo4d.com/windows)/[macOS](https://www.macupdate.com/app/mac/31352/coolterm))**