Source: https://mokxi.com/learn/3d-printer-firmware-basics
Updated: 2026-10-05

Learn

# 3D printer and CNC firmware basics

Advanced

Written by the Mokxi team, updated October 5, 2026

Start building free Open the editor

Or see every lesson.

- 261 parts on the bench
- 31 boards running now
- 1.00x real time, on every board

Press Run, then type M140 S60

Type M140 S60 in the Serial Monitor, let it settle, then slide the switch to drop the thermistor.

Printer and CNC firmware can feel like a wall of settings. Underneath, a few ideas do most of the work: a stepper that finds zero against an endstop, a heater held at temperature by a control loop with safety checks around it, a parser that turns G-code lines into coordinated motor steps, and a sensor that notices when the filament runs out.

The circuit above is a heated bed controller on an Arduino Uno in Mokxi: a 100k thermistor under a 4.7k pull-up, a MOSFET switching a 12 V lamp that stands in for the heater, and a PID loop. Press Run, type M140 S60, and watch the lamp glow bright and then settle. Then slide the switch to drop the thermistor off the bed, and watch the thermal runaway protection switch everything off.

This is not Marlin or GRBL. Each template is a small sketch that does one thing they do, the same way, and says where the real firmware goes further. Mokxi also does not move heat from a heater into a thermistor, so the bed sketch carries a small model of the bed, marked in the code, and the thermistor’s slider is the room.

Want the step-by-step version? The lesson "Start the 3D printer and CNC course" walks through this with checkpoints.
Open the lesson

## Thermal runaway protection

The most important code in a printer is the code that turns the heater off. If a thermistor works loose, it reads the room instead of the heater, the control loop sees a cold bed and pushes full power, and the heater keeps getting hotter. Thermal runaway protection is the firmware noticing that the reading is not following and halting until a reset.

Marlin describes two checks in its configuration: a watch period after a new target, during which the temperature must rise by a few degrees, and a protection period and hysteresis once at temperature, after which a sustained drop below the target halts the machine. The template runs both, plus a minimum and maximum reading that catch a broken or shorted sensor, with numbers scaled to its fast model. Never disable this protection on a real printer, and leave mains-powered beds, thermal fuses and earthing to the bench and the manufacturer’s instructions.

The runaway check, from the bed sketch

## PID in plain words

A heater controller has one number to choose, how hard to drive the heater, and it chooses it from three questions. How far am I from the target right now? That is the proportional term, and on its own it settles a little short, because as the error shrinks so does the push. How long have I been short, added up? That is the integral, and it slowly supplies the power that holding temperature takes. How fast is the reading moving? That is the derivative, and it eases off before the heater overshoots.

Two refinements in the template matter on real hardware. The integral is clamped, so a bed that is still far from its target cannot store up a huge correction and overshoot badly when it arrives, which is called windup. And the derivative is taken on the measurement rather than the error, so a new target does not make the output jump. Printer firmware tunes the three gains with an autotune routine that heats and cools a few times and measures the response; the template’s gains were chosen for its model and are not meant for any real bed.

## Drivers, microsteps and current

The A4988 in the motion templates needs only two signals: a pulse on STEP for every step and a level on DIR for the direction. Its three mode pins choose the microstep, from full step to a sixteenth, and a small trimmer sets the current limit, which on a real board must be set to suit the motor before it runs. Pololu’s page for the carrier gives the formula and warns against connecting or disconnecting a motor while the driver is powered, because that can destroy the driver.

## Homing against an endstop

A stepper knows how many steps it has taken, not where it is, so at power-up a printer homes: it moves toward a switch until it closes, backs off, and touches again slowly, because the slow touch is the repeatable one. The template does this with an A4988 at 1/16 step and 80 steps a millimeter, the figure a 20 tooth GT2 pulley gives a 200 step motor, then moves to typed positions and refuses any outside the axis, like software endstops. Mokxi has no carriage, so you click the endstop when the Serial Monitor says the axis is seeking.

## A mini G-code interpreter

The plotter template reads G28, G0, G1, G90, G91 and M114 from the Serial Monitor and answers ok after every line, which is how a sender knows to send the next one. G1 moves both motors together with Bresenham’s line algorithm so a diagonal is straight. G28 homes here, as it does in Marlin; GRBL homes with $H and uses G28 to go to a stored position. A real firmware also plans acceleration across many lines, which this one leaves out on purpose.

## Filament runout

A runout sensor sits before the extruder, so when it trips there is still filament in the path. The template debounces the reading, keeps printing for that distance, then pauses and beeps until filament is back and you press resume. Marlin’s matching setting is FILAMENT_RUNOUT_DISTANCE_MM, and it usually runs a filament change at the pause. The 3D printer and CNC course walks through all four templates with a check at every step.

## Questions

Is this Marlin or GRBL?

No. Each template is a short Arduino sketch that does one thing those firmwares do and says where they go further. Commands that borrow a firmware’s name say which one.

What is thermal runaway protection?

Firmware that switches the heater off and halts when the temperature stops following the heater, usually because the thermistor has come loose. Never disable it.

Why does the bed sketch use a model?

Mokxi simulates the thermistor, divider, MOSFET and PWM, but not heat flowing from a heater into a thermistor, so the sketch models the bed and marks that code clearly.

Does G28 home a GRBL machine?

No. GRBL homes with $H and uses G28 for a stored position. Marlin’s G28 homes, and the template follows Marlin’s meaning.

Related

## Keep going

Arduino A4988 and NEMA 17 Stepper: Wiring and Code Arduino Stepper Motor: 28BYJ-48 and ULN2003 MOSFET vs BJT: Which Transistor to Switch With Arduino PWM Fade: analogWrite, Pins and the Curve UART Serial Explained: Baud, Frames and the Wire The heated bed PID project: code, parts and wiring Advanced Stepper homing Intermediate The mini G-code plotter Advanced The filament runout sensor Intermediate Help: what the printer templates model The project page, with the full sketch Advanced

Sources

## Where the facts on this page come from

- Marlin: G28, auto home
- Marlin: M412, filament runout
- GRBL v1.1 commands, including $H homing
- Pololu A4988 carrier: microstep table and current limit

## Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.

Start building free Open the editor
