Quick Guide to the N CNC Code [Program Line Number]

a graphic of a cnc machine with text that says learn g code today N program line number

N codes are used in a CNC program to identify the block or line of the program.

In most programs, the blocks increase by 5 or 10 on each line. This allows you to add lines in between if the program needs to be edited.

N codes are not required though and can be left out if the programmer chooses.

Some example N code sequences are shown below:

Increment By

N Code Sequence

1

N01

N02

N03

N04

N05

5

N05

N10

N15

N20

N25

10

N10

N20

N30

N40

N50

Want to learn more about CNC G Code?

Where does the N code show up in the CNC program?

N codes show up at the beginning of each block and typically run through the entire program.

There are some programmers who chose to only use line numbers (N codes) on important lines such as the start of a new tool.

Why are N codes used?

N codes help the programmer follow along with the program. This is especially true for new programmers.

Identifying each line of code with a sequence (N) number makes it easier to find or modify different sections of the program.

Drawbacks to using N codes

Using N codes throughout your program does make the program file size larger because it contains more characters. 

This is more of a problem for older machines with very limited memory.

Format for using an N code

The N-code should be the first one to show up on every block (line) of the program.

This is how it looks in practice:

N10 G90 F100 S1200 T01.01

N20 G00 X36 Z10

N30 X26 Z2

N40 G01 Z-12 M03

N50 X36 M05

N60 G00 Z10

N70 M30

Notice how the N numbers jump up by 10 on each line.

If we needed to insert a couple more lines of code into the program it might end up looking like this:

N10 G90 F100 S1200 T01.01

N20 G00 X36 Z10

N30 X26 Z2

N34 Y20

N38 X40

N40 G01 Z-12 M03

N50 X36 M05

N60 G00 Z10

N70 M30

When editing the program we tried to keep the lines spaced out in case it needs to be edited again in the future. We could have numbered the new lines as N31 and N32 or N32 and N34. It really is up to the person editing the program. 

Good practice is to try and leave gaps in the numbers if possible.

Best way to number your N codes

The standard recommended format for numbering your lines of code is to increment by 5 or 10. 

This will leave spaces between each line in case the programmer needs to edit the program in the future.

N codes when using the M97 code

The M97 is used for calling a subprogram by line number.

The format for using an M97 code is:

M97 P125

In this example, the machine will read the subprogram call and then jump to line N125 in the current program. Using the M97 command allows you to jump around your program.

When using subprograms with M97, the best practice is to put your subprogram lines at the very bottom of the program. Doing this will clearly separate your subprogram from the rest of the program.

When you use the M99 code to end a subprogram called with the M97 code, the machine will jump back to the next line after the M97 was used.

Frequently asked questions about N codes

Do you have to use sequence (N) numbers in your program?

No. Sequence numbers with the N code are not required but they are highly recommended. Using them will make your program easier to read and follow.

Additionally, increasing the size of the program is less of an issue with newer machines than it was in years past.

Can you use a negative N number?

No, it is not possible to use negative numbers to identify the lines of a program.

Can you use a N number with a decimal point?

No, you will need to use whole numbers when using the N code.

What happens if my sequence (N) numbers are out of order?

If you sequence numbers are out of order such as:

N5

N20

N15

N10

N30

The machine will still read them in the order they are shown above. Unless you are using subprograms, the machine just ignores the sequence (N) numbers.

Want to learn more about CNC G Code?

Leave a Comment