Advertisement
microrobotics

EM5820 Thermal Printer - Serial Interface TTL

Apr 4th, 2023
1,480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The EM5820 Thermal Printer with a serial interface can be easily connected to an Arduino using the SoftwareSerial library. Here's an example code to print text and a simple horizontal line using the EM5820 Thermal Printer:
  3.  
  4. This code initializes the printer, configures the print settings, and then prints text followed by a horizontal line.
  5.  
  6. Connect the EM5820 Thermal Printer to the Arduino as follows:
  7.  
  8. VCC pin to 5V (or the appropriate voltage for your printer)
  9. GND pin to GND
  10. RX pin to digital pin 3 on the Arduino
  11. TX pin to digital pin 2 on the Arduino
  12. Please note that you may need to adjust the pin numbers in the code depending on the specific pins you choose to use for connecting the printer to your Arduino. Additionally, make sure that the EM5820 printer is powered by a power supply with enough current to operate properly, as the Arduino's onboard 5V regulator might not provide sufficient power.
  13. */
  14. #include <Arduino.h>
  15. #include <SoftwareSerial.h>
  16.  
  17. const int printerRxPin = 2; // Connect this pin to the TX pin of the printer
  18. const int printerTxPin = 3; // Connect this pin to the RX pin of the printer
  19.  
  20. SoftwareSerial printerSerial(printerRxPin, printerTxPin);
  21.  
  22. void setup() {
  23.   Serial.begin(9600);
  24.   printerSerial.begin(9600);
  25.   delay(500); // Allow time for the printer to initialize
  26.  
  27.   // Configure printer
  28.   printerSerial.write(27); // ESC
  29.   printerSerial.write(64); // @ - Initialize printer
  30.  
  31.   printerSerial.write(27); // ESC
  32.   printerSerial.write(55); // 7 - Print settings
  33.   printerSerial.write(7);  // Heating dots (7 - max heat)
  34.   printerSerial.write(100); // Print and feed time (100 * 10 microseconds)
  35.   printerSerial.write(20); // Heating time (20 * 10 microseconds)
  36.  
  37.   // Print text
  38.   printerSerial.print("Hello, EM5820 Thermal Printer!\n");
  39.  
  40.   // Print a horizontal line
  41.   for (int i = 0; i < 32; i++) {
  42.     printerSerial.write(0xFF);
  43.   }
  44.   printerSerial.write('\n');
  45. }
  46.  
  47. void loop() {
  48.   // Nothing to do in the loop
  49. }
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement