Saturday, November 10, 2007

Arduino --> printer (how)

This post is in response to a comment by Carl on an earlier post (see here).

The simplest method to interfacing an Arduino to a dot matrix printer is to use a BAS120 module or core chip (both of which can be bought at Microzed computers in Australia). These chips are designed to be used with Picaxe or Basic Stamp microcontrollers, but work fine with Arduino.

By setting the hardware serial speed of the Arduino to a baud rate of 2400, it is very easy to simply pass information onto the printer from, say, Max/MSP, using a serial interfacing protocol. This is because the printer module also runs at a baud rate of 2400.

It is then simply a matter of connecting the RX pin of the BAS120 module to the TX pin of the Arduino board.

There are a number of useful data byte to remember when using a printer. A '2' wakes the printer up so that it is ready to accept printing commands. A '3' puts the printer to sleep. A '10' followed by a '13' prints a carriage return followed by a line feed. In the case of the printer that has been featured in previous posts (Epson LQ 300), the '10,13' combination actually forced the printer to print. Some printers only start printing when they receive a page feed character (not just a line feed).

As one can see, the code below is very simple and offers no abstraction or symbolic conversion of any kind - all the printing data and manipulation has to occur from the computer end. But of course, it is possible to extend this code considerably such that the Arduino is doing more work.

Arduino Code

byte STX = 2;
byte data;

void setup() {
Serial.begin(2400);
Serial.print(STX);
digitalWrite(1, HIGH);
delay(5);
}

void loop() {
if(Serial.available() > 0) {
data = Serial.read();
Serial.print(data);
}
}

10 comments:

Carl said...

Thanks this is great. I look forward to experimenting with this.

Best,
Carl

Sebastian Tomczak said...

no probs, carl. enjoy!

Carl said...

Hey Sebastian,

So I just got the BAS-120 in the mail and I am looking forward to working with it. I just have one question that isn't quite clear on the module. Which pin is the RX pin on the BAS-120? There is that white holder on the left side with 10 pins coming out the side. This is where someone would just hookup the Stamp controller I am assuming, but since I am using Arduino I am not sure which pin to use to connect to the TX pin on the Arduino. Let me know.

Thanks,
Carl

Sebastian Tomczak said...

Hi Carl,
Please see this:
http://little-scale.blogspot.com/2007/11/bas120-pinout.html

It should clear up the pinout of the BAS120 in relation to the Arduino.

Carl said...

Sebastian,

Thanks for all your help with this printer stuff. I connected everything together and ran the Arduino code you posted, and I got a Smiley Face! Is that what you had intended? It was a nice welcome for sure. I will keep you updated on my status.

Thanks,
Carl

NES said...

Hi,

Does this only work with a dot matrix printer or is there a way to hook this up to an inkjet? And is there a place to get the BAS120 chip in Canada?

Thanks
~ NES

Sebastian Tomczak said...

The module uses the centronix / epson printing protocols. So as long as the modern inkjet supports these, it should be fine. I know that some inkjet printer are supported, but i do not know which ones.

HVW Tech [http://www.hvwtech.com/] are the official picaxe suppliers in Canada --- perhaps contact them? Or you could try finding a basic stamp supplier -- they might have them as well.

Cheers,

Harry Vermeulen said...

Hey Sebastian, I also ordered a bas120 in the hope of getting my receipt printer to work.

I connected everything uploaded the program you posted onto the arduino and started typing away in the "serial monitor" part of the arduino software, to see if it would print the words I typed, but it didn't. Would you be able to post a hello world example? That just prints Hello World every 3 seconds to test if I can actually print something? I can't seem to figure out how to do it.

By the way, I connected the pins from the bas120 directly to the pins of the printer. Pin 1 to Pin 1, Pin 2 to Pin 2 etc. Do you think that is right? I know some cables like a null modem used to have some pins switched in their cables.

Any help would be greatly appreciated!

Sebastian Tomczak said...

Hi Harry,

Firstly, try using printByte() instead of Serial.print() -- you may have better results.

This code was written on an old version of Arduino.

- Seb

Anonymous said...

Thank you very much. This works perfect for me :)