Turning my 3D-Printer into a Laser Engraver 4: G-code

Before it’s possible to turn images into instructions to the machine, those instructions need to be understood. Luckily, G-code, the name and format of those instructions, is very easy to get into.

G-code is plaintext and somewhat human readable. Reminiscent of assembly language to a degree, but, as far as I can tell, contains no variables and no conditional logic. There is one command that implements an unconditional jump, which can only be used to skip code  by jumping forwards (why would you do that?) or loop infinitely by jumping backwards. I see no reason to ever use this in printing/engraving.

With very little knowledge about G-code, I just jump right in by inspecting a few files generated for printing. They all share the same first couple of lines, with a few comments added from the generating tool:

M106 S0
M140 S60
M190 S60
M104 S200 T0
M109 S200 T0
G28 ; home all axes
G1 X0 Y20 Z0.2 F3000 ; get ready to prime
G92 E0 ; reset extrusion distance
G1 X100 E20 F600 ; prime nozzle
G92 E0
G1 E-2.5000 F1800
G1 Z0.090 F1000
; process Process1
; layer 1, Z = 0.090
; tool H0.100 W0.528
; skirt
G1 X81.171 Y87.833 F4800
G1 E0.0000 F540
G92 E0


This is essentially meaningless to me, but equipped with a command documentation, as can be found here, for example, the meaning of uncommented code quickly becomes clear.

  • G90 sets the coordinate system of the machine to absolute values. That is, when I tell the machine to “Move 1 2 3” it will move to position x=1, y=2, z=3, rather than moving by those distances. A relative system is also possible.
  • M82 does the same thing for the extruder motor. I do not use that motor, so the command is irrelevant for me.
  • M106 S0 sets the nozzle fan – in my case the laser – to 0 power, or off.
  • M140 S60, M190 S60 sets the temperature of the print bed to 60°C and waits until this is reached. Irrelevant for me.
  • M104 S200 T0, M109 S200 T0 does the same thing for the nozzle temperature (200°C). Again, irrelevant.
  • G28 – Home all axes. This is the first command that moves anything. Homing means that the print head moves backwards in all axes until it hits the limiting switch. Hitting that switch tells the machine that the print head has reached the origin in this axis – in a way, “come home” – and is the only way the printer ever knows where the print head actually is. All other movement is relative, with a sprinkle of wishful thinking.

Most other commands are not interesting here, as they concern “priming” the nozzle, that is, extruding a little bit of filament and wiping it.

There are three commands to talk about more:

  • M106 S<nnn> can use numbers from 0 to 255 in place of <nnn>. 0 turns the laser off completely, 255 turns it on completely. Intermediate values are not a good idea in my case, due to the precarious nature of using an Arduino as a voltage regulator, see previous post.
  • G1 X<x> Y<y> Z<z> moves the print head in a straight line to the point (x, y, z). One can also omit any of the axes. For example, we won’t need to change Z when engraving a flat plate, so we call it at most once, when getting into starting position.
  • G92 X0 resets the internal counter of where along the X-axis the print head currently is. This could, for example, to first home all axes, then move the print head such that the laser is in the lower left of the actual plate to be engraved, then resetting X and Y. This will place the origin of movement to the origin of the plate, which can simplify generating code quite a bit.

The initializing code for 3D-Printing boils down quite a lot for engraving, as many features are not needed. Ultimately, I ended up with just:

G90 ; Set coordinate system to absolute values in X, Y, Z
M106 S0 ; Turn off the laser
G28 ; Home all axes. Depending on the material to print on, homing Z may be undesirable/bad
G1 X65.00 Y30.00 Z48.00 ; Move laser to lower left corner of printing bed
G92 X0 Y0 ; Reset Coordinate System
M106 S255; Turn on the laser


From here, I need to read the image values to decide where to move the print head and when to turn on/off the laser.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.