Arduino library · Displays

Adafruit Thermal: a mini receipt printer on Arduino

Yes. You can use the Adafruit Thermal Printer Library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Thermal receipt printer). The example below, "Styled text and a barcode with Adafruit Thermal", runs on the Arduino Uno R3.

Built in on Mokxi: #include <Adafruit_Thermal.h> and it builds, with nothing to install. Free, and no account needed.

What Adafruit Thermal Printer Library does

The small TTL thermal printers sold by Adafruit and many others print 58 mm receipts by heating dots on special paper, with no ink. They take text and commands over a serial line at 19200 baud (some at 9600), and the Adafruit_Thermal library wraps the commands so a sketch can print styled text, barcodes and small bitmaps without learning the ESC/POS codes.

The library sits on any Stream, so on an Uno it rides a SoftwareSerial port and on a Mega or ESP32 a hardware one. Styles are switches you turn on and off around the text: boldOn() and boldOff(), underlineOn(), inverseOn(), doubleHeightOn(), and setSize() with S, M or L. justify() takes L, C or R. Everything printed after a style call uses it until you switch it back or call setDefault().

The print head needs real current, a couple of amps at 5 to 9 V while it prints, so it gets its own supply sharing ground with the board, as in the example: the printer’s RX on pin 5, its TX on pin 4, and a 9 V source on its head supply. In Mokxi the printout appears at once, so the timing settings change nothing, and a bitmap shows as a placeholder box of its size rather than its pixels.

Library Manager nameAdafruit Thermal Printer Library
AuthorAdafruit
Include#include <Adafruit_Thermal.h>
BoardsArduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V, ESP8266 NodeMCU, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP32-S3-CAM
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesThermal receipt printer

Install it

It builds on Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, Arduino Pro Mini 3.3 V, ESP8266 NodeMCU, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP32-S3-CAM.

In Mokxi it is built in: #include <Adafruit_Thermal.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "Adafruit Thermal Printer Library" by Adafruit from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add Adafruit Thermal Printer Library to lib_deps in PlatformIO. Mokxi follows the calls of release 1.4.1, so a sketch written here builds there too.

What to know in the simulator: The printer prints at once, so the timing calls change nothing; a bitmap shows as a placeholder of its size.

The calls you will use

In the order a sketch meets them.

Adafruit_Thermal printer(&mySerial)

The printer on a serial port you opened first, mySerial.begin(19200) for most models.

printer.begin()

Wakes the printer, resets its settings and sets the heat time. Call it once in setup(), after the port is open.

printer.boldOn()

Prints what follows in bold until boldOff(). underlineOn(), inverseOn() and doubleHeightOn() work the same way.

printer.setSize('L')

Character size: S for small (the default), M for double height, L for double height and width.

printer.justify('C')

Line alignment: L, C or R. It applies to every line until changed.

printer.printBarcode("ADAFRUT", CODE39)

Prints a barcode of the given type: UPC_A, EAN13, CODE39, CODE128 and others. Each type limits which characters and lengths it takes.

printer.feed(2)

Feeds blank lines so the last printed line clears the tear bar.

printer.hasPaper()

Asks the printer’s paper sensor and returns true when paper is loaded. Needs the printer’s TX wired back to the board.

if (!printer.hasPaper()) {
  Serial.println("Out of paper");
}

Example: Styled text and a barcode with Adafruit Thermal

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <Adafruit_Thermal.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(4, 5); // RX from the printer's TX, TX to its RX
Adafruit_Thermal printer(&mySerial);

void setup() {
  Serial.begin(9600);
  mySerial.begin(19200);
  printer.begin();

  printer.println("Hello, thermal world!");
  printer.boldOn();
  printer.println("Bold text");
  printer.boldOff();
  printer.justify('C');
  printer.setSize('L');
  printer.println("Large");
  printer.setSize('S');
  printer.justify('L');
  printer.printBarcode("ADAFRUT", CODE39);
  printer.feed(2);

  Serial.println(printer.hasPaper() ? "Paper loaded" : "Out of paper");
}

void loop() {
}

Wiring

  • Arduino Uno R3 5 to Thermal receipt printer RX
  • Arduino Uno R3 4 to Thermal receipt printer TX
  • Power VCC to Thermal receipt printer VH
  • Arduino Uno R3 GND to Thermal receipt printer GND

Open it in the editor, wired and ready to run

Common errors and how to fix them

The printer prints garbage characters

The baud rate does not match. Hold the feed button while powering the printer to print its test page, which states the rate, and use that number in mySerial.begin().

Print is faint, or the board resets in the middle of a receipt

The head is not getting enough current. Power the printer from its own 5 to 9 V supply rated for 2 A, never from the Arduino’s 5 V pin, and join the grounds.

hasPaper() always says there is no paper

The printer’s TX line is not connected to the board’s receive pin, so the answer never arrives. Wire it to the first SoftwareSerial pin, or skip the call if you only send.

Questions and answers

Can it print images?

On a real printer, yes: printBitmap() takes a width, a height and a 1-bit array, usually stored in PROGMEM. In Mokxi the bitmap prints as a placeholder box of that size.

Projects that use Adafruit Thermal Printer Library

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

Try Adafruit Thermal Printer Library without installing anything

Open the example, change the sketch, and keep your version in a free account.