applying the parallel port to drive a LED and read a switch from tcl

by Theo Verelst

The title should have mentioned that, in this case, I make the software run on a Microsoft Windows machine, though I understand the driver isn't necessary on Linux, so that should be fairly easy. I just don't have access to a Linux machine with usable parallel port.

A LED (Light Emitting Diode) is one of those small, (almost?) everlasting lights, usually in red, green, or blue, though nowadays you can buy them in any electronics store (there are over 230 in the small country of Holland) in blue, yellow, and orange, too, and even in white and 'high brightness' version. They used to come in 3 mm or 5 mm diameter, but you also have jumbos, rectangular, and many other shapes, lately especially very tiny SMD (surface mounted device) kinds, which you can find on computer boards.

This page describes the use of a LED on a printer port, to indicate something, for instance while the PC screen is turned off or in screen save mode, like whether your server is still running, or whether some process is finished. Your electronics store will be happy to sell you a LED, some wire (to place it away from the printer port connector), a few resistors, and a (push button) switch, and of course the printer port parallel connector plug (male), which is fairly standard. All in all, probably less then 10 (or even 5) dollars/euros.

Yet the advantages are considerable. First however consider that connecting wires/parts to your wonderful, expensive computer may damage it permanently when you err, and in certain ways that you may not be aware of, so be careful to have someone knowledgeable do the electronics work. Especially, apart from of course connecting nothing that produces electricity to the printer port (such as a mains connector...), you should be aware of static electricity sources as a possible wrecker of your computer circuits. Normally, when using standard connectors, your fingers, your woolen sweater (...), or some other ungrounded equipment will not touch the internal pins of, for instance, the printer connector. When you do, there is a chance of doing damage to the printer port circuitry (which isn't that expensive), or worse to the further internals of you computer. When you make sure you have grounded the computer, and preferably by touching a metal enclose of it, also yourself, the risk of touching any pin (still don't wear clothes that make you draw sparks when you touch metal) is minimal, so you can touch the LDE and switch without risk.

You only need a ground wire, and one status bit, and one data bit for this example, meaning only 3 wires need to be soldered in the printer 25-pin D plug. I soldered most relevant connections to a long flat cable, to act as a experimenter connection wire. I guess about 5 meters is the maximum normal length.

The idea of the simple circuit to interact with a tcl program without the need for a screen (or keyboard) with one LED and one push switch is:

Preferably make all connections isolated (cover the wires and soldering points, for instance with tape) before you plug in. Unless you know what you're doing: ALWAYS unplug the connector before soldering!!! It's quite realistic otherwise you blow up circuits in your computer (I know by experience from (garbage) computers).

The pin layout of the connector (view from the back of the male 25-pin MALE):

Pin 18-25 are all connected to the ground.

My experimenter plug looks like this when taken apart:

You'll have to install the driver file (on NT/2000/XP) from the parallel port page, and load the dll:

 load lpttcl

Assuming we have the LED connected up, via its current limiting resistor, we can drive it via the tcl commands:

 set output_bit 1;        # the bit line (number from lest significat = 0) you put the led on
 lpt_wrctrl 0;            # hopefully put the right printer port in output mode
 lpt_wrdata [expr 1 << $output_bit]


 lpt_wrdata 0

instead of the expr you can also simply use 255 (all bits 1)

To read the state of our switch, we read the status byte:

 puts [lpt_rdstat]

which should print a different value when you press or release the switch. The difference of the two values is the value of the bit you connected to. You can also make an 'if' with simply one of the two values as condition.

To get a continuous readout of the switch, you can do something likt this:

 entry .e -textvar e
 pack .e
 proc af {} {global e; set e [lpt_rdstat] ; after 100 af}

when you're done:

 rename af {}

Notice that when you feed back the state of the switch to the state of the LED, you can test whether the program that is supposed to do that (for instance by a repeated after command every 10th of a second) is still running, without having the need for an active screen.

Maybe I'll also put an example relay driver here, to for instance switch on the coffee machine by computer control.

ZLM is an electronics illiterate and finds this very interesting. How is the switch connected, is it a single wire? Without using any LEDs how many switches could be connected to a parallel port? Please post more things like this!

TV A switch always has two wires... One connects to the ground, the other via a small resistor to a select pin. The resistor is to limit the current when something goes wrong. Normally I guess there are 4 or 5 select lines, so 4 or five switches. Unless you'd make or use a more complicated circuit, then you could easily have hundreds of switches... Also note that connecting long (unshielded) wires to your port makes the signals sensitive to all kinds of electromagnetic fields, so they can get disturbed, and if you feed the printer port with some unknown signal, you can easily blow it up, and if you're unlucky your computer, too.... The given circuits should be fine, though.

I probably will!

Zarutian: TV, If one hasn't got a computer with an unused parallel port, could one use USB-MOD2 instead? ( and )

UZ (22oct06): lpttcl allows low-level access to an IEEE-1284 parallel port, and USB accessories that offer an additional parallel ports do not work with it. USB-MOD2 is based on an FTDI device; to use its pins as a parallel port, an extension should be made to allow access to the FTDI DLL [L1 ]. This would be an interesting extension as in principle it could be made available for all major platforms (Win98, Win2k/XP, Linux, Mac OS X).

PT 17-Mar-2008: I've recently written an FTDI extension [L2 ] to wrap the FTD2XX dll and provide Tcl channels to access USB devices that use this chip.

I'm making a page on a continuation of the little (for me that is...) project, where you can make a LED display driven by the parallel port under tcl control.

See also: