How to talk with machines: an introduction to G-code

G-code is a language in which people tell computerized machine tools how to make something. The how is defined by g-code instructions provided to a machine controller (industrial computer) that tells the motors where to move, how fast to move, and what path to follow.

Any machine which purpose is to move a tool, header or similar with motors, can be susceptible to be controlled by G-Code, this means that:

An example:

; generated by Slic3r 1.3.0 on 2019-01-05 at 18:56:22

; external perimeters extrusion width = 0.55mm (3.04mm^3/s)
; perimeters extrusion width = 0.70mm (7.88mm^3/s)
; infill extrusion width = 0.62mm (9.31mm^3/s)
; solid infill extrusion width = 0.70mm (2.63mm^3/s)
; top infill extrusion width = 0.70mm (1.97mm^3/s)
; support material extrusion width = 0.55mm (6.08mm^3/s)

M107 ; disable fan
M190 S60 ; set bed temperature and wait for it to be reached
M104 S200 ; set temperature
G28 ; home all axes
G1 Z5 F5000 ; lift nozzle

; Filament gcode

M109 S200 ; set temperature and wait for it to be reached
G21 ; set units to millimeters
G90 ; use absolute coordinates
M82 ; use absolute distances for extrusion
G92 E0 ; reset extrusion distance
G1 Z0.300 F7800.000 ; move to next layer (0)
G1 E-2.00000 F1500.00000 ; retract extruder 0
G92 E0 ; reset extrusion distance
G1 X-4.069 Y15.092 F7800.000 ; move to first skirt point
G1 E2.00000 F1500.00000 ; unretract extruder 0
G1 F1200
G1 X-15.091 Y4.091 E3.04030 ; skirt
G1 X-15.388 Y3.777 E3.06918 ; skirt
G1 X-15.729 Y3.373 E3.10450 ; skirt
G1 X-16.439 Y2.343 E3.18811 ; skirt
G1 X-16.649 Y1.956 E3.21753 ; skirt
G1 X-17.175 Y0.713 E3.30766 ; skirt
G1 X-17.275 Y0.386 E3.33049 ; skirt

G-code structure

Each line of G-code conforms an order to the machine. It can either imply general movement, or more machine specific tasks, such as enabling or disabling a fan, or setting a temperature setpoint.

To find out which order does what, we refer to specific code tables for the G-Code flavour we are using. A general introduction here. Some common ones:

Wondering what is Marlin Flavour?

Many 3D printers use Marlin Framework for generating the G-Code. It is a widely used and open source. Find the reference here.

G0 X12 ; move to 12mm on the X axis

G0 F1500 ; set the feedrate to 1500mm/minute

G1 X90.6 Y13.8 ; move to 90.6mm on the X axis and 13.8mm on the Y axis

Comunication

With most of the machines we communicate by using a serial connection that is a standart bidirectional connection protocol.
Used for communication between the machine board and a computer or other devices. All Arduino boards have at least one serial port (also known as a UART or USART), and some have several.

Lots of the machines have a  "Arduino" compatible microprocessor or use the same microprocessor as other arduino but with a custom pcb layout that includes the stepper drivers and other sensors,etc.

You can use the Arduino environment’s built-in serial monitor to communicate with the machine board. The most important things is to open the communication at the correct baudrate and the type of line end command we are sending.

What is the baudrate?

The baud rate is the rate at which information is transferred in a communication channel. In the serial port context, “9600 baud” means that the serial port is capable of transferring a maximum of 9600 bits per second.

At baud rates above 76,800, the cable length will need to be reduced. The higher the baud rate, the more sensitive the cable becomes to the quality of installation, such as how much of the wire is untwisted around each device.

Examples of GCODE commands

The G0 and G1 commands add a linear move to the queue to be performed after all previous moves are completed. These commands yield control back to the command parser as soon as the move is queued, but they may delay the command parser while awaiting a slot in the queue.

A linear move traces a straight line from one point to another, ensuring that the specified axes will arrive simultaneously at the given coordinates (by linear interpolation). The speed may change over time following an acceleration curve, according to the acceleration and jerk settings of the given axes.

A command like G1 F1000 sets the feedrate for all subsequent moves.

Marlin treats G0 (rapid linear movement) as an alias to G1 (rapid movement).

By convention, most G-code generators use G0 for non-extrusion movements (those without the E axis) and G1 for moves that include extrusion. This is meant to allow a kinematic system to, optionally, do a more rapid uninterpolated movement requiring much less calculation.

Notes

Parameters

Graphic Examples of commmands

G0 movements

The G0 command moves the machine at maximum travel speed to whatever coordinates follow G0 The machine will move in a coordinated fashion, and both axes complete their travel at the same time. G0 is not used for cutting. Instead, it’s used to move the machine quickly to begin a job or move to another operation within the same job.

G1 movements

A G1 command (Figure B) is similar but tells the machine to move at a specified rate called the feed rate (F):

G2 (Clockwise Motion) movements

Setting the mode to G2 and specifying the offset from center creates clockwise motion between the starting point and the specified ending points.

G3 (Counterclockwise Motion) movements

Just like G2, the G3 command creates an arc between two points. Whereas G2 specifies clockwise motion, G3 specifies counterclockwise motion between the points

G17/G18/G19 (Working Planes)

These modes set the plane to be machined. Typically G17 is used and is the default for most hobby machines, but two other planes can be used in a three-axis machine: • G17 = x/y plane • G18 = z/x plane • G19 = y/z plane

G20/21 (Inches or Millimeters)

The G21 and G20 commands determine the G-code units, either inches or millimeters: - G21 = millimeters - G20 = inches Here’s an example that’s set to millimeters: G21 G17 G90

G28 (Referencing Home)

A simple G28 command sends the machine to its home position. Adding coordinates will define an intermediate point to go to, before homing (to avoid collisions), like this: G28 Z0 Some machines require a G28.1 command to define the home position coordinates: G28.1 X0 Y0 Z0

G90 (Absolute Mode)

G90 causes units to be interpreted as absolute coordinates. This is the most common mode for hobby-grade CNC machines; it’s the “default” mode. Absolute coordinates will be interpreted as exactly that — absolute. G0 X10 will send the machine to x = 10. It will not send the x-axis to “10 more” units from where it’s currently located.

G91 (Incremental Mode)

The opposite mode of G90. Setting incremental mode means that every command issued will move your machine the specified number of units from its current point.

For example, in incremental mode, G1 X1 will advance the machine 1 unit in the x direction, regardless of its current location.

G-Code Rules

Just like a math equation, G-code has its own rules about the order of operations. Here are the most common, in order of precedence (that is, comments will be interpreted first and the change tool will be interpreted last):

- Comments
- Feed rate
- Spindle speed
- Select tool
- Change tool

When you issue a G command, you are putting the machine into that mode. If you issue a G1 command, such as G1 X5 Y13, then the machine moves to X5 Y13.

If you issue another set of coordinates, you do not need to issue another G1 command. Why? Because the machine is in G1 mode until you change it to something else like G0 or G2 or G3.

Feeds, Speeds, and Tools

Simple G-code commands are used for setting the speed, feed, and tool parameters.

“F” Is for “Feed”

The F command sets the feed rate; the machine operates at the set feed rate when G1 is used, and subsequent G1 commands will execute at the set F value.

If the feed rate (F) is not set once before the first G1 call, either an error will occur or the machine will operate at its “default” feed rate. An example of a valid F command: G1 F1500 X100 Y100

“S” Is for “Spindle Speed”

The S command sets the spindle speed, typically in revolutions per minute (RPM). An example of a valid S command: S10000

“T” Is for “Tool”

The T command is used in conjunction with M6 (M-codes are machine action codes) to specify the tool number to be used for cutting the current file: M6 T1 On industrial machines, an M6 T command usually produces a tool change with an automatic tool changer. On hobby machines with no tool changer available, issuing a new M6 T command will generally cause the machine to issue itself a feed-hold command, wait for the operator to change the tool, and then continue the job after the “resume” button is pressed.

G-Code Viewer

Grasshopper files

-Line Gcode generator -Multiline Gcode generator

Resources

Basics commands

Getting started