Adafruit thermal printer

Hello everybody,

About a year ago I wrote a code for an adafruit thermal printer. As the wiring was really bad back then, I'm fixing it now. But my wiring doesn't work anymore...

The printers RX and TX are connected to my micro pro as shown in the code below. For debugging, I'm ignoring all voids, except for "void printing". (The rest of the code is for accessing a SD-card, and this works)

The printer is connected to 9V and gets his 9V. The ground of the printer is connected to a ground on the arduino, so they share a common ground. Furthermore, the connections for RX and TX are good, I checked them with a multimeter.

The printer itself works. I jumped the J1 contacts and the test-page is printed. There i also see that baudrate of 9600 is correct.

But when starting my code, it says that void printing is entered, but is doesn't print. The example code isn't printed as well. I just can't find why...

I just assume the error lies somewhere with my RX/TX connections, but otherwise I'm clueless...

Here's the documentation I used:
https://learn.adafruit.com/mini-thermal-receipt-printer/making-connections

#include <SPI.h>
#include <SD.h>
#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"

#define TX_PIN 6 // Arduino transmit  YELLOW WIRE  labeled RX on printer
#define RX_PIN 5 // Arduino receive   GREEN WIRE   labeled TX on printer

SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
Adafruit_Thermal printer(&mySerial);     // Pass addr to printer constructor

String Text="default";
String fileName="print.txt";
File myFile;
char cha;
int zaehlerzeile;
int zaehlersuche;
int gefunden=0;
//int loop1=0;
int linescounted=0;
int ziel;
int maxzeile;
int max_zeile;


void setup() {
  pinMode(2, INPUT);
  //pinMode(3, OUTPUT);
  //digitalWrite(3,HIGH);
  Serial.begin(9600);
  mySerial.begin(9600);
  //Printer setup
  printer.begin();
  printer.setLineHeight(50);
  delay(5000);
  Serial.println("setup complete");
}

void loop (){
  //Serial.println("ready");
  //Pressing button on pin 2 starts process
  if(digitalRead(2)){
    Serial.println("button pressed");
    delay(1000);
    //initializeCard();
    //read lines on SD card
    //countlines();   
    //randomSeed(micros());
    //max_zeile=zaehlerzeile+1;     
    //generate random number
    //zaehlersuche=random(1, max_zeile);   
    //seek and save line
    //seekline();  
    //print
    Serial.println(Text);
    printer.wake();
    printing();   
    Text="";
  }
  
}
void initializeCard(){
  //Serial.println("initialize card");
  if(!SD.begin(4)){
    while(1);
  }
  //Serial.println("initialize finished");
}

void countlines(){
  myFile=SD.open(fileName);
  if(myFile){
    zaehlerzeile=0;
    while(myFile.available()){
        cha=myFile.read();
        if(cha=='\n'){
            zaehlerzeile++;
          }
      }
    zaehlerzeile++;
  }
  myFile.close();
  maxzeile=zaehlerzeile;
  //Serial.println(maxzeile);
}

void seekline(){
  gefunden=0;    
  myFile=SD.open(fileName);
  if(myFile){
    zaehlerzeile=1;
    while(myFile.available()){
      if(zaehlersuche==1){
        read();
      }
      while(gefunden==0){
        cha=myFile.read();
        if(cha=='\n'){
          zaehlerzeile++;
          if(zaehlerzeile==zaehlersuche){
            read();            
          }
        }
      }
    }
  }
}
  
void read(){
  Text=myFile.readStringUntil('\n');
  gefunden=1;
  myFile.close();
  //Serial.println(Text);
}

void printing(){
  Serial.println("printing");
  printer.justify('C');
  printer.setSize('L');
  printer.setLineHeight(50);
  printer.println("");
  printer.println("");
  printer.println("");
  printer.println(Text);
  printer.println("");
  printer.println("");
  printer.println("");
  printer.println("");
  //delay(2000);
  
}

They are not "voids". They are functions. The keyword void in the declaration of a function simply means that the function does not return a value.

But functions can return a value, where that is useful or makes your code more readable. For example, your function countlines() could be improved by returning a value:

int countlines(){
  myFile=SD.open(fileName);
  if(myFile){
    int zaehlerzeile=0;
    while(myFile.available()){
        cha=myFile.read();
        if(cha=='\n'){
            zaehlerzeile++;
          }
      }
    zaehlerzeile++;
  }
  myFile.close();
  //Serial.println(maxzeile);
  return zaehlerzeile;
}

You should not be using software serial on Pro Micro because it has a hardware serial port available. We should never use software serial where there is a hardware serial port available to use.

So I assume I should connect the pin labeled RX on the printer to the TX pin on my micro pro?

Furthermore remove the lines

#include "SoftwareSerial.h"
#define TX_PIN 6 // Arduino transmit  YELLOW WIRE  labeled RX on printer
#define RX_PIN 5 // Arduino receive   GREEN WIRE   labeled TX on printer

and instead of

mySerial.begin(9600);

use

Serial1.begin(9600);

Is this correct or do I have to change anything else?

@halbflauschmammut sorry if my comments are not solving your immediate problem. I am reviewing what you put in your post and these are things that occur to me.

Do you have a serial-usb adapter that you could use for testing?

You need to change this line also:

Adafruit_Thermal printer(&Serial1);     // Pass addr to printer constructor

Pro Micro are available in 3.3V and 5V versions. If you have the 3.3V and the printer expects 5V signals, this could be the problem. You may need to boost the signal. This is pretty easy to do with just a transistor and a couple of resistors, for example.

This is welcome as I'm a beginner. But since this part of the code isn't my problem at the moment, I'm not worried about this. I might change this when the more urgent problems are solved.

I honestly don't know what a serial-usb adapter you mean... I have the micro connected to my PC and watch via serial monitor what's happening.

I changed the code and rewired the printer, but still no printing...

Just to make sure:
I used the RX and TX shown here:
https://forum.arduino.cc/t/adafruit-thermal-printer/1224998/4

And the code looks like this now:

#include <SPI.h>
#include <SD.h>
#include "Adafruit_Thermal.h"
//#include "SoftwareSerial.h"

//#define TX_PIN 6 // Arduino transmit  YELLOW WIRE  labeled RX on printer
//#define RX_PIN 5 // Arduino receive   GREEN WIRE   labeled TX on printer

//SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
//Adafruit_Thermal printer(&mySerial);     // Pass addr to printer constructor
Adafruit_Thermal printer(&Serial1);

String Text="default";
String fileName="print.txt";
File myFile;
char cha;
int zaehlerzeile;
int zaehlersuche;
int gefunden=0;
//int loop1=0;
int linescounted=0;
int ziel;
int maxzeile;
int max_zeile;


void setup() {
  pinMode(2, INPUT);
  //pinMode(3, OUTPUT);
  //digitalWrite(3,HIGH);
  Serial.begin(9600);
  //mySerial.begin(9600);
  Serial1.begin(9600);
  //Printer setup
  printer.begin();
  printer.setLineHeight(50);
  delay(5000);
  Serial.println("setup complete");
}

void loop (){
  //Serial.println("ready");
  //Pressing button on pin 2 starts process
  if(digitalRead(2)){
    Serial.println("button pressed");
    delay(1000);
    //initializeCard();
    //read lines on SD card
    //countlines();   
    //randomSeed(micros());
    //max_zeile=zaehlerzeile+1;     
    //generate random number
    //zaehlersuche=random(1, max_zeile);   
    //seek and save line
    //seekline();  
    //print
    Serial.println(Text);
    printer.wake();
    printing();   
    //Text="";
  }
  
}
void initializeCard(){
  //Serial.println("initialize card");
  if(!SD.begin(4)){
    while(1);
  }
  //Serial.println("initialize finished");
}

void countlines(){
  myFile=SD.open(fileName);
  if(myFile){
    zaehlerzeile=0;
    while(myFile.available()){
        cha=myFile.read();
        if(cha=='\n'){
            zaehlerzeile++;
          }
      }
    zaehlerzeile++;
  }
  myFile.close();
  maxzeile=zaehlerzeile;
  //Serial.println(maxzeile);
}

void seekline(){
  gefunden=0;    
  myFile=SD.open(fileName);
  if(myFile){
    zaehlerzeile=1;
    while(myFile.available()){
      if(zaehlersuche==1){
        read();
      }
      while(gefunden==0){
        cha=myFile.read();
        if(cha=='\n'){
          zaehlerzeile++;
          if(zaehlerzeile==zaehlersuche){
            read();            
          }
        }
      }
    }
  }
}
  
void read(){
  Text=myFile.readStringUntil('\n');
  gefunden=1;
  myFile.close();
  //Serial.println(Text);
}

void printing(){
  Serial.println("printing");
  printer.justify('C');
  printer.setSize('L');
  printer.setLineHeight(50);
  printer.println("");
  printer.println("");
  printer.println("");
  printer.println(Text);
  printer.println("");
  printer.println("");
  printer.println("");
  printer.println("");
  //delay(2000);
  
}

I made pio 3 an output and set it to high. The voltage measured is 5V, so this is hardly the problem.

1 Like

Something like these


s-l300 (1)

They are a generally useful thing to have in your arduino toolbox.

You could connect one to the Tx output of the Pro Micro instead of or in parallel with the printer. When you connect it to your PC with a USB cable, a new COM/tty port/device will appear. You can select that, open serial monitor, set 9600 baud, and see exactly what characters (if any) are being sent to the printer by your code.

Alternatively you could use it to connect your PC directly to the printer without using the Arduino. You could send commands to the printer from the serial monitor and see the response the printer sends back.

Can I just check you understand that when using the hardware serial port on Pro Micro, the pins are 0 and 1, you cannot choose other pins like you can with software serial.

Yes, I used pin 0 and 1, labeled RX and TX on the microcontroller.

I will try to get a serial-usb adapter. In the meantime, if you have another good idea what the problem could be, please let me know.

You could try an led (+ series resistor) in place of the printer. You should see the led flickering as the data is sent. You might need to dim the lights in the room to see it at all. Or you might see the led is lit all the time, but as the data is sent, it flickers dimly

This doesn't prove that the correct data is being sent or received correctly by the printer, but at least it indicates that some data is being sent!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.