Thursday, April 28, 2011

A More Advanced Way to Level the Bed

Ever since I first built my Mendel I've felt there has to be a more advanced way to level the bed than rolling a bit of dowel under the extruder nozzle. The Mendel is a very advanced CNC machine, but relies on a bit of wood to level the bed. Leveling the bed is important because even a deviation of 0.1mm can make the difference between the first layer sticking or not. A spirit-level is not accurate enough and measures the plane between the bed and the bench, not the plane between the extruder and the bed.

I wanted to find a way to use the Mendel itself to calibrate the level between the bed and the extruder. After thinking through various different approaches over the last few months, I settled on the following solution.

It would be based on a micro-switch that could be suspended beneath the extruder. The micro-switch would be connected in place of the normal Z-axis end-stop. The micro-switch would then be used to 'probe' the bed at various spots, each time the end-stop registered it should be possible to determine how many steps ahead or behind of where the end-stop was expected to be the extruder head had moved. The GCode M117 is already specified to provide this kind of information, mainly for determining if an axis is skipping steps, but it works here too.

The Micro-Switch Holder

Micro-Switch Bracket
I needed a way to hold the micro-switch that wouldn't require the extruder to be removed. I also wanted an approach that would allow the micro-switch to be added and removed easily. The easiest way to achieve this was to use magnets to attach the holder to the nuts on the bottom of the X-carriage. I designed a bracket that could be easily printed. The bracket was U-shaped, so that the magnets could contact the nuts but it would clear the extruder head. There was a foot on the bottom of the bracket to hold the micro-switch, which was offset so the switch lever was under the extruder nozzle.

Developing the Test Code

I had to create a simple application that controlled the Mendel to perform the calibration and collate and graph the results. I wrote it in my favorite language, Python. The program essentially takes a reference reading on the bed, then probes the bed at a number of points across its surface, recording the number of steps above or below the expected stop point. Since the Z-axis  is screw-drive, it has incredibly high accuracy. My Mendel requires about 2700 steps per mm in the Z-axis. There does appear to be some variation in the micro-switch activation. Making repeated measurements in the same spot shows that it doesn't always return to zero, but the variation is relatively small, +/- 12 steps. Compared to a layer of blue-tape at ~230 steps, I'll live with the variance.

The firmware that is running on my Gen6 Mendel does not actually support the M117 command, so I had to port the appropriate code across from the current firmware into my version. The changes were relatively straightforward, but due to some interim changes in the firmware builds, not just a cut-and-paste job.

The First Test Results

The first set of results for my bed, which I considered to be relatively level, where quite surprising to say the least. The plot showed that the bed was bowed, clearly being pulled down in the corners by the screws and forcing the middle up. The total variation across the bed was 0.18mm, which explains why I was having some first layer adhesion problems on large prints. I could clearly do better.

Bowed Bed
Tuning the Bed

My first task was to remove the bed and look at it more closely. Holding a straight edge across it clearly showed that there was some initial level-ness issues. The aluminium bed is too thick to bend by hand, and the deviations were rather small (0.1mm), so I decided to grind the bed flat.

Using some 220-grit sandpaper taped to a sheet of 1/2" plywood, I ground down the bed until the visible gaps under the straight-edge were gone. I then reattached the bed very loosely and repeated the auto-calibration. It clearly wasn't level.
Unfortunately, not level straight out of the gate.

Using a spirit level was a bad idea, it measured the level-ness of the bench!

The bubble was between the lines!
Using the old-dowel method was pretty good, but still not perfect.
The trusty old dowel wasn't actually that bad.

Tweaking the bed screws and repeating the calibration it was clear that the force of the bed springs was distorting the bed. To get one side level, it skewed the opposite corner.
Adjusting one corner pulled the others out of whack.

To try to correct this I decided to switch out the printed springs with wire springs. The same problem occurred. The upward force of the springs caused problems when leveling the bed.

No springs, just screw pillars
In the end I opted to do away with the springs completely and use bolts fixed to the bed as pillars, resting on nuts on the Y-carriage for easy adjustment. It was much easier to adjust the bed to be completely level with this approach. I think I'll change the regular nuts out to locking nuts though, it will prevent them moving through vibration. 

The downside with this approach is there is no give in the bed if the extruder plows into it. To overcome this, I think I'm going to put some small compression springs on top of the extruder connection screws. This should allow the extruder to move up slightly if anything goes wrong.

Level At Last

After six-days of tweaks and experiments, the bed is finally level and I'm happy. It is now level to within 0.08mm. I'm ignoring the last set of measurements at the back of the bed. They were made at the very back edge of the bed where I can't print anyway and no amount of screw adjustment can fix it.

Looks bad, but look at the scale!
Next Steps

Now I have a calibration method that allows me to either perform very detailed mapping of the bed flatness without removing the extruder or breaking out the dowel. If I do have to make adjustments, I won't spend ages adjusting bolts to find that they have changed something on the other side of the bed.

I also have the ability to perform very quick verifications of bed flatness. A four point test at the four screw locations takes under 2-minutes and is very good indicator of how flat the bed is now.  I can also save the resulting graphs for future reference to see if things have changed over time. 

Also, because I now know the deviation of the bed in millimeters, it takes the guess work out of how much to adjust the bed by. Each revolution of a M4 screw moves the nut by 0.7mm. Since the nuts are 6-sided, by moving the nut by one face I can get 0.116mm of adjustment very easily.

This work has some other potential uses that I am going to investigate. It could be possible to take the surface map of the bed and use it to dynamically adjust the first layer of GCode to overcome any minor surface variation by changing the Z-height while printing. I might try developing a Skeinforge plug-in that does this. Another use might be to use it to make point-clouds of objects placed on the bed  - allowing me to create crude 3-D scans. I think that I'll need something more pointy than a micro-switch lever for the probing, but the calibration utility I have written will definitely work.

I'm just tidying up the calibration application and will post it shortly, along with the bracket STL file, if anyone is interested. I should also document the changes I made to the Gen6 FW to add the M117 command, because I'm sure I'll forget before the next release comes out!

8-Jun-11 - Update:

The calibration application is now available, along with the STL for the bracket here.

This series of scripts requires the Python libraries: PySerial, Matplotlib and NumPy

Note: This script banks on the Mendel firmware supporting the M117 GCode. Without it, it won't work! Comms library for communicating with a Mendel. Main calibration routines. Bed plotting routines. A GUI to make calibration easier.

To use the GUI:

  • Use the Printer Menu tab to configure the various settings to match your printer.
  • Calibrate Button, will perform a series of measurements across the whole bed at the specified X & Y step size.
  • Quick Cal Button, will perform a quick test at four specified points across the bed.
  • Variance Button, will perform a repeated measurement at a specified point to see what the variance is for the microswitch.


  1. Nice work. You will get better resolution if you remove the lever from the microswitch and use the plunger directly.

    I think the saddle shape is due to the XY bars drooping in the middle.

  2. Thanks nophead. I went back and forth on lever or no lever. I decided the lever would make the switching force less and distort the bed less when it was on the springs. I'll try it without and test the variance of the measurements, hopefully it will decrease.

  3. Any updates on the software or .stl file? I'm interested in levelling my bed - although my printer won't be functional for another 2-3 days.

  4. The files are now all published. If you find any 'featurettes' with the code, let me know.

  5. Oh! What a relief to find this ... I thought I was going to have to develop this all by myself, and now you've done it for me.

    Just a thought ... my brain was going down the route of no microswitch, just a croc clip on the print bed, and a crock clip on the extruder, and looking for "circuit make" when the extruder touches the bed. Of course, that assumes no tape of any sort on the bed.


  6. That would work too, but depending on your extruder type, you might either mar the bed, or deform the extrusion nozzle by repeatedly crashing it into the bed (even in a controlled fashion).