3D printer and CNC basics

Stepper homing, a heated bed PID loop with thermal runaway protection, a mini G-code plotter and a runout sensor.

Mokxi has four templates that show what a printer or CNC firmware does underneath, and the 3D printer and CNC basics course walks through them. They run on an Arduino Uno, on the pins a GRBL CNC shield uses: X STEP 2, Y STEP 3, X DIR 5, Y DIR 6, EN 8, X limit 9 and Y limit 10.

This is not Marlin or GRBL

Each template is a small sketch that does one thing those firmwares do, the same way, and says where the real thing goes further. Where a command borrows a firmware's name, such as M140 or G28, the help says which firmware uses it that way.

What is simulated, and what is not

The A4988 drivers, the NEMA 17 motors, the microswitches, the thermistor, the MOSFET and the optical sensor are all simulated parts. Two things are not:

  • There is no carriage. An endstop closes when you click its lever, so when the Serial Monitor says an axis is seeking, you play the carriage.
  • Heat does not flow from the heater into the thermistor. The bed sketch carries a small model of the bed, marked MODEL in the code, and the thermistor's slider is the room temperature. The model runs ten times faster than a real bed.

Stepper homing

One A4988 at 1/16 step and a 20 tooth GT2 pulley: 80 steps a millimeter. Type H to home: the axis seeks the endstop, backs off 5 mm, and touches again slowly (Marlin calls the second touch the bump). Then type a position in millimeters. Moves past 0 to 200 mm are refused, like a printer's software endstops.

Heated bed PID

A 100k NTC thermistor (beta 3950) under a 4.7k pull-up on A0, and a logic-level MOSFET on pin 9 switching a 12 V lamp that stands in for the heater. Type M140 S60 to heat, M140 S0 to stop and M105 for a report. The PID output is clamped against windup, and three protections run all the time:

  • a minimum and maximum temperature, which catch a broken or shorted sensor
  • a heat-up watch: after a new target the reading must rise in time
  • thermal runaway: once at temperature, a sustained drop below the target halts the heater until a reset

Slide the switch to drop the thermistor off the bed and watch the protection trip. The idea follows Marlin's thermal protection settings; the numbers are scaled to the fast model and are not any printer's.

Safety. A bed or hotend heater can start a fire. Never disable thermal runaway protection. Mains-powered beds need proper wiring, a thermal fuse and an earth, which belong on the bench with the manufacturer's instructions.

Mini G-code plotter

Two axes, two endstops, and a parser for G28, G0, G1, G90, G91 and M114, answering ok after every line as a firmware does for its sender. G1 moves both motors together with Bresenham's line algorithm. G28 homes, as in Marlin; GRBL homes with $H and uses G28 for a stored position. The plotter moves at a steady speed, where a real firmware plans acceleration.

Filament runout sensor

An optical slot sensor on pin 4 (filament blocks the beam) and an A4988 extruder at 93 steps a millimeter. A runout must last 300 ms to count, the extruder keeps going for the 10 mm of filament still in the path, and the print pauses until filament is back and you press resume. Marlin's matching setting is FILAMENT_RUNOUT_DISTANCE_MM, and it usually runs M600 at the pause.

Where to go next

Open the templates from /templates, take the course from /learn, or read the guide at /learn/3d-printer-firmware-basics.