How to Set X, Y, and Z Offsets with M851 G-Code? (Detailed)

Correctly setting the X, Y, and Z offsets is a vital step of the 3D printing process, as failing to do so causes issues such as the printer printing off the bed or in the air; and the printhead bumping into the bed.

While it requires a certain level of knowledge, setting these offsets with the M851 G-code is the most straightforward method to get your 3D printer up and working as quickly as possible.

So, how to set the X, Y, and Z offsets with M851 G-Code?

You can set the X, Y, and Z offsets by passing their respective flags to the M851 G-code alongside the value you want to set the offsets to.

An example would be -> M851 X1 Y3 Z5, which would set the X offset to 1, the Y offset to 3, and the Z offset to 5.

After setting the offsets, you should use the M500 G-code to store the values to avoid configuring the offsets every time you restart the printer.

As each of these offsets (especially Z offset) require a different approach to configure, we will be taking a deeper look into how you can calculate their values and configure them as correctly as possible in the upcoming sections.

How to Set the Z Offset with M851 G-Code?

Setting the Z offset is a question we hear about a lot, as Z is the offset that takes the highest amount of effort to calculate out of the three.

Here are the steps we recommend taking to set the Z offset with M851 G-code.

  1. Start by resetting the Z offset. You can do this with the command M851 Z0, which will ensure that there is no pre-existing offset to throw our calculations off.
  2. Home the printer. Use the G28 G-code to trigger auto-homing, which will bring all the axes to the 0 point.
  3. Disable hardware endstops. You can use the M121 G-code to do so.
  4. Disable software endstops. Use the M211 G-code with the S0 parameter to do so.
  5. Place a piece of paper on the bed in line with the nozzle. We will be using this piece of paper to know when to stop moving the nozzle in the next step.
  6. Bring the nozzle down slowly until it touches the piece of paper. You can do this via either the built-in panel or the software of your choice.
  7. Write the Z-axis value down and run the M851 G-code with it. For instance, if your Z-axis shows -2.5, run M851 Z-2.5.

After these steps, follow the steps below to test the new Z offset.

  1. Home the printer. Use the command G28 once again. This time, the panel should show the Z-axis as the negative of the Z-offset value you have just set. For instance, if you set -2.5, it would show 2.5.
  2. Set the Z-axis to 0. Use the command G0 Z0 to move the nozzle to the 0-point of the Z-axis.

The nozzle should touch the paper after these two steps, which would confirm that the Z offset is correctly set.

Finally, use the G-codes M120 and M211 S1 to enable the hardware and software endstops, and run the M500 command to save your settings to the EEPROM, which will allow your 3D printer to load them back automatically every time you boot it.

How to Set the X and Y Offsets with M851 G-Code?

Calculating the X and Y offsets is a straightforward task you can perform in a mere few minutes.

Here is how you can set the X and Y offsets with the M851 G-Code:

  1. Place a piece of paper on the bed directly below the nozzle. We will be using this piece of paper to measure the distance between the probe and the nozzle.
  2. Move the Z-axis of the nozzle to a point where it slightly touches the paper. Mark this point on the paper to make it more visible.
  3. Move the X and Y axes until the probe is over the point you have marked. Note the X and Y values of the axes, which will be your offset (horizontal distances between the probe and the nozzle) values.
  4. Set the offsets with M851. An example would be M851 X3 Y5 to set X offset to 3 and Y offset to 5.
  5. Save the settings to EEPROM. You can use the M500 command to do so.

M851 Not Working – What to Do?

An issue in which the offsets don’t function as intended after you set them with the M851 G-code is one of the most commonly talked about problems in the community.

Here are the things we recommend trying if M851 isn’t working for you.

  • Double-check the offset values. You can get a detailed report of the settings with the M503 G-code, where you can double-check the offset values. Using this command is particularly useful to see whether the offsets are not working even though they are active or due to not being stored at all.
  • Ensure that you save the configuration to the EEPROM. If the offsets are not working after a power cycle, not being stored is a highly likely culprit. To save to EEPROM, use the M500 command after setting the offsets. To ensure that the printer loads the configuration, execute M501 after the printer boots.
  • Hardcode the offset values to the firmware. Sometimes, hardcoding the offsets to the firmware works even when saving them to the EEPROM doesn’t. For instance, you can do this in Marlin firmware by setting the NOZZLE_TO_PROBE_OFFSET variable in the config file before you build and flash it to your 3D printer.
  • Flash an updated version of the firmware. In rare cases, the firmware itself may be the problem. Flashing an updated version of the firmware can solve your problem in such a scenario, making it worth giving it a try when nothing else works.

Wrapping Up

Correctly setting the X, Y, and Z offsets with the M851 G-code isn’t too challenging once you know the appropriate steps you should take, even though M851 is one of the more technically complex G-codes to utilize.

As these offsets being correct is absolutely crucial for the printing process to go smoothly, we highly recommend double-checking all the values you have calculated and ensuring that the printer correctly saved these values.

If you found this guide helpful, stay tuned for more detailed G-code explanations and 3D printing tips in the future!