How to Control the BLTouch with G-Code Commands? (Explained)

Automatic bed leveling is, without a doubt, one of the best things that have happened to 3D printing, especially considering the number of printing processes that have failed due to an incorrectly leveled bed due to the inconsistency that comes with the process of manually leveling the bed.

When automatic bed leveling is the topic, the BLTouch ABL sensor is often the first name that pops up in most 3D printing enthusiasts’ minds, which is no surprise with the many features it offers, ranging from its support for all types of build surfaces to the accurate results it produces.

Today, we will be examining the process of controlling the BLTouch automatic bed leveling sensor with G-code commands, which allows us to get the BLTouch to perform actions such as toggling the position of the pin manually, mainly for the purposes of testing and troubleshooting.

So, how can you control the BLTouch by issuing G-code commands to your 3D printer?

To control your BLTouch by issuing G-code to your 3D printer, you will need to invoke the G-code command that allows you to control servos along with the index of the servo and the position to set the servo to, with each servo position corresponding to a different action.

Next up, we will dive deeper into the process of controlling the BLTouch auto bed leveling sensor with G-code commands, go through a list of commands that the BLTouch supports, and look at different ways of sending the G-code commands to the BLTouch.

How to Control the BLTouch with G-Code Commands?

Just as it’s possible to control the functionalities of your 3D printer by executing G-code commands, it’s also possible to run some commands that will directly prompt the BLTouch to invoke some actions, such as pushing the pin down or pulling it up.

The process of controlling the BLTouch with G-code involves utilizing the command that allows you to set the position of servos and passing the specific servo position that corresponds to the BLTouch action you would like to execute as a parameter.

As the G-code command for setting the position of a servo and how the firmware interprets the servo angle value depends on the firmware you use, let’s go over some of the popular firmware and how you can control your BLTouch with them.

In Marlin firmware, the Servo Position G-code command is M280, and it follows the format M280 P<index> S<position>, with the value for the servo angle parameter being in terms of degrees.

An example would be M280 P0 S10, sending a servo angle degree of 10 to the servo number 0, which, in the case of BLTouch, would trigger the action of deploying the push-pin.

In Repetier, the G-code command for setting the servo position is M340, and while it also follows a similar format to Marlin, with the syntax M340 P<index> S<position>, the value that the servo angle parameter accepts is a PWM value that falls between 500 and 2500.

An example for Repetier would be M340 P0 S647, sending a PWM value of 647 to the servo number 0, which would, once again, trigger the action of deploying the push-pin for the BLTouch.

Finally, let’s look at controlling the BLTouch sensor in Klipper, which is slightly different due to the usage of a system called extended G-code commands. 

For Klipper, the command is called BLTOUCH_DEBUG, a command created explicitly for controlling the BLTouch and follows the syntax BLTOUCH_DEBUG COMMAND=<command>.

An example of the usage of this command in Klipper would be BLTOUCH_DEBUG COMMAND=pin_down, which would directly issue the BLTouch to deploy its push-pin without the need for any servo commands or angles.

Which G-Code Commands Does the BLTouch Support?

Since the BLTouch has a unique set of commands that you can execute by passing the corresponding values to the G-code that allows you to control servos, being familiar with the functionality of each command will come in handy to run the correct command whenever the occasion arises.

Below, we have listed all of the servo angles and PWM values that the BLTouch supports for the purposes of control with the servo position G-code command and the corresponding BLTouch actions they run.

  • Deploy Push-pin – PWM: 647 | Degrees: 10
  • Alarm Release – PWM: 1162 | Degrees: 60
  • Stow Push-pin – PWM: 1473 | Degrees: 90
  • Self-test – PWM: 1782 | Degrees: 120
  • Alarm Release & Stow Push-pin – PWM: 2194 | Degrees: 160

While there are a few more BLTouch commands, primarily related to the EEPROM, we recommend reading the BLTouch documentation to get more information about their usage as you won’t need them except for very specific cases.

How to Send G-Code Commands to the BLTouch?

Executing the BLTouch G-code commands will require you to have an interface that will directly allow you to issue lines of G-code for convenience, unlike the printing process, where you can utilize an SD card that contains a file with all the required G-code lines.

It’s possible to establish a direct link between your computer and your 3D printer to send G-code commands to the BLTouch in two ways: to either create the connection with a USB cable or network them.

To send G-code to your 3D printer (and the BLTouch) through a network, whether wired or wireless, you will need to connect your 3D printer to a server, such as OctoPrint running on a Raspberry Pi, and then connect to the OctoPrint interface running on the Raspberry Pi via your computer.

As OctoPrint provides an interface for directly sending G-code commands to your printer, you can easily send the servo position G-code along with the angle / PWM value corresponding to the BLTouch command you would like to run.

On the other hand, to send G-code commands to the BLTouch through a USB connection, you can use software such as Pronterface that will instantly allow you to interface with your 3D printer as soon as you establish a successful USB connection between your 3D printer and your computer.

Similar to OctoPrint, Pronterface also has a section that you can utilize to send G-code commands to your printer, allowing you to send the G-code for setting the servo position and the value required to run the BLTouch command of your preference.

Wrapping Up

While controlling the BLTouch manually through G-code isn’t something you will have to do frequently during standard operation, the commands can definitely come in handy in cases where troubleshooting and testing are required as they make it possible to invoke any of the BLTouch’s actions quickly.

To quickly recap, the G-code command that allows you to set the servo position, such as M280 in Marlin firmware, is what you can use to send commands to the BLTouch, as the BLTouch sensor is essentially a servo that is connected to the servo pin of the mainboard.

Since the BLTouch has some pre-defined servo angle positions corresponding to each of its commands, passing these servo angles to the servo position G-code as a parameter will allow you to execute any command of your preference.

Happy printing!