Fork me on GitHub

Project Notes

#524 Lucky 4D Printer

A “Lucky 4D” number generator, built with a JP-QR701 Thermal Printer, including a detour into to print Chinese (GB2312-80).

Build

Here’s a quick demo..

clip

Notes

So now I’ve figured out the basics of using the JP-QR701 Thermal Printer (see LEAP#523 Basics), time to do something a little more interesting(?)

Singapore has a “4D” (four digit) lottery that is especially popular during festivals like Chinese New Year.

This project generates “Lucky 4D” numbers at the press of a button. It’s a simple build, but I threw in an extra challenge - finding out how to get it to print Chinese characters.

Printing Chinese (GB2312-80)

The JP-QR701 specifications indicate that it has GB2312 support. I think this also applies to similar products that all seem to come from the same OEM or at least from the same design.

Most documentation - especially that found from English-language distributors - is however lacking in any detail on how to use the Chinese language support.

After a bit of digging around, I eventually found some documentation on the OEM 厦门精普电子科技有限公司 Xiamen Jingpu Electronic Technology Co Ltd site that revealed the trick - specifically in the QR701 Module Instruction Manual

It doesn’t use the standard character set / code page instructions, but rather uses two additional commands to enable/disable Chinese language support. When Chinese language support is enabled, 2-byte GB2312-80 characters can be written to the printer.

  • FS & - Set Chinese Character Mode
  • FS . - Cancel Chinese Character Mode
  • where FS is “Field Separator” chr(28)

Note: this is not a universal solution - it appears other printers may use other commands. The Kashino Technology CSN-A2 - that I believe may be the OEM for the AdaFruit Mini Thermal Receipt Printer (product ID 597) - appears to uses an ESC 9 n “select Chinese code format” command.

Encoding Some Chinese

I’m going to add 恭喜发财 to my 4D prediction for that extra dose of prosperity. I need the GB2312-80 coding, which I got the hard way by looking it up in some GB Code Tables:

Character Pinyin GB code
Gōng 0xB9A7
0xCFB2
0xB7A2
cái 0xB2C6

Adapting the Printer Library

I’m using the Adafruit Arduino Library for Small Thermal Printers, however this doesn’t directly support the required commands, and unfortunately hides away the necessary support functions inside the library’s private interface.

As a quick work-around, I’ve used a local copy of the Adafruit_Thermal.cpp and Adafruit_Thermal.h files and made a single modification: moved the writeBytes methods to the public interface.

So now I can send some Chinese characters (printed with all current settings applied e.g. size, weight, justification) like this:

printer.writeBytes(28, '&'); // FS &; Set Chinese Character Mode
printer.writeBytes(0xB9, 0xA7); // gong
printer.writeBytes(0xCF, 0xB2); // xi
printer.writeBytes(0xB7, 0xA2); // fa
printer.writeBytes(0xB2, 0xC6); // cai
printer.println();
printer.writeBytes(28, '.'); // FS .; Cancel Chinese Character Mode

Construction

The electronics are straight-forward:

  • barrel jack for 5V 2A power supply
  • power switch
  • mounted the leads for the printer connection
  • female header strips for mounting an Arduino Nano
  • a jumper that selects whether to power the Nano from the same supply as the printer or from the USB connector
  • a couple of bulk caps for the Nano supply to smooth out any power fluctuations caused by the printer
  • push-button connected to Arduino D2 (configured as INPUT_PULLUP)

Breadboard

Schematic

I’ve mounted it all in an old Raffles Hotel mooncake box for that extra (though conflicting) festive feeling!

wiring_up

All ready to play:

Build

Credits and References

About LEAP#524 ArduinoThermal Printer
Project Source on GitHub Project Gallery Return to the LEAP Catalog

This page is a web-friendly rendering of my project notes shared in the LEAP GitHub repository.

LEAP is just my personal collection of projects, usually involving an Arduino or other microprocessor in one way or another. Some are full-blown projects, while many are trivial breadboard experiments, intended to learn and explore something interesting (IMHO!).

The projects are usually inspired by things found wild on the net, or ideas from the sources such as:

Feel free to borrow liberally, and if you spot any issues do let me know. See the individual projects for credits where due. There are even now a few projects contributed by others - send your own over in a pull request if you would also like to add to this collection.