Ordinary G-code states every number outright. Macro programming lets the program compute them: variables hold values, loops repeat work, and IF statements make decisions. On Fanuc-style lathes this layer is called Custom Macro B, and the extended G-code syntax it adds turns one program into a family of programs. Two short turning examples carry most of the idea.

What new words does macro add?

A compact set, which is the good news:

ElementSyntaxWhat it does
Variable#101 = 2.5Stores a number
Arithmetic#101 = #101 + #100Computes values
LoopWHILE [...] DO1END1Repeats blocks
ConditionIF [#101 GT 30.] GOTO 90Branches
Macro callG65 P9010 A5. B2.Runs a macro with arguments

Variables come in ranges with different lifespans: locals (#1 to #33) reset per macro call, commons (#100 range) survive within a session, and the permanent range (#500 and up) survives power-off, the full map in #100 vs #500 variable differences, which the second example uses.

Example one: a parametric grooving loop

A shaft needs identical grooves every few millimeters. Plain G-code repeats the same four blocks per groove; the macro version reads three variables and loops:

#100 = 5.                  (pitch between grooves)
#101 = 10.                 (first groove, mm from face)
#102 = 30.                 (last groove position)
WHILE [#101 LE #102] DO1
  G00 Z[0 - #101]          (position at the groove)
  G01 X16. F0.08           (plunge the groove)
  G00 X22.                 (retract clear)
  #101 = #101 + #100       (step to the next groove)
END1
G00 Z5.

Five grooves or fifty, the program is the same length, and re-spacing the pattern means editing one number. The Z[0 - #101] expression is the habit worth copying: compute inside brackets so the variable can stay a readable positive distance.

Example two: a permanent parts counter

Permanent variables remember values across power cycles, which makes a one-line production counter:

#500 = #500 + 1            (count this part)
IF [#500 LT 200.] GOTO 99  (keep running until 200)
M00                        (operator: insert change due)
N99 M30

The machine now taps the operator’s shoulder at part 200 for an insert change, with the count surviving weekend shutdowns. The same pattern drives warm-up gating, offset nudges per N parts, and the probing routines that make up the hand-written macro layer described in why machinists still write G-code manually.

What are the gotchas?

Three, all manageable. The option gate: on many Fanuc-style controls Macro B is a paid option, and a control without it throws the improper-code alarm on macro statements, the situation covered in Fanuc alarm 010; check the option list first. Dialect drift: the concepts are universal but spellings differ, and LinuxCNC’s O-codes (o100 whileo100 endwhile) deliver the same loops with different syntax, so port the logic, never the literal text. Hidden numbers: a macro’s positions live in variables, so a wrong input cuts a wrong part with perfect confidence; prove out with conservative values and single block, watching the variable display, more carefully than any plain program.

Where do macros fit in your progression?

They are the top rung of the manual-programming ladder: after fluent reading, setup work, and supervised edits, the path mapped in from CNC operator to programmer. Senior machinists who own probing macros and parametric families earn that standing one working example at a time, and the two above are honest first steps. The plain-code layer underneath them, the standard motion vocabulary, has to be reflex first, which is what a recall routine on the G-code practice hub builds.

When the same structure serves a whole catalog of sizes, the discipline scales up into family-of-parts macro programming, variable sheets and all. One more turning niche that leans on exactly this loop structure: broaching keyways on a lathe, where the stroke-step-repeat pattern is a macro begging to be written.

Bottom line

Macro programming adds variables, loops, and logic to turning G-code: a grooving loop shows the WHILE pattern, a permanent counter shows #500 variables, and together they cover most everyday macro work. Mind the option gate, the dialect spellings, and the hidden numbers, and prove out harder than usual. The syntax is small; the leverage is not.

Sources

Frequently asked questions

What is macro programming in CNC turning?

An extension of G-code, Custom Macro B on Fanuc-style controls, adding variables, arithmetic, IF logic, and WHILE loops, so one program covers a family of parts by reading values instead of fixed numbers.

What is a simple macro example for a lathe?

A grooving loop: three variables hold first position, pitch, and last position; a WHILE loop positions, plunges, retracts, and steps until done. Re-spacing every groove means editing one number.

Do all CNC lathes support macro programming?

No. Macro B is a paid option on many Fanuc-style controls, which reject macro statements without it. LinuxCNC includes equivalent O-codes natively. Check the option list first.

What is the best way to learn macro programming for turning?

Make the plain code layer reflex first. A free app like G-Code Sprint drills the everyday codes and repeats whichever ones you miss; macros then become a short list of new words.

G-Code Sprint is a study and practice tool only. Always follow your instructor, employer, machine manual, and shop safety procedures.