How To Connect a Thermal Printer to an Arduino Board
2022-09-26 | By Maker.io Staff
A recent article investigated how thermal printers operate and their use cases, benefits, and drawbacks. It concluded that receipt printers, as they are often also called, are a great and inexpensive way of adding a printer to a wide array of devices across many applications. Therefore, this article covers the basics of connecting a thermal printer to a standard Arduino board (like the Arduino Uno). It also discusses how to send data to the printer using a simple Arduino library.
Powering the Thermal Printer
The printer used throughout this tutorial requires an external power supply that operates at 5V and can handle peak currents of around 1.5 Amperes. You can, for example, use this external power supply that ensures the printer gets enough power even when initially heating up. Start by connecting the wires supplied with the printer. However, pay attention to the labels on the back of the printer so that you don’t get the wires mixed up:
Pay attention to the labels when connecting the wires to prevent damaging the printer.
Most thermal printers have a self-test feature you can activate by pushing or holding down a button on the device while it powers up. This button also lets you feed the paper one line at a time once the printer is powered up. For now, hold down the button while connecting the printer to the power supply to print a self-test report:
The printer spits out a self-test report when you hold down the button while powering up the device.
You can try flipping the paper should you notice that the printer unwinds the paper roll without producing any visible output. This problem is likely because only one side of the thermal printer paper is coated with a heat-sensitive coating.
Interfacing the Thermal Printer with an Arduino
Next, it’s time to connect the printer to an Arduino board to produce more meaningful output than just a simple self-test page. This printer uses a standard serial connection over a pair of TX/RX pins. Therefore, operating the printer only occupies two digital pins and one GND connection on the Arduino board:
Connect the thermal printer to the Arduino, as shown in the schematic diagram above.
Next, use the Arduino IDE’s library manager to install the Adafruit Thermal Printer library:
Install the Adafruit Thermal Printer library using the Arduino IDE’s built-in library manager.
Sending Data to the Printer
Once installed, you can use the Adafruit thermal printer library for sending text, barcodes, QR codes, and simple bitmap images, such as the logos commonly found on store receipts, to the printer. The following sketch prints a few lines of text using the thermal printer hooked up to the Arduino:
#include "Adafruit_Thermal.h" #include "SoftwareSerial.h" #define TX_PIN 6 // Arduino TX pin -- RX on printer #define RX_PIN 5 // Arduino RX pin -- TX on printer SoftwareSerial printer_connection(RX_PIN, TX_PIN); Adafruit_Thermal printer(&printer_connection); void setup() { printer_connection.begin(9600); printer.begin(); printer.setFont('B'); printer.println("Hello, World!"); printer.setFont('A'); printer.println("Hello, World!"); printer.inverseOn(); printer.println(F("Good Bye, World!")); printer.inverseOff(); printer.doubleHeightOn(); printer.println(F("Large Text")); printer.doubleHeightOff(); } void loop() { }
The first few lines import the necessary libraries, and the two define statements specify which digital IO ports you utilized when connecting the development board to the printer. Then, the following two lines open a new software serial connection (you could also use hardware serial) and create a thermal printer object.
The setup function sends a few strings to the device for printing onto the thermal paper. It first sends a hello world string using two different font styles, then an inverse text where the font’s background is black while the letters themself remain white, and the final few lines print a short message using a double-height font. You can also choose from more settings such as bold font, italic, and different alignment options, for example, centered text. You can find these options in the examples that come with the printer library.
Finally, you can also print barcodes using a few additional lines of code:
printer.printBarcode("ADAFRUT", CODE39); printer.setBarcodeHeight(100); printer.printBarcode("123456789123", UPC_A);
Summary
Receipt printers are a fantastic way of adding an inexpensive, simple-to-use, small printer to any project. Most printers require relatively high currents of over one Ampere during operation, so an external power supply is often needed. However, other than that, interfacing a receipt printer with an MCU or development board is as easy as connecting two serial communication lines.
The Adafruit thermal printers have a compatible Arduino library that makes printing characters, bitmaps, barcodes, and QR codes a breeze. You can accomplish all these operations by adding only a few lines of code to your sketch. In addition, the library supports multiple fonts, formats, and styles.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum