PC Tutorial: Parallel Port Interfacing Techniques - Part 2

In this tutorial we will flash a LED (Light Emitting Diode) connected to your printer port socket. Reading the first partis recommended to get a general idea what you are upto.

Connections

To connect a LED to your port first you have to know what it looks like. Here are the two most common connectors that you can have.

D-Type 25 Pin connector

​

Pin Layout:

​

​

36 Pin Parallel Centronics

​

Pin Layout:

​

​

The LED

Shown below is a very common LED found anywhere from toys to computers. To connect, insert the positive pin into Pin number 2 of your port. And the negative to any ground of your connector. Pin 19 is an easy reach.

​

In the above image the longer pin is positive and the shorter one is negative (ground). If you have a LED looking like the left one then you have to find out yourself which one is +/- by connecting it to a battery. I would recommend a 220ohm or 470ohm resistor in series with the LED. Mine worked without the resistor but I cannot promise anyone!

Open the executable; you should see a blue screen with port addresses, pins and their description. Choose the address where you have connected the port wire, and click on PIN 2 (Data 0) (there should be a cross on it now). Look at the LED, it must glow. If it does not glow then try different Port addresses, LPT1, LPT2 etc.

Programming the Parallel Port in C, C++, VB or Java with ‘Windows the great XP’ is not easy since it does not allow you to access the Ports directly. All hardware is virtualised. I somehow have a feeling programming in VB is easier. Very basic LED flashing is also easy through Assembly Language. I’ll look into this problem and post a tutorial soon.

Dont hesitate, ask me if you have trouble with anything!!! Writing a tutorial from scratch is not easy so please don’t mind random grammar mistakes 😀

Hay thanks biggie. I too am waiting for the upgrades and some attachment ability (if you all think its necessary). In this perticular tutorial, I took the working pics in the bottom, the remaining are from the linked sites, and since this was a very simple one it did not need any major schematics. I will be writing a tutorial to program in some languge for more flexability, there I will be clear on the diagrams and pictures.

I want ot see people get interested by reading this tutorial, and I hope I did that. If there can be any changes to improve it, anyones views are welcome! 😁

This is just the begining, we can in a year come up with a full home automation tutorial and project 😛

xheavenly, would it be possible for you to make a brief post about USB interfacing if you have time. Just the basics!!. Just like your posts about parallel port interfacing would be good.

And probably turning on and off an LED using the USB port if thats possible 😉

and few other things...

is there any difference between RS232, the parallel port and the serial port?

1. Well USB is a bit difficult than parallel or serial port. And moreover I have never worked with USB. Their programming in windows is shrouded with crappy permition protocols and timing stuff. wind00z XP/Vista is really the worst OS you can possibly use for programing hardware. Its all virtualised.

Well, there are many differences but technically parallel is parallel communication. That is, at the same time you can have more than one output/input working. Usually there are 8 inps and 8 outps depending on your BIOS settings and hardware. They are:

RS-232 IS serial port. It has serial communication. That is, one bit after the other in a single wire. It has a longer range too, meaning long wires will not spoil the data. Check this page for [SIZE=-1]Interfacing the Serial Port - Parts 1 and 2.[/SIZE]

The USB is a whole other story. Here is line from Beyondlogic.org.

" Starting out new with USB can be quite daunting. With the USB 2.0 specification at 650 pages one could easily be put off just by the sheer size of the standard. This is only the beginning of a long list of associated standards for USB. There are USB Class Standards such as the HID Class Specification which details the common operation of devices (keyboards, mice etc) falling under the HID (Human Interface Devices) Class - only another 97 pages. If you are designing a USB Host, then you have three Host Controller Interface Standards to choose from. None of these are detailed in the USB 2.0 Spec."

Oh, I should have posted here instead of Part 1 of the tutorial 😛 great work xheavenlyx. Wanted to ask a few things:

Is it safe to use the parallel port on a laptop for experiments? eg, lighting LED's.

Can you give a sample C code on how to light an LED on and off? Like what libraries to use. At least I know it involves a while loop and a delay 😛 I believe you have to use this file called Inpout32.dll, am I right?

Can you also recommend me a program to use for Assembly Language programming?

1. No its not really safe to directly use a parallel port of a laptop. You CAN try an LED or something, because the grounds can sink that much current from your port. However, dont try using motors directly. The links I have provided are VERY useful and have many circuits too. Read carefully. I cant cover that here, since everyone has their own design.

2. god, forgive me, I actually stoped programming PP in newer PC because of that problem (Windooz..input32.dll) but if you search you will get a LOT of info. As for the libraries, not many are needed. In win98 it were stdio.h and io.h or something. Very few. Here is a sample code I wrote 4 years ago (!):

//This example code shows how to switch a LED on/off in Visual C++.
//We use two variables N and L explained below.
//I am assuming you have some knowledge in C++. Instruction to connect the LED is given in the Tutorial.
#include <iostream.h> //The most important include to display data on screen.
#include <conio.h> //Include for outp and inp functions for the printer port.
#include <windows.h> //For _sleep functions for our time delay.
#define addr 0x3BC //IMPORTANT: Normaly 378 is the most common address,
//if not then insert your address instead of 378
int main()
{
int freq; //Variable for time delay.
int L; //Variable for counts of Blinking LED.
cout<<"\t\t XheavenlyX WELCOMES YOU!!! \n\n"; //How can I forget this ^_^
cout<<"How many times to blink?? Enter 0 to exit: "; //Ask user how many times to blink the LED
cin>>L; //And exit if zero is entered.
if (L==0)
{
cout<<endl<<"Exiting application..."<<endl; //Code to exit if L = 0.
for (int i=0;i<=5;i++) {_sleep(500);} //A lame pause before exit!
exit(0);
}
cout<<"Frequency (in mS): "; //Ask user to enter the frequency at which the LED will blink.
cin>>freq; //Store that value in freq.
//Bottom code is the Juice! This will blink your LED the number of times you put in L.
//The speed of blinks will depend on freq. As an example if you entered 500milliseconds. The LED
//will blink twice every second since 500ms is half of 1000ms (1000millisecondss = 1sec)
for (int i=1;i<=L;i++) //The loop. L times to blink.
{
_outp(addr,1); //Output is high in the first 'Data bit' of port(*), i.e Pin number 2 where you have connected the LED.
_sleep(freq); //freq mS delay between the high and low of LED.
_outp(addr,0); //Output is low now
_sleep(freq); //Pause again between the high,
}
return 0; //main returns nothing to you. ^_^
} // For an indepth look at
//(*)NOTE: There are some simple calculations to turn on/off the other PINs on the printer port.
//links
// https://msdn2.microsoft.com/en-us/library/733szwah.aspx _outp, _inp etc.

3. Assembly learning is somewhat a smooth ride if you stick with it. And it can be very very helpful. Speacially if you are planning on optimizing complex control instructions. It can be for Microcontrollers or your PP programming. That too, youll have to search...😀

Hi..good day
I would like to say thank you for posting those stuffs about parallel ports. It helped me a lot. I am very much interested with that tutorial. Thank God there are people like you. As for now, i want to make a project using LEDs to connect in parallel port. The LEDs should turn off/on upon user input. The whole thing is to be controlled by an Assembly language program or a C++ program that has an inline funtions of Assembly language. The problem is, I got very little Knowledge in assembly language. From your tutorial,i already leared how to connect the LEDs in the parallel port. Thanks to you. I hope you can help me.

Aside from LEDs, i was also pkanning to use the stepper motor. Same thing will happen,it will be connected thru the Pport. Even a single or continous rotation operation of the motor may do. Still an Assembly language program is needed here.

Thank you for telling me that this page was really important to you! and hope to help in anyway can.

Firstly, I have to confess that I haven't tested PP interfacing on new OS! Like Wondows XP and Vista. Thats because in these new OS, windows does now allow direct hardware access. You have to access it through another dll, read the above posts. And my laptop has no ParallelPort! Now there are 2 things you can do.

1. go to these sites I have given above and here. and try to implement it in XP. When I get time to do this, Ill surely let you know. And if you have an old pc with windows 98 then better.

2. I can give you the schematica of interfacing a motor, or stepper motor. And an idea of the algorithm, but remember you have to implement it yourself on your PC so, the only way to learn it is thru experimentation.

Remember, I cannot explain all the electroncis and programming involved, its impossible. So here is some start:

The above image shows how to interface a relay. 12V relay is good since this relay which in turn will control the switch of a simple DC motor. The diodes are very important, so as to reduce the voltage spike moving into your PP and destroying it.

Now dear bro, for stepper motor you have to research yourself. Presently I am on a college project so I have no time to do that. Go further in your project and tell me how its going and what you have done!! Thats the most important part. So try whatever you have learnt and tell us about it.

By the way @ash, can you tell me if you have tried interfacing on XP and what were the results, please try to do so this week if you can, it will be really helpful.

Ah, I haven't tried yet, was also busy with projects. But I'll try by the end of this week hopefully! My roommates had interfaced with it on a desktop PC with Windows XP, lighting up a mini traffic light system. I'll get them to teach me a bit.

@ abyss and @ heavenly..
Hello friends, i am also looking to run a stepper motor using the parallel port.. i have bit idea of stepper motor operation . i am also finding difficulties in outout on parallel port as i am having winXP .. can win98 solve this problem??? and what to do if i want to run the same program in winXP???

@ abyss and @ xheavenlyx..
Hello friends, i am also looking to run a stepper motor using the parallel port.. i have bit idea of stepper motor operation . i am also finding difficulties in outout on parallel port as i am having winXP .. can win98 solve this problem??? and what to do if i want to run the same program in winXP???

Do everything I have given above in the tutorial plus the following posts and experiment. Then tell us what you have done and what were the results and how you have tried it, what was your code in which language, and what was your circuit? everything like this.

THEN you can post your problem here, and we will try our best to ans you.

And yes win98 is easier to program then XP. (How its done is given above somewhere)

Computer programming was one of the great advancement in technology. It contributed a lot to help humanity. Most electronic devices that we use today are endowed with programming.

Implementation of computer programs with hardware is quite an arduous task. The computer program comes in various languages. The proper programming language must be noted. Furthermore, the bus line should be appropriate in order for the hardware to run effectively. Hence, the language to use should be relative with the bus line.

Assembly language program was found to be the outstanding language when it comes to hardware programming. It provides an easy data communication between the program and the hardware. It shows an actual data representation which can be use to drive the hardware.

In harmony, the parallel port was one of the suitable line of communication between the program and hardware. As a bus line, it allows a direct link amid the instruction set and the data output.

In this project, I introduce the PC Parallel Port and how to identify its address for use in home electronic projects. An assembly language program was used to drive the hardware. The hardware deals with LEDs (Light Emitting Diode). It includes simple LEDs and a Seven-segment Display. The program contains instruction that can turn the LEDs ON and OFF. The program also receives values that will be use to drive the seven-segment display. The program uses an A86 macro assembler, D86 debugger V4.04.

By implementing this project, one can have a better understanding about Hardware Programming.

The Parallel Port is the most commonly used port for interfacing home made projects. This port will allow the input of up to 9 bits or the output of 12 bits at any one given time, thus requiring minimal external circuitry to implement many simpler tasks. The port is composed of 4 control lines, 5 status lines and 8 data lines. It's found commonly on the back of your PC as a D-Type 25 Pin female connector. There may also be a D-Type 25 pin male connector.

The lines in DB25 connector are divided in to three groups, they are:

Data lines

Control lines

Status lines

As the name refers, data is transferred over data lines, Control lines are used to control the peripheral and of course, the peripheral returns status signals back computer through Status lines. These lines are connected to Data, Control and Status registers internally. The details of parallel port signal lines are given in Figure1.

The Parallel Port has three commonly used base addresses. These are listed in table 2, below. The 3BCh base address was originally introduced used for Parallel Ports on early Video Cards. This address then disappeared for a while, when Parallel Ports were later removed from Video Cards. They has now reappeared as an option for Parallel Ports integrated onto motherboards, upon which their configuration can be changed using BIOS. LPT1 is normally assigned base address 378h, while LPT2 is assigned 278h. However this may not always be the case as explained later. 378h & 278h have always been commonly used for Parallel Ports. The lower case h denotes that it is in hexadecimal. These addresses may change from machine to machine.

Address​

Notes:​

3BCh - 3BFh​

Used for Parallel Ports which were incorporated on to Video Cards​

378h - 37Fh​

Usual Address For LPT 1​

278h - 27Fh​

Usual Address For LPT 2​

Accessing Ports: Writing to the PortTo write to the eight data registers we can pass a value of 0 - 255 at the parallel port address. In Binary every bit of that number controls one or more output bit. Table 4 illustrates them.

DB25-Pin​

2​

3​

4​

5​

6​

7​

8​

9​

Data Pin​

0​

1​

2​

3​

4​

5​

6​

7​

Value​

1​

2​

4​

8​

16​

32​

64​

128​

Table 3: Data Pins Value​

For example to set pins 2 and 3 to logic 1 then output value would be: 1+2=3. Similarly to set on pins 3, 5 and 6 then output value would be: 2+8+16=26. In this way value for any bit combination can be calculated. Also it should be noted that:

Value​

Description​

0​

Set all data Pins to logic level Zero​

255​

Set all data Pins to logic level One​

Table 4: Particular Value​

Schematic Diagram

The Program Code

The following Assemble Code writes 255b to the port using Assembly’s OUT mnemonic. The OUT accepts 2 operands: DX and AL. it writes the data from the AL (Accumulator Register) to the port Address stored in DX (Data Register).

A86 (with its 32-bit version A386) is the finest assembler available, at any cost under any terms, for the Intel 86-family of microprocessors (IBM-PC, compatibles, and not-so-compatibles). A86 accepts assembly language source files, and transforms them directly into either: (1) .COM files executable under DOS (or in a DOS box under Windows), starting at offset 0100 within a code segment; (2) .OBJ files suitable for feeding to a linker to create EXE files; or (3) object files starting at offset 0, suitable for copying to ROMs. A86 is a full featured, professional-quality program. I designed A86 to be as closely compatible to the standard Intel/IBM assembly language as possible, given that I insisted upon making design and language enhancements necessary to make A86 the best possible assembler.D86 (with its 32-bit version D386) is a screen-oriented program that allows you to troubleshoot faulty computer programs written to run under DOS (or in a DOS box under Windows). It "freezes" the state of your program, and allows you to investigate the values of registers, flags, and memory. You can monitor your program's execution by stepping it one instruction or procedure at a time; or you can start your program running, telling D86 to stop it when it reaches certain locations. D86 recognizes the symbol-table output of the A86 assembler, creating a symbolic disassembly of your A86 program, and allowing you to refer to locations and variables by name.OPERATION

T Assemble the Hardware to the breadboard and connect the circuit to the designated data pins. T Plug the Printer cable to the parallel port.T Make sure that the Assembler is in the location: C:\ASMT Run the CMD.exe.

T On the cmd, type in cd\. Then press enter. Next, type in cd asm then press enter.

T Assemble the file 255.asm, off.asm, LED.asm and segment.asm respectively. Use these syntax:asm LED.asmT Run each program by just typing the filename.T Use the table 4 for desired outputs.

But I thing there is problem, even debug or assembly compilers do not work easily in XP and Vista, direct hardware access is not allowed. And if this is taken from some website then dont forget to post the actual website address, since some diagrams are missing, and I want to go through them.

Actually I have a current project where I need some simple digital IO from a PC.

My question is if it would be possible to use the parallel port for continous input and output at the same time.

Example:
1: Some program state tells me I need an automatic door to be opened.
2: I'll set a one bit output to be high so I trigger a switch for the door.
3: This switch is connected to a set of sensors determining if it's ok to open door. There are some security issues.
4: If it is not ok a light at the door will show that manual actions need to be done to allow the door to be opened.
5: When it's ok a switch will send a input to the parallel port and the program will do some task that depends on the door beeing open.

So what i need is 1 bit output saying that i need the door to be opened and a 1 bit input to tell me if the door is open. But these has to be simultaniously available.

The cable will be short as the switches for both input and output will be placed next to the PC and the long wires will go from the switch to door circuits.

Can this be done with the parallel port or do i need to invest in a digital 16 channel PCI I/O board? (seems thats the minimum number of IO's on that sort of cards on the market.)

I would really like to be able to just use the parallel port as to not impose anything vendor specific on the project.

I found a turorial on the parallel port. It had the following include files which refused to work in my visual studio 98.dos.h problem with this is that it was claimed in the turtorial to have outportb function and delay both of which were absent._defs.hwhich was non existent and also hed problems with some of its code. they generated alot of errors.
i obtained both the header files in the net. they had alot of issues. anyone know what the problem is??????????
the actual tutorial broke the 20000 words limit and i couldn,t post it however here is a sample of one of the programs it had.

D. Simple Example

Refer to the figure titled Figure #3 - Typical Application showing a normally open push button switch being read on the BUSY input (Status Port, Bit 7) and an LED which is controlled by Bit 0 on the Data Port. A C language program which causes the LED to flash when the push-button is depressed appears below. Note that an output logic zero causes the LED to light.

Inserting image into post is simple. Just upload your image to popular image hosting sites [ ex. Welcome to Flickr - Photo Sharing ] and copy the complete url of the image [upto the file extension ]. You can do it by right-clicking on the image and selecting "Copy Image Location" option.

Then click on the button in post editor of CE and insert that URL. Your image will appear inside the post.