Automatic Bed Leveling – Anycubic i3 Mega

Manual leveling bed in a 3d printer can be an extremely tedious task and at the beginning it is simply difficult. However, a well-leveled table is extremely important for proper execution of the printout. Bad bed leveling may lead to print not sticking to the bed. There may be gaps in the initial print layer or the layers will be too thin. Filament can accumulate around the nozzle as you print the first layers and numerous other problems. Therefore, the auto-leveling sensor can be a great help in this process.


Auto bed leveling sensor allows us to create a 3D image of our table and use it to correct our prints. With this solution, the software uses a 3d image to correct the Z axis positions on an ongoing basis (e.g. if the A point is higher than the B point, while moving software will decrease the Z axis positions).


So much for theory. There are various auto leveling sensors on the market, but I will use the Creality3D BL Touch that I received from a friend for which I thank him. The sensor will be installed in the Anycubic i3 Mega printer. Unfortunately, this model does not have a direct interface to install this module, so there will be much software and hardware work.



Software Instalation

The default Anycubic i3 Mega software does not supports auto bed level sensor, therefore it is necessary to install open source software that supports such functions. In my opinion, the best solution is Marlin-Ai3M.


Marlin-Ai3M is a custom firmware for Anycubic I3 Mega and Mega-S printers based on the popular Marlin Firmware. This software has many advantages, such as automatic bed leveling, but also allows PID controlled heatbed, enables better fan power supply or “S-Curve Acceleration” which positively affects the print quality and reduces vibrations. Those are just a few advantages of this software.


When using this software, however, we must remember that:
We will not be able to restart our print after power failure (for me this function never worked properly)
– When turning on the printer, we will not get the startup sound (which can be an advantage)
– The sd card must not contain files and folders with special characters


More information can be found on the official website of the project


So lets download the latest software (I personally use v1.4.6 stable (Marlin 1.1.9)) from . There are already three compiled hex files provided. Unfortunately, none of them has BL Touch support enabled, so we must download uncompiled source code version (source


If we don’t have Arduino Ide installed yet then now we install it and open the Marlin.ino file. This will open the whole project for us, but we are only interested in the Configuration.h file. It is a configuration file for our printer. Depending on what modifications we have already made and what we want to make, we can set it here.


Lines 559-569 are responsible for the type of drivers used in the printer. By default, A4988 is installed, but my printer has undergone modifications that can be read here and I have TMC2208 drivers with inverted sockets installed.

For me, this element looks like this:

#define X_DRIVER_TYPE TMC2208_STANDALONE // comment out for stock drivers
#define Y_DRIVER_TYPE TMC2208_STANDALONE // comment out for stock drivers
#define Z_DRIVER_TYPE TMC2208_STANDALONE // comment out for stock drivers
#define Z2_DRIVER_TYPE TMC2208_STANDALONE // comment out for stock drivers
#define E0_DRIVER_TYPE TMC2208_STANDALONE // comment out for stock drivers
#define E1_DRIVER_TYPE TMC2208_STANDALONE // comment out for stock drivers

Lines 856-868 are responsible for the direction of the drivers movement, these values depend on what drivers are installed and whether we also inverted socket during the installation of the drivers. For me, this fragment looks like this (the code itself is well described by the creators, thanks to which it is easy to understand what we have to do, even if we have other drivers):


// Invert the stepper direction. Change (or reverse the motor connector) if an axis goes the wrong way.
#define INVERT_X_DIR true // set to true for stock drivers or TMC2208 with reversed connectors
#define INVERT_Y_DIR false // set to false for stock drivers or TMC2208 with reversed connectors
#define INVERT_Z_DIR false // set to false for stock drivers or TMC2208 with reversed connectors

// @section extruder

// For direct drive extruder v9 set to true, for geared extruder set to false.
#define INVERT_E0_DIR false // set to false for stock drivers or TMC2208 with reversed connectors
#define INVERT_E1_DIR false // set to false for stock drivers or TMC2208 with reversed connectors
#define INVERT_E2_DIR false
#define INVERT_E3_DIR false
#define INVERT_E4_DIR false

However, what may be of interest to the most people is the part responsible for the work of the auto leveling sensor. First we need to choose the type of probing. Lines 672-849 are responsible for this, but we only need to comment on lines 718 responsible for manual probing


and uncomment lines 736-739 responsible for probing with BL Touch:

#define BLTOUCH
#define BLTOUCH_DELAY 375 // (ms) Enable and increase if needed

Another thing we need to configure is which technique we will use when leveling the table. Lines 982-986 are responsible for this.


More about various techniques can be read at however, I personally use the recommended and most accurate technique (and the most time-consuming) AUTO_BED_LEVELING_UBL, the code looks like this:


With this type of bed leveling, we still need to uncomment line 992


Finally, we uncomment line 1154, thanks to which our working table will be homing to the center



Project prepared in this way need to be exported to .hex file. To do this, in arduino ide, select Tools → Board → Arduino Mega and Mega 2560. Next, go to Sketch → Export the compiled program.


At the end of this article, you can download the hex file I generated and the entire configuration file.


Finally, we have to upload the prepared custom firmware to the printer. For this we will use the Cura program. We connect our printer with the cable provided by the manufacturer. If necessary, we install drivers. We turn on the cura program. There we go to Settings → Printer → Manage Printers. Select our printer and click Update firmware → upload custom firmware. We select generated hex file after about two minutes, our printer should be ready to work.



Hardware Instalation


After finishing the software part, we can go to the hardware. As I mentioned at the beginning, the Anycubic i3 Mega does not have a built-in support for the auto leveling sensor, so we have some soldering to do.


To a small printed circuit board located at the top of the trolley holding the hotend. We need to solder two cables responsible for grounding and signal.




We connect our BL Touch to the newly soldered cables. The black cable is connected to the ground, white to the signal.


Once we have it done we can attach the sensor to the trolley. For this purpose, it is best to print this model



This model is mounted with two screws to the upper ventilation grille. Screw the sensor to the bottom of the model with the next two screws. In my case, it was necessary to lower the BL Touch a bit, so I put three small pads between the model and the BL Touch.



The last issue is connecting the sensor to the motherboard. The three colored cable of the BL Touch need to be pulled out through the upper outlet in the grille and led to the motherboard.


This cable must be connected to the following inputs:



It is important that we connect our BL Touch in accordance with the above-mentioned pins. In the case of my sensor, the color of the cables was as follows: red – 5v, blue – ground, yellow – signal. For me, it was necessary to change the power and ground cables to match the pins on the motherboard.



Preparing for printing


If everything has been done correctly, BL Touch should undergo auto-diagnosis during start-up. This means that during startup, the device should release and retract the probe 2-3 times. The only thing left for us to do is to use the newly installed sensor. The easiest way is to add these commands to each of our printouts:

G28; Home axis
M851 Z-3;
G29 P1 //probing
G29 P3 //Extrapolates leveling topology
G29 F10 //Sets fade height to 10mm
G29 S1 //Saves the bed topology
G29 A //Ensures UBL is activated
G29 L1 //Loads mesh #1
M500; Save firmware
M420 S1; Enable BLTouch


In the cura software, we can do it by going to Settings –> printers –> manage printers –> machine settings:



M851 Z-3; is responsible for the distance from the nozzle to the probe trigger point in the Z axis. We can determine it by trial and error, but the best way is to follow Marlin’s documentation, that is:


– Home the Z axis
– Raise the Z axis a little and release the BL Touch probe (command M280 P0 S10)
– In small steps lower the Z axis until the probe reacts
– Enter the obtained value on the Z axis into the M851 Z-x command


We prepare any printout. If everything works, we should get an effect similar to the following:



Testing and Troubleshooting


Many problems may arise during the installation of BL Touch. Cables may be incorrectly connected, software may be incorrectly configured or even the sensor itself may be defective and finding the problem is not always easy.


One of the things we can do at the beginning is to check if the sensor is receiving commands.
M119 – probe state (open or triggered)
M280 P0 S10 – probe release
M280 P0 S90 – probe retract
M280 P0 S120 – Test mode
M280 P0 S160 – alarm deactivation (LED continuous flashing)


We can send commands in the Cura software, but unfortunately this program does not give us any feedback, so I personally recommend the Repetier-Host software. Connecting the printer using it is extremely simple and the feedback received from the printer will be displayed on the console, which can make it very easy to find the errors.




In order to check how well our new sensor is doing and if everything is properly configured, we can use these two prints.


If the prints are evenly printed on every part of the heatbed, our job is done.


A lot of information, especially about hardware connection, has been taken based on this site which I also encourage you to visit.


Below, I am sharing all the files that were used in this project:


Hex file

BL Touch Mount model

Bed Level Test model 1

Bed Level Test model 2

Leave a Comment

WordPress Video Lightbox Plugin