# G-Code Crash Course for Working with Plotters

I specifically wrote about serial programming for Arduino previously because I want to feed an Arduino controlling an XY-plotter G-codes, over a serial interface.

## What are G-Codes and how are they different from say SVG?

Both SVG, PDF and G-Codes do in a way represent a vector graphics image. But when plotting, routing or 3D printing a regular vector graphics format is not well suited.

A typical vector graphics format is very far away from how a machine operates. A plotter e.g. wants to be told, when to move its pen down on the paper, when to lift it up, and how to move to the different positions where it will start moving.

G-Codes give these kind of specific instructions. E.g. it will typically instruct a plotter to move rapidly to a position when the pen is not touching a surface, while switching to lower speed when the pen is touching the surface.

For dealing with routers you also want instructions about how fast the drill bit should spin, how deep it should go into the material. For 3D printers you want instructions of how fast material should be deposited etc.

However this is intended as a minimal guide to understand a minimal set of codes needed for 2D plotting.

## G-Codes for 2D Plotting

To get an idea of how G-codes work, let me first show you the beginning of an example file.

`%G90G49M3 S15000G0 X20.456 Y20.910 Z15.000G1   Z35.000 F2500G1 X18.202 Y13.354  F2500X16.806 Y13.352 X15.300 Y18.656`

The percentage sign tends to mark the beginning of the data. Useful for serial communication I presume.

So there are a couple of things to observe about this little code segment:

An instruction is typically a letter and some numbers. The first letter say what category of command we are dealing with. Anything starting with `G` says something about how the plotter should move. Should it move fast, slow, relative, absolute etc.

X, Y and Z give coordinates.

## Each Line an Action

Each line is an action. So these two code segments do not do the same thing. This first line will move the pen to coordinate (20.456, 20.910, 15.00) in one motion. Coordinates are assumed to be in millimeters.

`G0 X20.456 Y20.910 Z15.000`

However this segment splits up the movements. So first we move 20.456 mm along the x-axis. Once that is done, we move 20.910 mm along the y-axis. And finally when that is done we adjust the height of the pen to 15.0 mm.

`G0 X20.456 Y20.910 Z15.000`

## Mode Based

Instructions like `G0`, `G1` and others start modes. E.g. `G0` means move fast, while G1 means move slow. However once you've issues `G0`, it will keep moving fast for every coordinate given on the following lines. It will not start to move slow until it hits a line starting with `G1`.

## G90 and G91 for Absolute and Relative Positioning

The coordinates given by X, Y and Z instructions can be absolute or relative to current position. `G90` means absolute position. `G91` means relative.

`G91G0 X15 Y10`

So the example above would mean move fast 15 mm along x-axis from where pen is currently residing and 10 mm along y-axis relative to current position.

`G90` is probably what you want to use most of the time when drawing something. However I can see `G91` as very useful when testing the plotter and trying to calibrate it. I've noticed when trying my XY-plotter that it flies of rapidly too far in one direction and crashes into the frame. Obviously something is wrong, and my stop switches don't work.

However it would be nice to have a bit more control, and know your stepper motors will only moves a certain distance from where you have presently placed the pen.

## G0 and G1 for Positioning and Drawing

With G0 you go max speed to a position. That would be bad if you are say routing something with a drill bit. Thus it is what you should prefer doing when the machine is just positioning itself. Such as when the pen is lifted and moving into position.

Use G1, when you are drawing and want slower speed for better control.

## F, Feed Rate

The F code does not matter for XY plotting, but since it pops up in the example files I have for plotting and you will probably see it often, I guess it is good to know about. Example line:

`G1   Z35.000 F2500`

It simply sets the feed rate to 2500 whatever the unit is. Feed rate is how fast the drill bit spins. So this instruction changes to slow movement (G1) and change the vertical position of the drill bit (Z35), while changing the rotation speed of the drill bit (F2500) assuming we are dealing with a router.

# Final Remarks

For more detailed explanation there are lots of great resources out there. I particularly liked this one from Simplify3D.

Written by