Marlin a beginners guide

Spread the love
Marlin a beginners guide
5 (100%) 1 vote

So many times it seems the simplest thing is the most complex and annoying, like a dirt road that you just can’t get to the end of but there is hope a light at the end of the tunnel. Marlin is one of the many flavors of code that is used to run a 3d printer. It is an operating system of sorts in a basic machine level code. Hence the reason so many people get lost in it. Marlin can be a bit daunting at first and everyone thinks well if I change it how do I know what is the right thing to change well I am going to walk you through the basics of what is necessary to more or less get it working.
To begin with there is one simple basic rule read, read the entire thing no it won’t make any sense to you that’s ok but read the whole thing a few times over get a feel for the word structure and take not of how its laid out it is that way for a reason, the programmer laid it out so that 1 it was in clusters or groups that like over each other 2 you could jump from section to section to define the necessary parameters as needed and 3 it makes it simple to solve a problem because all the code for that one part is right there grouped together.
Various types of firmware
There are several flavors of firmware some are brand specific others are generic and can be used with any type of 3d printer there are also a few that are only for CNC machines which are simmiler but very different to 3d printers.
1 Sprinter
2 Teacup
3 sjfw
4 Marlin
5 Sailfish
6 Grbl
7 Repetier-Firmware
8 Aprinter
9 RepRap Firmware
10 ImpPro3D
11 Smoothie
12 Redeem
13 Further reading go here =>
These are the most common and regularly used flavors out there but were going to stick with marlin as our base of reference since that is what most 3d printers use.
Firmware just what is it?
Firmware is the code which resides on the printer’s motherboard. The firmware is the link between software and hardware, it deciphers commands from the G code and controls the motion of the 3d printer accordingly. The firmware configuration is unique to your 3d printer. It knows the properties of the 3D printer, like the dimensions or heating settings. It plays a major role in the quality of the print and how the machine works. So how do you work with in the firmware to make it work for you?
Well first you are going to need an Arduino board and a ramps board as they are the most common and the most cost effective to buy. Then you will have to pop on over the Arduino site and grab a copy of their software it free to download and use here is the link for the site download and install this is where we will be able to see the firmware and make any changes that we want.
Programming rule #1: comments

Sometimes we need help and comments to understand the programmer, but we don’t want the computer to see the comments so we use separators. In Arduino IDE the separator between computer language and comments is a double slash //, it works for every line. The compiler will not try to read after this separator. This is the only programming rule that we need to understand to configure the firmware properly. Sometimes we will activate some options by removing the comments and other times we will comment-out options to deactivate them
Ok so let’s open the file Configuration.h in the Marlin folder.
Got it open? Ok great let’s have a look here and get a basic rundown of what you are looking at
Baud rate this is how fast the computer can talk to the 3d printer so well Set the communication speed in baud to: #define BAUDRATE 250000
This is a base point that most 3d printers can communicate on so like a bit faster others a bit slower but you will know if you need to drop it down or raise it when you connect it and you run a few tests.
There are two speeds commonly used by 3D printing software, the 250000 and 115200 baud rate. If one doesn’t work with your hardware or software, simply try the other one and see if that works.


Just above the line: #define MOTHERBOARD you will see a list of different motherboards. You have a list of boards find yours and use the appropriate number. For example a RAMPS 1.3 / 1.4 (Power outputs: Extruder, Fan, Bed) will be configured:
#define MOTHERBOARD 33
It doesn’t matter if you don’t use a fan or heat bed for now.
Defines the number of extruders
If you use more than one extruders you can write it there.
#define EXTRUDERS 1
Thermal Settings
Temperature sensor choice
Among the list (in configuration.h), choose the thermistor number that you use for each of the hot ends and for the heat bed. Caution should be taken here If you pick the wrong number, you will get an inaccurate temperature reading.
#define TEMP_SENSOR_0 1
#define TEMP_SENSOR_1 0
#define TEMP_SENSOR_2 0
TEMP_SENSOR_0 is used by the first hot end and TEMP_SENSOR_BED is connected to the heat bed. If you don’t use a sensor, use 0 to disable it.
Minimum temperature
Use a minimum temperature different than 0 to check if the thermistor is working. This security will prevent the controller from heating the hot end at max power indefinitely. This is important. It should already be set by default
#define HEATER_0_MINTEMP 5
#define HEATER_1_MINTEMP 5
#define HEATER_2_MINTEMP 5
#define BED_MINTEMP 5
units are in °Celcius

Maximum temp
Depending on your hardware material your hot end will have a specific maximum temperature resistance. This security will prevent the temperature from going over it. For example a J-Head extruder may use a PTFE tube to guide the filament in the hot zone which can be damaged over 240°C. Notice the controller can overshoot the target temperature by a couple of °C, it is better to keep a security margin here.
#define HEATER_0_MAXTEMP 230
#define HEATER_1_MAXTEMP 230
#define HEATER_2_MAXTEMP 230
#define BED_MAXTEMP 120
PID temperature controller.
This is an advanced option which needs to be tuned later. For now you can use the default options.
Prevent dangerous extrusion:
These settings are set by default.
For a security reason you may want to prevent the extrusion if the temperature of the hot end is under the melting point of the material. You don’t want to push the extruder over solid material and risk breaking it.
If there is a mistake in the code like a dot position for example, you would want to prevent a very long extrusion command.
Here you can set the values for the minimum temperature:
Mechanical settings
End Stops
End stops are switches that trigger before an axis reaches its limit, It will move each axis in a specific direction until it reaches an end stop, this is the home for the printer.

Pull-ups resistances
It is a good practice to use a pull-up or pull-down circuit for a basic switch. To keep it simple the pull-ups resistance are needed if you directly connect a mechanical end switch between the signal and ground pins.
Fortunately there is already a pull-up resistor integrated in Arduino that can be activated via the software.
Web links for more specific info on that can be found here:

#define ENDSTOPPULLUPS // Comment this out (using // at the start of the line) to disable the end stop pull-up resistors
The Marlin firmware allows one to configure each limit switch individually. You can use multiple end stop types on the same printer.
For a total of six that’s 2 for the X axis 2 for the Y axis and 2 for the Z axis, one each for the MAX and 1 each for the MIN (front back top bottom left and right of the printer)

Invert end switch logic
Some limit switches are normally closed (NC) and turn off when triggered and some are normally open (NO) and turn on when triggered. Ideally the limit switches would be normally on and turn current off when triggered. The printer would interpret the same signal if the end stop is hit as if the end stop is broken or disconnected.
NO = true
NC = false
const bool X_MIN_ENDSTOP_INVERTING = false;
const bool Y_MIN_ENDSTOP_INVERTING = false;
const bool Z_MIN_ENDSTOP_INVERTING = false;
const bool X_MAX_ENDSTOP_INVERTING = false;
const bool Y_MAX_ENDSTOP_INVERTING = false;
const bool Z_MAX_ENDSTOP_INVERTING = false;
Use three or six end stops?
Under normal circumstance the printer only needs 3 the zmin xmin and ymin this is how it finds the home position but on some printers they “get lost” and you have to set up a xmax ymax and zmax so that the printer can know where the edges of the build plate are
Uncomment the following lines to disable all max or all min end stops.
Invert stepper motor direction
There is only way to know if the stepper motor direction is correct you have to try it. I would wait until the configuration is completed, compiled and sent to the controller before you try. You can come back here after know if there is a problem or not.

1: Position the printer axis at the center of each axis.
2: Keep your finger close to the stop button.
3: Send a command to move the X axis a little amount like +1 or +10 mm
4: If the printer moves in the other direction, you will have to reverse the axis direction.
5: Repeat for each X Y Z axis
To inspect the extruder you would need to heat the hot end to the extrusion temperature load a sample of filament and Send extrusion command of 10 mm and check the motor rotation direction.
You can invert the stepper motor direction if it was wired the wrong way. It doesn’t make a difference if you invert the stepper motor wiring or if you invert it in the code. *Remember to power off the printer before unplugging or replugging the stepper motors.
#define INVERT_X_DIR false
#define INVERT_Y_DIR true
#define INVERT_Z_DIR true
#define INVERT_E0_DIR true
#define INVERT_E1_DIR true
#define INVERT_E2_DIR false
Set home direction

Here we set the home direction when the home button is pressed. It tells the printer which direction it should go to reach the end stop and get its reference position.
#define X_HOME_DIR -1
#define Y_HOME_DIR -1
#define Z_HOME_DIR 1
The Z axis is the one which will get the printing surface very close to the nozzle. It needs to be very precise and very quick to trigger otherwise there will be a collision between the nozzle and the surface and this can damage the heat bed and the hot end

If you use optical end stops for instance you can position the end stops at the center of the moving area. You can tell the printer to move over the end stop signal. I never use it. If you placed end stops at the end of each axis, then keep these options set to true. (I highly recommend not using optical stop ends not because they don’t work but they are hard to calibrate and keep calibrated with the printer)
Don’t go over the minimum limit:
#define min_software_endstops true
Don’t go beyond the maximum limit
#define max_software_endstops true

Printer area
Here we can define the print size in the firmware by telling it what the limits are of the travel zones. The travel distance is not calibrated yet and the practical distance will be different than the calculated distance. So for now it is better to reduce the travel distance to less than the actual size once were sure it works we can come back and change it then
#define X_MAX_POS 190
#define X_MIN_POS 0
#define Y_MAX_POS 190
#define Y_MIN_POS 0
#define Z_MAX_POS 190
#define Z_MIN_POS 0
Movement settings

Define the number of axis
It is the total number of axis (3) plus the number of extruders (1).
#define NUM_AXIS 4
Homing feed rate
This is the moving speed of the axis when homing in [mm/min]. Oftentimes in Marlin, speed or acceleration are expressed in [mm/s] or [mm/s2] but the feed is expressed in [mm/min].
#define HOMING_FEEDRATE {5060, 5060, 4*60}
Axis steps per unit
The stepper motor receives step by step moving command from the controller. The controller needs to know the steps/mm ratio to send the appropriate steps to reach the required distance.
Just how many steps are needed to move an axis by 1 mm?
Belts and pulley (usually x y axis):
steps_per_mm = (motor_steps_per_rev * driver_microstep) /
(belt_pitch * pulley_number_of_teeth)
lead screw (z axis)
steps_per_mm = (motor_steps_per_rev * driver_microstep) / thread_pitch
Direct drive extruder:
e_steps_per_mm = (motor_steps_per_rev * driver_microstep) /
(hob_effective_diameter * pi)

Extruder with gear reduction:
e_steps_per_mm = (motor_steps_per_rev * driver_microstep)
(big_gear_teeth / small_gear_teeth) / (hob_effective_diameter * pi)
#define DEFAULT_AXIS_STEPS_PER_UNIT {80,80,200.08/3,760}

Send the firmware to the 3D Printer via Arduino

In Arduino IDE, save the modification done to Configuration.h
Verify the code
If everything is fine, Arduino should say “Done compiling” otherwise you will get one or more error messages. If this happens, there is usually a mention about the line of the error. Check your code for a comments error //, dots or other special characters that may have been mistyped.
Once it is compiled properly you can connect the Arduino board to the computer with the USB cable.
Select the proper port, Tools menu / Serial Port /
Upload the compiled code to the Arduino

Now your printer is ready to move. It’s alive!!! Yes you did it you have set up your own custom firmware for your printer!

But wait there is still more……. Test the stepper moters make sure they move like there supposed to
Just one more thing. You need to try the stepper motor direction as mentioned above. You will need to use a host software connected to the 3D printer. The instructions to use this software will be explained in another post, for now I will stick to the basic guidelines.

You can find more info on Pronterface here:
You can also use Repetier –host which has an awesome G code viewer:
To test the motor direction, connect the printer to the computer with the USB cable.
Position the printer axis manually at the center of each axis.
Power up the printer
Open a host software like Pronterface or Repetier-host
Click to connect the host to the printer
Keep your finger close to the stop button of the motherboard (just in case).
Send a command to move X axis a little amount like +1 or +10 mm

If the printer moves in the other direction, you will have to reverse the axis direction.
Repeat for each X Y Z axis

To inspect the extruder you will need to heat the hot end to the extrusion temperature, load your filament and make a mark on it with a marker so you can see it. Then send the extrusion command of 10 mm and check the motor rotation direction.

as a final note
You can invert the stepper motor direction if it was wired the wrong way, it doesn’t make a difference if you invert the stepper motor wiring or if you invert it in the code. *Remember to power off the printer before unplugging or replugging the stepper motors.
#define INVERT_X_DIR false
#define INVERT_Y_DIR true
#define INVERT_Z_DIR true
#define INVERT_E0_DIR true
#define INVERT_E1_DIR true
#define INVERT_E2_DIR false


Comments 1

Leave a Reply