# Thermal Printer controlled by a push button

**URL:** <https://forum.arduino.cc/t/thermal-printer-controlled-by-a-push-button/1247640>\
**Category:** General Guidance\
**Created:** [April 14, 2024, 4:19pm UTC](https://forum.arduino.cc/t/thermal-printer-controlled-by-a-push-button/1247640 "2024-04-14T16:19:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![eli\_lee](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@eli\_lee](https://forum.arduino.cc/u/eli_lee)\
**Post date:** [April 14, 2024, 4:19pm UTC](https://forum.arduino.cc/t/thermal-printer-controlled-by-a-push-button/1247640/1 "2024-04-14T16:19:48Z")

</div>

Hello, everyone!  
I've recently bought an Arduino Kit and developed a project trying to print some phrase controlled by a push button. With all you guys' helps, following former Q&As , I managed to connect and initiate QR204 Mini Thermal Printer from AliExpress (since ones from Adafruit are no more possible to get), and it works on 1 time without the button.

This is the code:

```arduino
#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"
#define TX_PIN 3
#define RX_PIN 2

SoftwareSerial mySerial(RX_PIN, TX_PIN); 
Adafruit_Thermal printer(&mySerial); 

void setup() {

  mySerial.begin(9600);
  printer.begin(9600); 
  printer.wake();       
  printer.setDefault();
    
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    delay(4000);
    printer.println(F("Text1"));
    delay(3000);
    printer.setLineHeight(50);
    printer.justify('C');
    printer.inverseOn();
    printer.println(F("Text2"));
    printer.inverseOff();

    printer.justify('L');
    printer.setLineHeight(150);
    printer.println(F("Text3"));
    printer.setLineHeight();
    printer.println(F("Text4, "));
    delay (2000);

    printer.setLineHeight(100);
    printer.println(F("Text5")); 
    printer.justify('C');
    printer.setLineHeight(150);
    delay(3000);  

    printer.println(F("Text6"));
    printer.justify('L');
    printer.setLineHeight();
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    delay(3000);

    printer.setDefault();
    printer.sleep();
}

void loop() {
}

```

And now I'm trying to make it work when the button is pushed, but it doesn't work.  
I cannot find the reason so writing this for help.  
Below is the code I failed.

```arduino
#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"
#define TX_PIN 3
#define RX_PIN 2 
#define button1 7

SoftwareSerial mySerial(RX_PIN, TX_PIN); 
Adafruit_Thermal printer(&mySerial);     

void setup() {
  pinMode(button1, INPUT_PULLUP);
  Serial.begin(9600); //PC-Arduino Communication
  mySerial.begin(9600); //Arduino-Printer Communication
  printer.begin();
}

void loop() {
  //Pushing button
  if(digitalRead(button1) == LOW){
    //button is pressed
    printer.wake();     

    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    delay(4000);
    printer.println(F("Text1"));
    delay(3000);
    printer.setLineHeight(50);
    printer.justify('C');
    printer.inverseOn();
    printer.println(F("Text2"));
    printer.inverseOff();

    printer.justify('L');
    printer.setLineHeight(150);
    printer.println(F("Text3"));
    printer.setLineHeight();
    printer.println(F("Text4"));
    delay (2000);

    printer.setLineHeight(100);
    printer.println(F("Text5")); 
    printer.justify('C');
    printer.setLineHeight(150);
    delay(3000);  

    printer.println(F("Text6"));
    printer.justify('L');
    printer.setLineHeight();
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    printer.println(F(" "));
    delay(3000);

    printer.setDefault();
    printer.sleep();

  }
}

```

Plus, adding next code for the end part also doesn't work.

```arduino
else { 
printer.setDefault();
    printer.sleep(); }

```

And this is the circuit.

 ![IMG_5248](https://europe1.discourse-cdn.com/arduino/original/4X/d/0/9/d09f3ca473303e31ca591426a0a27ef5dd22f5c5.jpeg)

I'm so novice to Arduino and coding, and also English is not my first language so sorry for all clumsinesses.

Thank you!

---

<div class="post-metadata">

**Author:** ![markd833](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/markd833/32/1186928_2.png) [@markd833](https://forum.arduino.cc/u/markd833)\
**Post date:** [April 14, 2024, 4:43pm UTC](https://forum.arduino.cc/t/thermal-printer-controlled-by-a-push-button/1247640/2 "2024-04-14T16:43:27Z")

</div>

There should be 2 wires coming from your "mini breadboard" - one to pin 7 and the other the the UNO GND.

What is the purpose of the 9V battery attached to the mini breadboard? Is it an illuminated pushbutton?

You should be able to connect the pushbutton between UNO Pin 7 and UNO GND.

---

<div class="post-metadata">

**Author:** ![DaveX](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/davex/32/751443_2.png) [@DaveX](https://forum.arduino.cc/u/DaveX)\
**Post date:** [April 14, 2024, 4:44pm UTC](https://forum.arduino.cc/t/thermal-printer-controlled-by-a-push-button/1247640/3 "2024-04-14T16:44:28Z")

</div>

Look at the Arduino File/Example/02.Digital/Button example, and consider putting much of the stuff in your setup() function into a printLineOnTheThermalPrinter() function per

> [@Planning and Implementing an Arduino Program](https://forum.arduino.cc/t/planning-and-implementing-an-arduino-program/252364):
>
> From what I can see there are many tutorials about doing specific tasks with an Arduino but I have not come across any that guide a newcomer through the process of planning and implementing a program to make it easy to understand and debug. The following 9 posts in this Thread are an attempt to fill that gap. ...R

You might modify the Button example to also call a printLineOnTheThermalPrinter() function ehrn the button is pressed.

---

<div class="post-metadata">

**Author:** ![eli\_lee](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@eli\_lee](https://forum.arduino.cc/u/eli_lee)\
**Post date:** [April 14, 2024, 5:06pm UTC](https://forum.arduino.cc/t/thermal-printer-controlled-by-a-push-button/1247640/4 "2024-04-14T17:06:48Z")

</div>

OMG it works!!!!! I still can't believe this goes well... 🫢  
You saved my time, energy, .. and other things!!! Thanks a lot!!!  
I'Il do my best to give a lot to beginners like me in the future. Super thanks!

---

<div class="post-metadata">

**Author:** ![eli\_lee](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@eli\_lee](https://forum.arduino.cc/u/eli_lee)\
**Post date:** [April 14, 2024, 5:09pm UTC](https://forum.arduino.cc/t/thermal-printer-controlled-by-a-push-button/1247640/5 "2024-04-14T17:09:05Z")

</div>

Since it works, now I'm going to make it sophisticated by your suggestion and the link. Thank you so much!

---

<div class="post-metadata">

**Author:** ![eli\_lee](https://avatars.discourse-cdn.com/v4/letter/e/e5b9ba/32.png) [@eli\_lee](https://forum.arduino.cc/u/eli_lee)\
**Post date:** [April 15, 2024, 4:49am UTC](https://forum.arduino.cc/t/thermal-printer-controlled-by-a-push-button/1247640/6 "2024-04-15T04:49:40Z")

</div>

Thanks to you and the kind instruction attached, I revised my code and it is now much easier to read and apply. Huge thanks from the bottom of my heart! Have a nice day 😁

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 12, 2024, 4:49am UTC](https://forum.arduino.cc/t/thermal-printer-controlled-by-a-push-button/1247640/7 "2024-10-12T04:49:56Z")

</div>

Closed on October 12, 2024, 4:49am UTC

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