Modifications ideas for web interface

Modifications ideas for web interface

I have been asked how to control other pins on parallel port than just the data pins. Here are some instructions how to modify the web interface source code to do that.

The portcontrol program that does the actual port contolling stuff already supports controlling many other things than just the parallel port data pins. The PHP example code control.php just does not support those features. If you want to use those features, you need to modify the control.php source code and change the port definition on it. If you want to control both data pins and other pins, then it might be a good idea to make a copy of control.php to another name and make modifications to it.

To make the changes open the port controlling PHP code, and modify the following lines:

In Windows system:

return " Time: " . date("M dS, Y, H:i:s  ") . "Status: " . shell_exec("portcontrol.exe LPT1DATA read print bin");
shell_exec("portcontrol.exe LPT1DATA read setbit " . $x . " write");
shell_exec("portcontrol.exe LPT1DATA read resetbit " . $x . " write");

In Linux system:

return " Time: " . date("M dS, Y, H:i:s  ") . "Status: " . shell_exec("/usr/local/sbin/portcontrol LPT1DATA read print bin");
shell_exec("/usr/local/sbin/portcontrol LPT1DATA read setbit " . $x . " write");
shell_exec("/usr/local/sbin/portcontrol LPT1DATA read resetbit " . $x . " write");

By changing the LPT1DATA to one of supported port identifier, the controlling will be done to that port:

 * Supported port identifiers
 *  NONE

For controlling parallel port pins LPT1STATUS and LPT1HANDSHAKE are the ports to use.

Controlling some real world electronics

The idea of the interface shown above can be expanded to control some external electronics by simply adding a buffer circuit to the parallel port. The programming can be done in exactly the same way as told in my examples.

Building your own relay controlling circuits

The following circuit is the simples interface you can use to control relay from parallel port:

                              |    __|__
                            Relay   /^\  Diode 1N4002
                             Coil  /---\
                              |      |
                           | / 
                 4.7K    B |/  C
parallel port >-\/\/\/\/---|        NPN Transistor: BC547A or 2N2222A
data pi                    |\  E
                           | V
parallel port >--------------+
ground pin                   |

The circuit can handle relays which take currents up to 100 mA and operate at 24V or less. The circuit need external power supply which has the output voltage which is right for controlling the relay (5..24V depending on relay). The transistor does the switching of current and the diode prevent spikes from the relay coil form damaging your computer (if you leave the diode out, then the transistor and your computer can be damaged).

Since coils (solenoids and relay coils) have a large amount of inductance, when they are released (when the current is cut off) they generate a very large voltage spike. Most designs have a diode or crowbar circuit designed to block that voltage spike from hitting the rest of the circuit. If that diode is bad, then the voltage spike might be destroying your “sink” transistor or even your I/O card over a period of time. The mode of failure for the sink transistor might be short circuit, and consequently you would have the solenoid tap shorted to ground indefinitely.

The circuit can be also used for controlling other small loads like powerful LEDS, lamps and small DC motors. Keep in mind that those devices you plan to control directly from the transistor must take less than 100 mA current.

WARNING: Check and double check the circuit before connecting it to your PC. Using wrong type or damaged components can cause you paralllel port get damaged. Mistakes in making the circuit can result that you damage your parallel port and need to buy a new multi-io card. The 1N4002 diode in parallel with the relay is an essential protection component and it should not be left out in acu case, or a damage of the parallel port can occur because of high voltage inductive kickback from the relay coil (that diode stops that spike from occuring),

Safer new design

The circuit example above works well and when transistor is of correct type and working properly. If for some reason B and C sould be shorted together and you are suing more than +5V in the relay side, the circuit can push that higher voltage to the parallel port to damage it. The following circuit uses two 1N4148 diodes to protect parallel port against higher than +5V signals and also against wrong polarity signals (power on the circuit is accidentally at wrong polarity.

                              |    __|__
                            Relay   /^\  Diode 1N4002
                             Coil  /---\
                              |      |
          Diode            | /
          1N4148  4.7K   B |/  C
parallel  >-|>|-+--\/\/\/--|        NPN Transistor: BC547A or 2N2222A
port data       |          |\  E
pin             +-|<|-+      | V
            1N4148    |      |
parallel  >-----------+------+
port ground                  |

Adding even more safety idea: Repalce the 1N4148 diode connected to ground with 5.1V zener diode. That diode will then protect against overvoltage spikes and negative voltage at the same time.

Bad circuit example

I don’t know WHY I see newbies who don’t THINK electronics very well yet always putting the relay “AFTER” the transistor, as if that was something important. Well it’s NOT, and in fact its a BAD PRACTICE if you want the parallel port to work well! This type of bad circuit designs have been posted to the usenet electronics newsgroups very often. The following circuit is example of this type of bad circuit design (do not try to build it):

                           | /
                 4.7K    B |/  C
parallel port---\/\/\/\/---|        NPN Transistor: BC547A or 2N2222A
                           |\  E
                           | V
                             |    __|__
                           Relay   /^\  Diode 1N4002
                           Coil   /---\
                             |      |

NOTE: This is a bad design. Do not build or use this circuit.

The problem of the circuit is that the voltage which goes to the relay is always limited to less than 4.5V even if you use higher Vcc supply. The circuit acts like an emitter follower, which causes that the voltage on the emitter is always at value base voltage – base to emitter voltage (0.6..0.7V). This means that with maximum of 5.1V control voltage you will get maximum of 4.5V out no matter what is the supply voltage (when it higher than 5V and below transistor breakdown voltage).

Other problem is that in some cases this type of circuit can start to oscillate if the base resistor is right on the edge.

Basic circuit with optoisolation

One of the simples optoisolated output circuit for parallel port is the following 4N33 based circuit:

4N33 based parallel port optoisolator

The 4N33 optocouplet device has a Darlington output transistor that is capable of driving up to 30 mA of load safely. The maximum voltage on the output side is 30V. The input to output isolation can handle up to 1500V voltage. You connect the input side + to the parallel port output pin you want to use for the controlling. Then you connect the input – side to parallel port ground pin. The output side is connected to the circuit to be controlled at right polarity. This example cirucit used 1 kohm resistor to limit the control current current (circuit should also work well with 470 ohm resistor). Because the current fed to the optocoupler is very low (just few mA), the guaranteed outptu current available from the optocoupler is low. You can expect to get something like 10 mA of drive capacity on output (maybe more if you are lucky to have a coupler with high CTR and parallel port with high output current). The circuit can be built also using 4N32 optocoupler that is very similar to 4N33.

4N33 component data:

  • The 4N32 and 4N33 are optically coupled isolators with a gallium arsenide infrared LED and a solicon photodarlington sensor.
  • Switching can be achieved while maintaining a high degree of isolation between driving and load circuits.
  • Very high current transfer ratio, 500 % Min.
  • High isolation resistance
  • Forward continuous current maximum 60 mA
  • Output Collector-emitter breakdown voltage 30V
  • Output can easily drive 50 mA current
  • Output Power dissipation maximum 150 mW
  • Isolation test voltage 5300V

The component data is taken from datasheet available at These optocouplers can be used to replace reed and mercury relays with advantages of long life, high speed switching and elimination of magnetic fields.

Transistor amplified optocoupler circuit

If you want to have a very good protection and of the parallel port and more drive capacity you might consider optoisolation using the following type of circuit:

                                                V+  (12V)
                                    |            +------+
Parallel                            |            |      |
Port                                |    D1     ---     |
                                    |   1N4001  / \    Relay coil
          R1    1  ----------- 5    |          /---\    |
 D(x) ----1k------| Opto-     |-----+            |      |
                  | Isolator  |                  +------+
 GND -------------|           |-+                |
                2  ----------- 4|                |
                   CNY 17 or    |   R2        | /
                   4N25         |   4.7K    B |/  C   T1
                                +--\/\/\/\/---|      BC547A or 2N2222A
                                              |\  E
                                              | V
                                          external circuit ground

Typical optoisolator pinout (CNY 17 and 4N25):

 1--|----          |------------|--6
    |   |          |            |
    | \---/ \      |    ------  |
    |  \ /  \     | | / C   |  |
    |  --- \  \|   | |/     |  |
    |   |   \ --   --|       ---|--5
    |   |    \|    B |\         |
 2--|----    --      | V E      |
    |                   --------|--4
 3--|--NC                       |

The opto-isolator is there to protect the port. Note that there are no connections between the port’s electrical contacts. The circuit is powered from external power supply which is not connected to PC if there is no need for that. This arrangement prevents any currents on the external circuits from damaging the parallel port.

The opto-isolator’s input is a light emitting diode.R1 is used to limit the current when the output from the port is on. That 1kohm resistor limits the current to around 3 mA, which is well sufficent for that output transitor driving.

The output side of the opto-isolator is just like a transistor, with the collector at the top of the circuit and the emitter at the bottom. When the output is turned on (by the input light from the internal LED in the opto-coupler), current flows through the resistor and into the transistor, turning it on. This allows current to flow into the relay The output current from the optoisolator with the input current listed above should be around 1-3 mA range (depends on exact optisolator type and component variations). This current goes through R2 to the transisto base.

Turning the input on the parallel port off causes the output of the opto-isolator to turn off, so no current flows through it into the transistor and the transistor turns off. When transistor is off no current flows into the relay, so it switches off. The diode provides an outlet for the energy stored in the coil, preventing the relay from backfeeding the circuit in an undesired manner.

The transistor in the circuit can be used for controlling output loads to maximum of around 100 mA (depends somewhat on components and operation voltage used). The external power supply can be in 5V to 24V range. When you use a relay that takes less than this 100 mA of current and works at the power supply you use, you should be OK. The output load that you can control with the circuit with a relay only depends on the relay output contact ratings (maximum current and maximum voltage).

The circuit ban be used also directly to control small loads (less than 100 mA current). Just put the load you want to control in place of the relay coil.

Some component data on the components used:

  • 2N2222A: NPN transistor, T018 case, Vce=40V, Vcb=75V, Ic=800mA, Hfe=100-300, 300MHz, 500mW
  • BC547A: NPN transistor on TO92 case, Vce=45V, Vcb=50V, Ic=100mA, Hfe=110-800, 300MHz, 625mW
  • CNY17: Optocoupler with Phototransistor Output, CTR from 40% to 200% depending on version, 4400 Vdc isolation
  • 4N25: optocoupler with phototransistor output, CTR typically 50% (20% minimum), 2500V isolation, input forward current max 80 mA, 30V max output voltage

Optoisolated high power control circuit

Here is a higher power version of the circuit described above:

                                                V+  (12V)
                                    |                  |      |
Parallel                            |                  |      |
Port                                |          D1     ---     |
                                    |         1N4001  / \    Relay coil
          R1     1 ----------- 5    |                /---\    |
 D(x) ----1k------| Opto-     |-----+                  |      |
                  | Isolator  |                  +-----+------+
 GND -------------|           |-+                |            |
                 2 ----------- 4|                |            |
                   CNY 17 or    |   R2        | /             |
                   4N25         |   4.7K    B |/  C T1        |
                                +--\/\/\/\/---|     BC547A    |
                                |             |\  E           |
                                |             | V          | /
                                /               |        B |/C  T2
                                \  R3           +----------|    power
                                / 10 kohm                  |\E  transistor
                                \                          | v
                                |                            |
                                                external circuit ground

In this circuit Q1 is used for controlling the base current of Q2 which controls the actual current. You can select almost any general purpose power transistor for this circuit which matches your current and voltage controlling needs. Some example alternatives are for example TIP41C (6A 100V) or 2N3055 (100V 15A). Depending your amplification facter inherint to the transitor Q2 you might not hough be able to use the full current capability of the output device T2 before there will be excessive losses (heating) in that transistor.

This circuit is basically very simple modification of the original optoisolator circuit with one transistor. The difference in this circuit is that here T2 controls the load current and Q1 acts as a current amplifier for T2 base control current. Optoisolator, R1, R2, Q1, D1 work exactly in the same way as in one transistor circuit described eariler in this documents. R3 acts like an extra resistor which guarantees that T2 does not conduct when there is no signal fed to the optoisolator (small possible current leaking on optosiolator output does not make T1 and T2 to conduct).

Mains power controlling with parallel port

It is possible to control mains voltage through parallel port with a suitable circuit. When controlling mains voltage, you need to be varycarefyl and know what you do to do it safely. Mains voltage can kill if you get in touch with it, and bad mains controlling circuit can burn down your house.

First idea for controlling mains power is to use one of the circuit above to control a relay that then controls the mains power. This suits for many applications as long as the relay is rated for the mains power switching applications and for the current rating of your applications. The relay contact is used to switch the phase/live wire going to the equiment. A properly designed circuit should have in addition to the relay (plus parallel port interface circuit) also a peoprly sized fuse that will cut the power going through the relay in case of short circuit or overload at the equipment being controlled. The fuse here is used to protect the relay against overload. A relay will work on applications where the device is turned on and off quire rarely. If you are switchign the device on and off often, the normal relay will siffer of limited mechanical and electrical age, and in some applications also on noise caused by sparks that are formed when relay contacts open and close. Those sparks can cause radio frequncy noise.

Another component suitable for mains voltage controlling is a solid state relay. The circuit show below describes how to control a solid state relay from PC parallel port. The solid state relay controls the mains voltage.

Mains power controlling with solid state relay

The relay for this application should be sone rated for the mains voltage you used and the current your controlled equipment (marked with L on the picture) takes. The solid state relays designed for mains operation provide the needed isolation between the control input and mains side. The solid state relay should be used according the manufacturer application notes and your local electrical equipment codes. You should keep the mains side and low voltage side isolated in all cases (even on equipment damage case). You should also put a properly rated fuse in series with the solid state relay so protect the relay against overload. A proper size fuse will not protect the solid state relay against overheating of the load tries to take too much current through the relay. The fuse might not ne able to protect the relay agains short circuit damages (if you short-circuit the load, you generally loose the solid state relay and the fuse).

Many solid state relays can be controlled directly to parallel port without extra components. You need to select a solid state relay that is voltage controlled and the control voltage range can take the voltage that printer port outputs (5V or somewhat less). For reliable operation you should select a relay that can operate at down to 3V input voltages and does not take too much control current (a SSR that takes only few milliampreres is preferred because current output capacity of parallel port is usually limited to that). To gurarantee that the operation is reliable with the direct connection, be sure to measure that the control vontage entering the SSR is within the specified operating range when the relay is controlled to parallel port (you can measure this without mains power applied to the rest of circuit, safer o measure in this way). Controlling a solid state relay with lower than specified control voltage can lead unreliable operation of solid state relay, and can even cause some solid state relays fail when heavily loaded!

It is also possible to build the mains voltage controlling part from discrete components. Here are two example cirucuits:

110V AC controlling

220V AC controlling

Those are just as an example. I do not recommend you to build those circuits. Nowadays the solid state relays are available at reasonable prices, and with them it is easier to build safe controlling circuits.

One very important thing to note on mains controlling circuits is that they should be built very carefully and right. Mains voltage can kill if you come in touch with it. A baddly constructed circuit can overheat and cause fire. Any mains controlling circuit should be built in such way that there is an overcurrent protection component that protects the circuit against overloads (usually a fuse in the power input). The circuit must be built into a safe and mechanically stable case. An insulating plastic case (electronics case made from plastic that does not burn easily) is one option. Another option is to built the circuit into a gounded metal case. The exact practices how to build safe mains powered circuits is outside the scope of this article. You should know those details before attempting to built any ciruit that connects directly to mains power.

Compact 8 channel output driver

You can build a circuit with many outputs by conbiming many individual transistor based circuits. If you want to hve a compact construction with up to 8 outputs, I would recommend you to consider using ULN2803 IC that is manufactured by Allegro and several other manufacturers. Here is the pinput of this ULN203 IC:

ULN2803 pinout

ULN2803 is a 8-bit 50V 500mA TTL-input NPN darlington driver. Featuring continuous load current ratings to 500 mA for each of the drivers, the ULN2803A high-current Darlington array is ideally suited for interfacing between low-level logic circuitry and multiple peripheral power loads. Typical loads include relays, solenoids, stepping motors, magnetic print hammers, multiplexed LED and incandescent displays, and heaters. The drivers need no power supply; the VDD “common” pin is the common cathode of the eight integrated protection diodes. The data sheet for this IC can be found at and

The ULN2803 is connected between each of the eight ‘output’ lines of the printer port and the device it controls. The output ‘device’ can be as simple as a LED, a small motor, or a relay. The inputs on the left side of the IC are directly suitable to be connected to PC parallel port output lines. The outputs are open collector output (output gets grounded through transistor when corresponding input line goes to high state), so they are well suitable for controlling various loads powered through external power supply. The maximum controllable voltage is 50V and maximum current per channel is 500 mA. Outputs may be paralleled for higher load current capability. The input and output sides of the IC have the same common ground that must be connected also to the ULN2803 IC ground pin.

The “common” line is connected to a suitable overvoltage protection circuitry to prevent damage to the IC due to “back emf” when loads such as motors and relays switch on and off. This “common” line can be for example connected to the power supply line that supplies power to the relays. You can also use for example 30V zener connected to this line as protetion component (limits relay power supply to maximum less than 30V). Or you can connect a 12V zener rfom common to the relay power supply plus (limits spikes to power supply voltage plus 12V, do not use higher than 30V power supply).

Here is an example of control circuit that drives eight LEDs using ULN2803 IC:

Driving LEDs with ULN2803

This circuit can be also used to drive other kinds of loads, for example relays, small light bulbs etc. Just replace the LED plus resistor combination with the load you want (as long as the load is within the capabilities of the ULN2803 output drive capacity). You can use for example 15V zener diode for this ciruit.

Other examples of circuits implemented with ULN2803 can be found at and

Links to parallel port output circuits

Circuit diagram links

  • Connecting the ULN2803 to the Printer Port
  • PARALLEL PORT RELAY BOARD – There are 8 relays each capable of switching 12VDC/10A or 240VAC/5A. This is sold as a kit with full details on the documentation. This page gives you control software for download.
  • Parallel Port Relay Interface – three examples of controlling a relay from the PC’s parallel printer port
  • The Simplified I/O Interface – How it Works – In this application of the ULN2803 IC, small currents available at the Printer Port are used to control devices that could not be connected directly to the Port, such as motors and relays.
  • Stepper Motor Controller Connection Diagrams
  • Relay control – The question of how to connect and control a relay from the parallel port came up. Here’s how you can do it, even if you don’t know a lot about electronics!
  • Simplified Output Interface – is a simple and inexpensive output control interface for the Centronics printer port. Final cost per interface is less than $30. The integrated circuit (IC) used is an eight line “Darlington Driver” coded ULN2803. The digital state of each of the eight lines from the printer port is used to control the IC’s internal “drivers” which in turn control the LEDs. Power for the LEDs is provided by the external battery.
  • Four-Way I/O is a simple and inexpensive 4-way input/output interface for the Centronics printer port. The output section is controlled by an eight line “Darlington Driver” integrated circuit (IC) called a ULN2803. The digital state of each of the eight lines from the printer port is used to control the IC’s internal “drivers” which in turn control the Relays and LEDs. Power for the Relays and LEDs is provided by the 12 volt regulator (LM 7812) via a plug pack power supply.
  • The 8-Way Relay Board controls 8 relays using ULN2803 IC.
  • A Single-Channel Control Line is a very simple relay controlling board that controls to parallel port. The aim was to develop a circuit simple enough to be manufactured ‘from the ground up’ on the first day of the weekend and then spend the second day developing software to run it. This circuit is based on one transistor controlling a relay.
  • Parallel Port Relay Interface gives examples of controlling a relay from the PC’s parallel printer port (LPT1 or LPT2). There are examples for a solid state relay and normal relay controlling. Also parallel port reading is covered.

Programming I/O



Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s

%d bloggers like this: