How GCode for 3D printer works

How does GCode for 3D printer work?

The answer is the key to understand the language that runs some open source 3D printers.

Historically GCode was a simple language, born at the MIT in the 1950’s, to manage movements on a CNC machine. Basically a 3D printer is a CNC that adds material instead of subtracting it (additive manufacturing). GCode, as all the languages, has a grammar to know. GCode is at the base of photo plotters that serve to etch  printed circuits board (PCB) in electronics. This programming language has been standardized by the RS-274D.

To fully understand how GCode works, it is better to explain, basically, how a 3D printer works.

How does a 3D printer work?

Stampante 3D Hephestos 2
A 3D printer

A 3D printer builds an object by melting a plastic filament. The object grows layer by layer. The extruder, that melts the filament, is like a pen that sticks fused plastic following lines calculated by a 3D design.

Starting from a designed cube, as example, we have to convert this 3D design into an “stl” file (stereolitography). The design will be converted in a mesh, many triangles that surround the external surface of the cube.

How GCode for 3D printer work - 3D design
How GCode for 3D printer work – the original 3D design

Below you can find a rendering of a Mesh tessellation. You can see every face of the cube divided into two triangles. The three dimensional space coordinates describe each triangle.

How GCode for 3D printer work - mesh rendering
How GCode for 3D printer work – mesh rendering

The 3D printer works like a 2D printer to build a layer. Many layers sticked together build our initial 3D designed cube. The GCode is the way to program the large amount of movements needed to “sketch” the 3D printed layers.

To calculate every layer we have to use a special software called “slicer” that convert an “stl” file into a GCode file, specifically written for a 3D printer.

We can use a free slicing software like Repetier-Host to convert the 3D designed cube into a valid GCode. Below you can see one simulated printed layer inside a slicing software called Slic3r.

How GCode for 3D printer work - one simulated extruded layer
How GCode for 3D printer work – one simulated extruded layer

Hereafter the same layer represented as a 2D sketch. Please pay your attention to the lines that trace the movements of the extruder.

How GCode for 3D printer work - one layer path
How GCode for 3D printer work – one layer path

If you run the GCode programming commands, inside an open source 3D printer running a firmware called “Marlin”, you will build a real plastic cube. You can see some GCode programming lines below.

How GCode for 3D printer work - GCode command lines
How GCode for 3D printer work – GCode command lines

The grammar of GCode language

You can find a specific GCode language description inside the “Marlin” firmware, used to run a 3D printer. The GCode will be written as a plain text. It is made by a “command” and some data. A semicolon start a comment text.

The command start with a letter followed by a number.

First example:

I need to trace a segment starting from first point (located at coordinates X=5mm, Y=10mm and onto the bed plate at Z eight=0.2 mm) and ending to the second point (at coordinates X=15mm, Y=10mm and Z=0.2 mm). I have to write the following commands:

G21 ; set units to millimeters (according with your 3D design unit)
G90 ; absolute positioning (every movement will be on absolute coordinates)
G28 ; Go to origin on all axes (extruder head will be moved to coordinates 0, 0, 0 of the 3D printer)
G1 F600 ; set movement velocity to 10mm per second (600 mm per minute)
G1 Z1.000 ; raise extruder head above 1 millimiter
G1 X5.000 Y10.000 ; move the extruder head to first point
G1 Z0.200 ; move down the extruder head to the target height
G1 X15.000 Y10.000 ; move the extruder head to second point
; END of the GCode commands

What follows is a

short list of the GCode Marlin flavor language commands.

You can find the whole command list to Marlin GCode.

G20 ; set unit to inches
G21 ; set unit to millimeters
G0 ; linear movement with velocity set to maximum (rapid positioning)
G1 ; linear movement with velocity calculated with constrained acceleration (linear interpolation)
G28 ; print head move to touch the X Y and Z end stop sensors. Coordinates Origin 0,0,0
G90 ; set the movement as an absolute coordinates positioning
G91 ; set the movement as a shift positioning from the previous position. Relative movement.
G92 ; set position. Reset the counter position to a new number.
M109 S210; set extruder temperature to 210°C. Wait until target temperature has been reached.
E<number> ; move filament into the extruder by <number> mm (valid for 3D printer with 1 extruder only)
F<number> ; set velocity/feed rate, of any movement, to <number>
X<number> ; set position on the X axis to <number>
Y<number> ; set position on the Y axis to <number>
Z<number> ; set position on the Z axis to <number>

Print example:

I need to extrude a segment starting from first point (located at coordinates X=5mm, Y=10mm and onto the bed plate at Z eight=0.2 mm) and ending to the second point (at coordinates X=15mm, Y=10mm and Z=0.2 mm). I have to write the following commands:

G21 ; set units to millimeters (according with your 3D design unit)
G90 ; absolute positioning (every movement will be on absolute coordinates)
G28 ; Go to origin on all axes (extruder head will be moved to coordinates 0, 0, 0 of the 3D printer)
G1 F600 ; set movement velocity to 10mm per second (600 mm per minute)
G1 Z1.000 ; raise extruder head above 1 millimiter
M109 S210 ; set extruder temperature and wait until reached (valid for PLA filament)
G1 X5.000 Y10.000 ; move the extruder head to first point
G1 Z0.200 ; move down the extruder head to the target height
G1 X15.000 Y10.000 E1; move the extruder head to second point and extrude 1mm of melted plastic filament
G1 Z1.000 ; raise extruder head above 1 millimiter
G28 X ; goto the origin of X axes, equivalent to command G1 X0.000
M104 S0 ; set extruder temperature to 0, aka switch off the extruder
; END of the GCode commands

Math behind extrusion flow

You can understand how calculate the width of an extruded segment at this link: Understanding flow math. This calculation define how much E <number> you have to set to print correctly.

How to send GCode command to a 3D printer

You can send GCode commands, line by line, using at least two software:

  • Arduino IDE
  • Repetier Host

Arduino IDE is used to make the Marlin firmware. Repetier is used to slice a 3D stl file and send GCode commands, by serial communication, to 3D printers.

Arduino IDE

is able to send generic commands from its build-in Serial Monitor (Instrument -> Serial Monitor). Set a valid BaudRate like 115200. Write a line filled by GCode commands and clic to “Send”. You should notice some change in your 3D printer.

Repetier Host

Clic to “Manual Control” to view and send Gcode commands. You can write any commands into the “G-Code” empty window. Clic to “Send” to send commands to your 3D printer.

How GCode for 3D printer works - repetier host editor
How GCode for 3D printer works – repetier host editor

Pubblicità

Credits and Links

wikipedia: GCode programming language

wikipedia: printed circuit board

Repetier-Host: 3D printer slicing and GCode software

Marlin: 3D printer firmware and GCode description

Arduino IDE: how to send GCode commands

GCode tutorial

MIT first milling with GCode machine

Autore dell'articolo: Lucio Sciamanna

Lascia un commento