Rohan Barnwal
Published © LGPL

Print "Hello World" Using ESP32-S3-BOX-3 and Thermal Printer

Learn how to interface a QR204 thermal printer with the ESP32-S3-BOX-3 and print text with a single button press.

BeginnerFull instructions provided1 hour171
Print "Hello World" Using ESP32-S3-BOX-3 and Thermal Printer

Things used in this project

Hardware components

QR204 Thermal Printer
×1
Espressif ESP32 S3 Box3
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Code

Arduino
#include <Arduino.h>
#include <HardwareSerial.h>

#define LGFX_AUTODETECT
#include <LGFX_AUTODETECT.hpp>
#include <LovyanGFX.hpp>

static LGFX lcd;

// QR204 UART
HardwareSerial PrinterSerial(1);

#define TXD2 43
#define RXD2 44

bool lastTouch = false;

void printHello()
{
  // Initialize printer
  uint8_t init_cmd[] = {0x1B, 0x40};
  PrinterSerial.write(init_cmd, sizeof(init_cmd));

  delay(200);

  PrinterSerial.println();
  PrinterSerial.println("========================");
  PrinterSerial.println("Hello World!");
  PrinterSerial.println();
  PrinterSerial.println("I am QR204");
  PrinterSerial.println("Thermal Printer");
  PrinterSerial.println("========================");

  PrinterSerial.println();
  PrinterSerial.println();
  PrinterSerial.println();

  // Feed paper
  PrinterSerial.write(0x0A);
  PrinterSerial.write(0x0A);
  PrinterSerial.write(0x0A);
}

void setup()
{
  Serial.begin(115200);

  lcd.init();
  lcd.setRotation(1);
  lcd.setBrightness(255);

  lcd.fillScreen(TFT_BLACK);

  lcd.setTextColor(TFT_WHITE);
  lcd.setTextDatum(middle_center);

  lcd.setFont(&fonts::FreeSans12pt7b);

  lcd.drawString("PRESS BUTTON", 160, 90);
  lcd.drawString("TO PRINT", 160, 140);

  PrinterSerial.begin(
      9600,
      SERIAL_8N1,
      RXD2,
      TXD2);

  Serial.println("Ready");
}

void loop()
{
  uint16_t x, y;

  bool touched = lcd.getTouch(&x, &y);

  if (touched && !lastTouch)
  {
    Serial.println("Printing...");

    lcd.fillScreen(TFT_BLACK);

    lcd.drawString("PRINTING...", 160, 120);

    printHello();

    delay(1500);

    lcd.fillScreen(TFT_BLACK);

    lcd.drawString("PRESS BUTTON", 160, 90);
    lcd.drawString("TO PRINT", 160, 140);
  }

  lastTouch = touched;

  delay(20);
}

Credits

Rohan Barnwal
52 projects • 41 followers
Rohan Barnwal - maker, hacker, tech enthusiast. I explore new tech & find innovative solutions. See my projects on hackster.io!

Comments