Displaying on Paper – Thermal Printer + Arduino - Projeto TI
Headlines News :

.

Home » » Displaying on Paper – Thermal Printer + Arduino

Displaying on Paper – Thermal Printer + Arduino

Written By x86_g on 2012-10-15 | 12:28 PM


The following code and library are compatible with arduino software 1.0+ ONLY. You can download the newest version of the arduino software here.
Outputting data can be extremely useful, and typically when we look at doing this, it is either to the Arduino’s serial terminal, or to some sort of display. But what about making physical copies of the data? So a few months back,SparkFun started selling a thermal printer that you could connect to a microcontroller (or via adafruit ). That same day my brain filled with crazy thing you could do with it, like a giant fortune cookie that could print your fortune on the fly.
Anyways… If you dont know about thermal printers, they are most often the printers your store’s receipts are printed on. The reason for this is that they dont use ink, or use a cartridge of any sort. The paper it prints on turns black when heated. So this printer simply applies heat where another printer would apply ink. Eventually the printers head will wear out, but this after several miles of printing. Yes, miles – about 30 of them. So given that the rolls of paper are 34ft long, you can print about 4,600 rolls before the print head dies, meaning for most of us, you will never see that happen.

Hooking it up

So this printer can be powered off of 5V, but NOT the 5v pin on your arduino, and no via USB. It uses very little power when it is is just sitting by, but uses around a full amp when it is printing! And because the most USB can handle is half of that, we need an external power supply. Something between 5 and 9V, and a minimum of 1400ma output (larger is fine). I found that powering the printer with 9v, it printed much faster, and slightly darker than 5v, and if you have it available, I recommend using 9v.
There are two ways of connecting the power supply as shown in the illustrations. You can either power your arduino and the printer separately, or you can power the arduino, and power the printer off of the arduino.
To do the latter, the printer’s power wire (red) needs to connect to the VIN pin on the arduino, not the 5v pin. The reason is that the 5v pin goes through a regulator and is not meant to supply this kind of power, but the VIN pin is directly connected to the power input on the arduino. So it can supply much more.
Now just connect the rest of the wires as shown.

Code

The printer needs a serial connection to the arduino. And normally that would mean we would connect to digital pins 0,1 but, if we did this, anything you printed to the serial terminal for debugging or otherwise the printer would see and possibly print out. We don’t want that, so we will be using a libraty so that you can keep those pins free.
This printer has a huge amount of features. 2 different barcode, text output, size settings etc, and each one needs a new configuration to get it working. So we created a library for this so that you could keep your code clean, and simple.

keywords.txt - Text
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#######################################################
# keywords.txt - keywords file for the Thermal library
# Created by Adam Meyer of bildr Aug 10th 2011
# Released as MIT license
#######################################################
#######################################
# Datatypes (KEYWORD1)
#######################################
Thermal KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
test KEYWORD2
printBarcode KEYWORD2
printFancyBarcode KEYWORD2
boldOn KEYWORD2
boldOff KEYWORD2
sleep KEYWORD2
wake KEYWORD2
setSize KEYWORD2
setBarcodeHeight KEYWORD2
feed KEYWORD2
tab KEYWORD2
justify KEYWORD2
doubleHeightOn KEYWORD2
doubleHeightOff KEYWORD2
inverseOn KEYWORD2
inverseOff KEYWORD2
setDefault KEYWORD2
setHeatTime KEYWORD2
setHeatInterval KEYWORD2
setPrintDensity KEYWORD2
setPrintBreakTime KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
License.txt - Text
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Copyright (c) 2010 bildr community

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Read_Me.txt - Text
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//ARDUINO 1.0 COMPATIBLE ONLY!
The code is provided under the MIT license please use, edit, change, and share.

Thanks to Adafruit (adafruit.com) for bitmap support.

Before loading the Thermal_example code, or even opening the arduino software, place the Thermal folder in your arduino library.

////ARDUINO LIBRARY LOCATION////
On your Mac:: In (home directory)/Documents/Arduino/libraries
On your PC:: My Documents -> Arduino -> libraries
On your Linux box: (home directory)/sketchbook/libraries
Thermal - Other
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Thermal.cpp - C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <Arduino.h>
#include <Thermal.h>



Thermal::Thermal(int RX_Pin, int TX_Pin, long baudRate) : SoftwareSerial(RX_Pin, TX_Pin)

{

pinMode(RX_Pin, INPUT);

pinMode(TX_Pin, OUTPUT);

begin(baudRate);



int zero = 0;



heatTime = 80; // 80 is default from page 23 of datasheet. Controls speed of printing and darkness
heatInterval = 2; // 2 is default from page 23 of datasheet. Controls speed of printing and darkness
printDensity = 15; // Not sure what the defaut is. Testing shows the max helps darken text. From page 23.
printBreakTime = 15; // Not sure what the defaut is. Testing shows the max helps darken text. From page 23.


setHeatTime(heatTime);
setPrintDensity(printDensity);


setDefault();
}

void Thermal::setDefault()

{
wake();
justify('L');
inverseOff();
doubleHeightOff();
setLineHeight(32);
boldOff();
underlineOff();
setBarcodeHeight(50);
setSize('s');
}

void Thermal::test()

{
println("Hello World!");
feed(2);
}



void Thermal::setBarcodeHeight(int val)

{
// default is 50
writeBytes(29, 104, val);
}

void Thermal::printBarcode(char * text){
writeBytes(29, 107, 0); // GS, K, m!

for(int i = 0; i < strlen(text); i ++){
write(text[i]); //Data
}

write(zero); //Terminator

delay(3000); //For some reason we can't immediately have line feeds here
feed(2);
}

void Thermal::printFancyBarcode(char * text){
writeBytes(29, 107, 4); // GS, K, Fancy!

for(int i = 0; i < strlen(text); i ++){
write(text[i]); //Data
}

write(zero); //Terminator

delay(3000); //For some reason we can't immediately have line feeds here
feed(2);
}

void Thermal::writeBytes(uint8_t a, uint8_t b)

{
write(a);
write(b);
}

void Thermal::writeBytes(uint8_t a, uint8_t b, uint8_t c)

{
write(a);
write(b);
write(c);
}

void Thermal::writeBytes(uint8_t a, uint8_t b, uint8_t c, uint8_t d)

{
write(a);
write(b);
write(c);
write(d);
}

void Thermal::inverseOn()

{
writeBytes(29, 'B', 1);
}

void Thermal::inverseOff()

{
writeBytes(29, 'B', 0, 10);
}

void Thermal::doubleHeightOn()

{
writeBytes(27, 14);
}

void Thermal::doubleHeightOff()

{
writeBytes(27, 20);
}

void Thermal::boldOn()

{
writeBytes(27, 69, 1);
}

void Thermal::boldOff()

{
writeBytes(27, 69, 0);
if (linefeedneeded)
feed();

linefeedneeded = false;
}

void Thermal::justify(char value)

{
uint8_t pos = 0;


if(value == 'l' || value == 'L') pos = 0;
if(value == 'c' || value == 'C') pos = 1;
if(value == 'r' || value == 'R') pos = 2;

writeBytes(0x1B, 0x61, pos);
}

void Thermal::feed(uint8_t x)

{
while (x--)
write(10);
}

void Thermal::setSize(char value)

{
int size = 0;


if(value == 's' || value == 'S') size = 0;
if(value == 'm' || value == 'M') size = 10;
if(value == 'l' || value == 'L') size = 25;

writeBytes(29, 33, size, 10);
// if (linefeedneeded)
// println("lfn"); //feed();
//linefeedneeded = false;
}

void Thermal::underlineOff()

{
writeBytes(27, 45, 0, 10);
}


void Thermal::underlineOn()

{
writeBytes(27, 45, 1);
}

void Thermal::printBitmap(uint8_t w, uint8_t h, const uint8_t *bitmap)

{
writeBytes(18, 42, h, w/8);
for (uint16_t i=0; i<(w/8) * h; i++) {
write(pgm_read_byte(bitmap + i));
}
}

void Thermal::wake()

{
writeBytes(27, 61, 1);
}

void Thermal::sleep()

{
writeBytes(27, 61, 0);
}

////////////////////// not working?
void Thermal::tab()

{
write(9);
}


void Thermal::setCharSpacing(int spacing)

{
writeBytes(27, 32, 0, 10);
}


void Thermal::setLineHeight(int val)

{
writeBytes(27, 51, val); // default is 32
}



void Thermal::setHeatTime(int vHeatTime)

{

heatTime = vHeatTime;

write(27);

write(55);

write(7); //Default 64 dots = 8*('7'+1)

write(heatTime); //Default 80 or 800us

write(heatInterval); //Default 2 or 20us

}



void Thermal::setHeatInterval(int vHeatInterval)

{

heatInterval = vHeatInterval;

write(27);

write(55);

write(7); //Default 64 dots = 8*('7'+1)

write(heatTime); //Default 80 or 800us

write(heatInterval); //Default 2 or 20us

}



void Thermal::setPrintDensity(char vPrintDensity)

{

//Modify the print density and timeout

printDensity = vPrintDensity;

write(18);

write(35);

int printSetting = (printDensity<<4) | printBreakTime;

write(printSetting); //Combination of printDensity and printBreakTime

}



void Thermal::setPrintBreakTime(char vPrintBreakTime)

{

//Modify the print density and timeout

printBreakTime = vPrintBreakTime;

write(18);

write(35);

int printSetting = (printDensity<<4) | printBreakTime;

write(printSetting); //Combination of printDensity and printBreakTime

}
Thermal.h - Header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef Thermal_h
#define Thermal_h

#include <Arduino.h>
#include <SoftwareSerial.h>



class Thermal : public SoftwareSerial

{

public:

Thermal(int, int, long); // constructor
void setDefault();
void test();



void inverseOn();
void inverseOff();
void doubleHeightOn();
void doubleHeightOff();
void boldOn();
void boldOff();
void underlineOn();
void underlineOff();



void justify(char value);
void feed(uint8_t x = 1);
void sleep();
void wake();



void setCharSpacing(int spacing);
void setSize(char value);
void setLineHeight(int val = 32);



void printBarcode(char * text);
void printFancyBarcode(char * text);
void setBarcodeHeight(int val);



void printBitmap(uint8_t w, uint8_t h, const uint8_t *bitmap);

// ??
void tab();



void setHeatTime(int vHeatTime);

void setHeatInterval(int vHeatInterval);

void setPrintDensity(char vPrintDensity);

void setPrintBreakTime(char vPrintBreakTime);


private:
boolean linefeedneeded;


// little helpers to make code easier to read&use
void writeBytes(uint8_t a, uint8_t b);
void writeBytes(uint8_t a, uint8_t b, uint8_t c);
void writeBytes(uint8_t a, uint8_t b, uint8_t c, uint8_t d);


int zero;


int heatTime;
int heatInterval;
char printDensity;
char printBreakTime;
};

#endif
Thermal_Example.ino - Arduino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//ARDUINO 1.0 COMPATIBLE ONLY!
//ARDUINO 1.0 COMPATIBLE ONLY!
//ARDUINO 1.0 COMPATIBLE ONLY!

#include <SoftwareSerial.h>
#include <Thermal.h>

int printer_RX_Pin = 2;
int printer_TX_Pin = 3;

Thermal printer(printer_RX_Pin, printer_TX_Pin, 19200);

void setup(){
////////////////////////////////////////////////////////////////////
//Following are in setup, but do not need to be. Use them anywhere.
//Just here so they do not just keep printing over and over
//Printer will ignore commands during printing, so use delay(3000)
//after prints to ensure it see everything you want to print.
//SOME FUNCTIONS WILL FEED A LINE WHEN CALLED TO SOLIDIFY SETTING
////////////////////////////////////////////////////////////////////

printer.justify('R'); //sets text justification (right, left, center) accepts 'L', 'C', 'R'

printer.setSize('L'); // set type size, accepts 'S', 'M', 'L'
printer.println("hello"); //print line

printer.setSize('M'); // set type size, accepts 'S', 'M', 'L'
printer.println("hello"); //print line

printer.setSize('S'); // set type size, accepts 'S', 'M', 'L'
printer.println("hello"); //print line

printer.feed(); //advance one line
printer.feed(); //advance one line


//printer.setHeatTime(80); // 80 is default from page 23 of datasheet. Controls speed of printing and darkness
//printer.setHeatInterval(2); // 2 is default from page 23 of datasheet. Controls speed of printing and darkness
//printer.setPrintDensity(15); // Not sure what the defaut is. Testing shows the max helps darken text. From page 23.
//printer.setPrintBreakTime(15); // Not sure what the defaut is. Testing shows the max helps darken text. From page 23.

//printer.print("hello"); //add text to print line without printing it. Call println to print.//printer.print(1234);
//printer.println("hello"); //print line//printer.println(1234);

//printer.printBarcode("123456789123"); //print simple bar code - up to 12 characters long//printer.printFancyBarCode("WWW.BILDR.ORG"); // print fancy barcode. Cap latters and some symbols
//printer.printFancyBarcode("WWW.BILDR.ORG"); // print fancy barcode. Cap latters and some symbols
//printer.setBarcodeHeight(50); // set barcode px height: 0-255

//printer.boldOn(); // Turn bold on//printer.boldOff(); //Rurn bold off
//printer.doubleHeightOn(); // sets type to print double hight//printer.doubleHeightOff(); //Turn off double hight printing
//printer.inverseOn(); //set to print white on black//printer.inverseOff(); //set to print black on white (default)

//printer.sleep(); //Tell printer to sleep. MUST call wake before printing again, even if reset//printer.wake(); //Wake //printer.
//printer.wake();

//printer.setSize('L'); // set type size, accepts 'S', 'M', 'L'
//printer.feed(); //advance one line
//printer.tab(); //Tabs text over 8 spaces

//printer.justify('R'); //sets text justification (left, center, right) accepts 'L', 'C', 'R'

//printer.setDefault(); //set printer to defaults. ****WILL FEED SEVERAL LINES WHEN CALLED***

}

void loop(){


}

Share this article :

0 comentários:

Postar um comentário