Click here to Skip to main content
Click here to Skip to main content

Control Electrical Appliances using PC

By , 30 May 2004
 

Sample Image - circuit

Introduction

Hi there, I was searching the web for a way to allow me to control electrical appliances using PC and I found it here. Here is a circuit (figure 1) for using the printer port of a PC, for control application using software and some interface hardware. The interface circuit along with the given software can be used with the printer port of any PC for controlling up to eight equipments.

The interface circuit shown in figure 1 is drawn for only one device, being controlled by D0 bit at pin 2 of the 25-pin parallel port. Identical circuits for the remaining data bits D1 through D7 (available at pins 3 through 9) have to be similarly wired. The use of opto-coupler ensures complete isolation of the PC from the relay driver circuitry.

Background

Parallel port is a simple and inexpensive tool for building computer controlled devices and projects. The simplicity and ease of programming makes parallel port popular in electronics hobbyist world. The parallel port is often used in computer controlled robots, Atmel/PIC programmers, home automation, ...etc... Here is a simple tutorial on parallel port interfacing and programming, with some examples.

Everybody knows what is parallel port, where it can be found, and for what it is being used. The primary use of parallel port is to connect printers to the computer and is specifically designed for this purpose. Thus it is often called as printer Port or Centronics port (this name came from a popular printer manufacturing company 'Centronics' which devised some standards for parallel port). You can see the parallel port connector in the rear panel of your PC. It is a 25 pin female (DB25) connector (to which printer is connected). On almost all the PCs only one parallel port is present, but you can add more by buying and inserting ISA/PCI parallel port cards.

Parallel port modes

The IEEE 1284 Standard which has been published in 1994 defines five modes of data transfer for parallel port. They are:

  1. Compatibility Mode
  2. Nibble Mode
  3. Byte Mode
  4. EPP
  5. ECP

The programs, circuits, and other information found in this tutorial are compatible to almost all types of parallel ports and can be used without any problems (Not tested, just because of confidence!).

Hardware

The pin outs of DB25 connector is shown in the picture below:

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

  1. Data lines (data bus)
  2. Control lines
  3. 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 to the computer through Status lines. These lines are connected to Data, Control And Status registers internally. The details of parallel port signal lines are given below:

Pin No (DB25) Signal name Direction Register - bit Inverted
1
nStrobe
Out
Control-0
Yes
2
Data0
In/Out
Data-0
No
3
Data1
In/Out
Data-1
No
4
Data2
In/Out
Data-2
No
5
Data3
In/Out
Data-3
No
6
Data4
In/Out
Data-4
No
7
Data5
In/Out
Data-5
No
8
Data6
In/Out
Data-6
No
9
Data7
In/Out
Data-7
No
10
nAck
In
Status-6
No
11
Busy
In
Status-7
Yes
12
Paper-Out
In
Status-5
No
13
Select
In
Status-4
No
14
Linefeed
Out
Control-1
Yes
15
nError
In
Status-3
No
16
nInitialize
Out
Control-2
No
17
nSelect-Printer
Out
Control-3
Yes
18-25
Ground
-
-
-

Parallel port registers

As you know, the Data, Control and Status lines are connected to there corresponding registers inside the computer. So, by manipulating these registers in program, one can easily read or write to parallel port with programming languages like 'C' and BASIC.

The registers found in a standard parallel port are:

  1. Data register
  2. Status register
  3. Control register

As their names specify, Data register is connected to Data lines, Control register is connected to Control lines and Status register is connected to Status lines. (Here the word connection does not mean that there is some physical connection between data/control/status lines. The registers are virtually connected to the corresponding lines.) So, whatever you write to these registers will appear in the corresponding lines as voltages. Of course, you can measure it with a multimeter. And whatever you give to Parallel port as voltages can be read from these registers (with some restrictions). For example, if we write '1' to Data register, the line Data0 will be driven to +5v. Just like this, we can programmatically turn on and off any of the Data lines and Control lines.

Where these registers are?

In an IBM PC, these registers are IO mapped and will have an unique address. We have to find these addresses to work with the parallel port. For a typical PC, the base address of LPT1 is 0x378 and of LPT2 is 0x278. The Data register resides at this base address, Status register at base address + 1 and the control register is at base address + 2. So, once we have the base address, we can calculate the address of each register in this manner. The table below shows the register addresses of LPT1 and LPT2.

Register LPT1 LPT2
Data register (baseaddress + 0) 0x378 0x278
Status register (baseaddress + 1) 0x379 0x279
Control register (baseaddress + 2) 0x37a 0x27a

Using the code

I will use Inpout32.dll (or hwinterface.ocx) for Win 98/NT/2000/XP. This DLL has the following features:

  1. Works seamless with all versions of Windows (Win 98, NT, 2000 and XP)
  2. Using a kernel mode driver embedded in the DLL
  3. No special software or driver installation required
  4. Driver will be automatically installed and configured automatically when the DLL is loaded
  5. No special APIs required, only two functions Inp32 and Out32.
  6. Can be easily used with VC++ and VB or C#.
val=axHwinterface1.InPort(888);
axHwinterface1.OutPort(888,(short)(val|2));

The above code reads the value from port 0x378 (888 decimal) and then OR with value 2. This code will send value 1 to D1.

Hint

For a value=0, all the outputs (D0-D7) are off. For value=1, D0 is ON, value=2 D1 is ON, value=4 D2 is ON, and so on. E.g., if value=29 (decimal) = 00011101 (binary) -> D0, D2, D3, D4 are ON and the rest are OFF.

Points of Interest

You can use voice recognition (or DTMF) to control your house by this method (Smart House).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Taha Amin
Team Leader http://www.linkdev.com/
Egypt Egypt
B/OSS.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalcontroll home appliances using pcmemberRohit47899-Feb-13 4:26 
please send me the pcb layout and programming of this project..
 
please snd it to <u>rohit.charg@gmail.com</u> Thumbs Up | :thumbsup:
Questionpcb layoutmemberMember 86325398-Feb-12 18:42 
Can i have pcb layout for this project just for reference? ASAP
Questionneed code in Assemblley Language to controll house applaiamcesmemberArshad17919-Dec-11 4:43 
plz mail it to
 
arshad_eng@yahoo.com
QuestionProblem with this projectmemberkamrul101-Oct-11 5:22 
Hello,

I see this project Control Electrical Appliances(<a href="http://www.codeproject.com/KB/cs/control_e_appliances.aspx">Control Electrical Appliances using PC</a>[<a href="http://www.codeproject.com/KB/cs/control_e_appliances.aspx" target="_blank" title="New Window">^</a>]) using my pc in here , when I run this project in my pc it given the following error

Warning     1     Could not find type 'AxHWINTERFACELib.AxHwinterface'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built.     0     0    

Warning     2     The variable 'axHwinterface1' is either undeclared or was never assigned.     C:\Documents and Settings\Md Kamrul Islam\My Documents\Downloads\Control_e_appliances\Control_e_appliances\Form1.cs     381     0    

 
Warning     4     Cannot load type library for reference "AxHWINTERFACELib". Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED))     Control_e_appliances

Warning     5     Cannot load type library for reference "HWINTERFACELib". Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED))     Control_e_appliances

Warning     6     The referenced component 'AxHWINTERFACELib' could not be found.

Warning     7     The referenced component 'HWINTERFACELib' could not be found.

Error     3     The type or namespace name 'AxHWINTERFACELib' could not be found (are you missing a using directive or an assembly reference?)     C:\Documents and Settings\Md Kamrul Islam\My Documents\Downloads\Control_e_appliances\Control_e_appliances\Form1.cs     16     11 Control_e_appliances

could you please give me a solution for this
QuestionParallel Port Problem Need Urgent HelpmemberSaddam Khan20-Sep-11 17:31 
Hello Bhaskar Sir,
I am really inspired by ur project and i appreciate ur efforts,
 
My Question in Detail:
I made two modules in VB regarding Inp and Out done programing, then i send the output to parallel port and i use inp function to get that value from sended port address, it seems my signals are reaching at the port. but i am still not confirming while they are reaching there or not, please inform me any simulator that detects signals in parallel port,
Secondly whenever i try to boot up my PC all Pins are on High, Even when i place One LED leg at controle Pin and One at Ground Still my LED Lights Up (Which should not be done)
 
Please show me a way that my LED only Lights Up on Certain Pins whom i give instructions from my Program otherwise these pins should be Off( not high as always).
 
I think there is nothing wrong from programing side but the problem is with hardware that is always active and lights up LED everytime before my instructions and also my if i send &H0 it also doesnot work and doesnot disable the datapins.
 
please help me urgently...
QuestionUnavailability of parallel portmemberMember 80132104-Aug-11 19:22 
Dear sir,
 
If a computer has no built in parallel port, then how to make this project?
 
Is RS232 will be helpful or any other easy available circuit is present?
Questionhow to make the codememberfirzan24-Jan-10 7:27 
dears, kindly send me the details of making the code to on and off the project by using ms dos
thanks/firzan email firzan12@emirates.net.ae
Questionhow to run code given by you for home appliance controlmemberkau_12322-Jan-10 22:52 
Smile | :) hi.. i working with project home appliances control system through voice recognisation so i download your code but not run so please tell me how to run that project and what code or dll require to run that project please tell me answer my email-id is itsme_kau13@rediffmail.com
please reply
Questionhow electrical appliances controlling through PC in javamemberkaveee20-Oct-09 4:29 
Hello..sir
how electrical appliances controlling through PC in java.using parallel port please sir help me..Confused | :confused:
AnswerRe: how electrical appliances controlling through PC in javamemberaruljo13-Dec-09 19:27 
how electrical appliances controlling through PC in c.using parallel port please sir help me.and we have laptop ,shall we use serial port reply me
Generalcode amemberRegie Ilan10-Aug-09 16:14 
hi sir good morning<< i want a code using c program language,, can you give it to me?// please contact me in my email ilan_21@yahoo.com
Laugh | :laugh:
GeneralRe: code amemberdharmrajgurjar23-Sep-09 21:38 
val=axHwinterface1.InPort(888);
axHwinterface1.OutPort(888,(short)(val|2));
Generalwant java source codememberpratik655410-Jul-09 7:18 
hi,sir.........
i want a java source code for controlling electrical appliances from mobile.....
plz send it to pratik6554@yahoo.co.in
GeneralError: An unhandled exception of type 'InvalidActiveXStateException' occurred in system.dllmemberMember 26463329-Mar-09 0:30 
Could anyone help me out.
Generalinput functionmemberorked mohamed11-Feb-09 21:53 
i want to know how can i declare axhwinterface ,when i write it the program made error in it
Generalhelp on PCBmemberashkan_20_2030-Oct-08 5:59 
hi to all!
I want to make this project but i can't! beacuse i can't make its PCB(Printed circuit board).
can someone help me?
thanx
Generaluse CmemberAnkit_avr22-Jun-08 22:28 
hi nice job man but can u provide me the code of this in c and also plz tell me howz its gonna run in my pc . i have win xp as my operating system .plz do reply as i m new in this field.
waiting for your reply...... Smile | :)
GeneralRe: use CmemberJulias Ceasor22-Feb-12 5:28 
The PC runs the program and sends the data to printer port. The data would be a voltage of +5V or below. This is enough to operate a 6V relay which is attached with the circuit. When the port receives high value, the relay will be operated and makes connection with AC hence the power goes to the electrical appliances attached with it. The AC power totally separate from printer port supply. Hence, no fear about damage in the system provided the safer circuit to avoid reverse voltage in the circuit.
 
Got it!
GeneralSoftwarememberrohitissharma4-Jun-08 10:13 
I have a software developed in C++. I have done coding on LPT port to control the eight electrical devices.
say swith ON/OFF by pressing the keys. even there is an option by which we set the automatic time to switch ON/off the devioces. I want to sell this software and want to get a good job
GeneralUse C++memberahmedghazi9-May-08 4:20 
hi
can you use c++ program >

 
ahmedghazi

GeneralPLZZ HELPmemberharshita maloo18-Apr-08 23:03 
plzz i want the C coding for controlling the home appliances using PC..can anyone plzz help me out???if u get the coding then plzz mail at---harshita_maloo@yahoo.co.in
 
plzzz it's urgent....
thanx in advance..
Question:) Can you help me plzmemberMo3azMM17-Apr-08 6:51 
?????? ?????
How r u Mr. Taher
I hope all fine
I have a project in my college that represent in your article
so, if you can help me from A to Z I will be thankful
and I'll contact to you in any way ( MSN, Mobile, Visit )
?? ???? ????
?? ???????
Generalrequesting for source codememberarjun patel6-Apr-08 19:33 
can someone has the code to control electrical appliances in VISUAL BASIC 6 using parallel port interfacing,plz send the code to this E-mail arjunpatelpapu@gmail.com.
plz i wanted the code very urgent.
Generalrequesting for source codememberarjun patel6-Apr-08 19:31 
can someone has the code to control electrical appliances in VISUAL BASIC 6 then plz send the code to this E-mail arjunpatelpapu@gmail.com.
plz i wanted the code very urgent.
GeneralDTMFmemberSaeed.39415-Mar-08 4:09 
Hi i have a question about dtmf,
i need to charge my credit by command on gsm modem (e.g *140*1#) and network return a answer
i cant do it with my modem , how can i do it and how can i get response after run this command.
QuestionControlling power devices using PC through parallel portmemberSukumarbk5-Mar-08 0:49 
Hi,
 
I am new to this forum. I am working on embedded systems. I have a question.
 
I can able to control the power device through parallel port, but when the power devices stopped working I should get the feedback message that It stopped working(whatever may be the problem, It is not required). I simply wanna display the messag on the PC(like feedback).
 
The components are centronics printer port, relay, bulb for testing.
 
Thanks,
Sukumar.
 
Sukumar
GeneralGOOD !!memberpaul Tuambini27-Feb-08 0:45 
I appreciated the circuit and modified to a complex one!!!!!!!!
 
However the source code was not been able to run, so i make my own..
 
But thanks for the circuit, It helps me too much to create a good project!! Smile | :) Big Grin | :-D
Generala good effortmemberahmed saleem24-Feb-08 20:36 
thhis project is a good effort for the students to under stand the input output and controling the i\o register's valuesRose | [Rose]
QuestionCode to program the parallel portmemberBanditito31-Dec-07 3:58 
Hello Taha
 
I had a llok into your article and it looks to be quite what I'm looking for. But a part of the code is protected by passwords and can't be extracted. Did you protect it by chance? Would you supply me with the open code?
Another question: Do you know how to read data with the parallel interface as well?
I want to use this interface to control my toy train, I'm familiar with the electronics but I'm not experienced in programming C# and HW interfaces.
 
Regards, ALex
GeneralRe: Code to program the parallel portmemberTaha Amin31-Dec-07 4:31 
hello,
no it's not encrypted just register @ codeproject.
use microcontroller like pic.
 
check this "pic follow robot"[^]
Regards
Taha Amin
GeneralRe: Code to program the parallel portmemberBanditito31-Dec-07 4:48 
Wow, that's quick. No, I'm registered at CodeProject, downloaded the two files but when I tried to unzip inpout32_Source_and_bins I'm asked for a password to unzip certain files (the first one is cm_files). Of course I respect if you do not want to make it visible, but I'd be curious and hope to learn.
 
regards, ALex
GeneralRe: Code to program the parallel portmemberTaha Amin31-Dec-07 7:00 
it is not protected it seems a problem in your zipping software Smile | :)
NewsRe: Code to program the parallel portmemberpatricks emmy16-Oct-09 6:38 
can any one suggest me the code to cotrol electrical appliances in VISUAL BASIC 6 .
 
So kindly send me the code to complete my project work.
 
[ i need to interface pc to the appliance using either parallel port or serial port with VB6 ONLY.] the mail address is badugu07@yahoo.com
QuestionBy this i can use AC appliance On/OFFmemberDenish_Dens20-Sep-07 0:25 
Hello I m developing one windows application in which i want to design one User interface in C# language where user can easily on/off the Device (AC Power Supply) Please It so urgent please mail me on denish.dens@gmail.com
 

I am waiting for the good feedback from your side
 
Denish IT Solution
AnswerRe: By this i can use AC appliance On/OFFmemberkomalre9-Sep-09 6:53 
hello sir,
tell me how it is possible in java..
QuestionC# parallel port programming for data reading from DTMF decordermembersajith senevirathne4-Aug-07 19:01 
i have a DTMF decorder its number is MT8870. i want the code in C# that connects the DTMF BCD output(4pins output)to the parallel port of a computer.this code should read the BCD output of the DTMF and convert into a normal decimal number which represents the dialed number.and writes into txt file
Generalplz Helpmembermatata_mn31-Jul-07 2:58 
Hi
i Dl this project but i can't see it in Design view!!!
I added Refrencec.
I use Vs 2005 .plz say me how I can run it???
 
Mati

Generalcode to control electrical appliances in VISUAL BASIC 6memberkillroyz22-Jun-07 19:05 
can someone has the code to control electrical appliances in VISUAL BASIC 6 .
 
So kindly send me the code if you have one
 
[ i need to interface pc to the appliance using either parallel port or serial port with VB6 ONLY.]
 
pls send in my email roy_sam315@yahoo.com
 
ThanksSmile | :)
 

Generalelectricity bulbmemberGiorgi Dalakishvili24-May-07 10:27 
Hello, I want to control a simple light bulb. How can I do it? Can you give me some tips? Thanks
 

QuestionHow to control appliances in vb6memberalchino14-May-07 5:21 
can any one suggest me the code to cotrol electrical appliances in VISUAL BASIC 6 .
 
So kindly send me the code to complete my project work.
 
[ i need to interface pc to the appliance using either parallel port or serial port with VB6 ONLY.]
 
pls send in my email Alemayehu_lemma@yahoo.com
 
Thanks
 

QuestionHow to run the codes you have given?membershivanshu22-Apr-07 4:19 
hi,
Im a engineering student ,im working in this project and got stuk at point ,like how to go about running those codes given by you,can you give me the algorithmic approach to how to do it?
you can contact me via mail
my id -hurricane.shiv@gmail.com
GeneralA reader's commentstaffSean Ewington2-Apr-07 11:32 
i has a few problem about this circuit, can you explain how this circuit regenerate.
GeneralSolution of all problemsmemberPumsti22-Mar-07 16:09 
The existing C# code is unfortunately several times wrongly
 
first this is a function from the :
original code
 

private void IO(int bit)
 
{
 
val = axHwinterface1.InPort(888);
 
axHwinterface1.OutPort(888,(short)(val|bit));
 
label1.Text=axHwinterface1.InPort(888).ToString();
 
}
 
with this code you can only switch a bit to one
 
"or" is not to invert a bit. correct is xor in this case
the right code looks like this
 
axHwinterface1.OutPort(888,(short)(val^bit));
 
00101001 XOR
00100000 =
00001001
 

much more better then invert is an and mask then invert the and mask after it use the mask
 
mask = 00100000 to delete the second bit now invert it with ~ to
11011111 now use and with this mask on the port value
 

 
second mistake:
original code:
 
private void rBtnD7On_CheckedChanged(object sender,
System.EventArgs e)
 
{
 
IO(128);
 
}
 
private void rBtnD7Off_CheckedChanged(object sender,
System.EventArgs e)
 
{
 

there are two functions of the radiobutton change event.one function to set a bit one to delete a bit. this is wrong becouse if the change event is released, the system will call both function one behind the other.you can easy proof this with the debuger.So as the code is.the first call will set a bit and the other will delete it Frown | :(
you should onny use one function from the change event and proof the status of the radiobutton
 
the best way to get the activex control(.ocx) to work is to register it easy with regserver32 name.ocx then go to c# and add easy the ocx to your toolbox and use it in the widows form
the other way to isolate the ocx at the property of the reference withaout register it to the windowsregistry did not work at my one program.
 
never the less now all works fine Big Grin | :-D
 

 

 


GeneralRe: Solution of all problemsmemberMember 26463329-Mar-09 0:28 
Hi
 
Thanks your correction has helped me a lot.
 
But Iam getting an error at the following line:
 
axHwinterface1.OutPort(888,0);
 
Code:
=====
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
axHwinterface1.OutPort(888,0);


// TODO: Add any constructor code after InitializeComponent call
}
 
Error: An unhandled exception of type 'InvalidActiveXStateException' occurred in system.dll
 
Could you help me out.
Questionprogramme a pci slotmemberMember #392903815-Mar-07 23:25 
Hi,I want to programme a pci slot with java.I expect to control an electronic device using that programme.Specialy I want to mention here, that I have very poor knowledge about the subject about pci programming.Please help me.Thank you!
 
prasad
GeneralProblem with Controlling home appliances with PCmemberbenteee19-Feb-07 9:50 
Hello,
 
I have built the circuit, but using the program provided it is not able to control the parallel port.
 
I would be glad if someone could help me out.
 
Thanks in advance.
 
Bye!!!
 
possia

Generalhi sirmembergpremkamal12-Feb-07 19:58 
how can i use this dll in vc#.net
iam try to send/read data from parallel port
plz help.
 
prem
GeneralProblem with Controlling home appliances with PCmemberkishoredvrs5-Feb-07 14:29 
Respected Taha,
 
I am kishore,an engineering student.
I started working on Control of applliances using PC
but i am not getting the output.I was trying with C++,a language that i am not familiar with.
 
Can u send me the code for this in C languagage.
 
Thanking u in anticipation.

GeneralHardware problemmembersalawieh16-Dec-06 0:34 
Dear All,
Its my first experience with the communication between SW and HD to control appliances, an i just would like to know how i can find such a controller already built, i've no experience with electronics and iam not planning to build the circute my self, pls reply to me asap.
 
Regards,
Generalerror: An Unhandled exceptionmemberlawscom1-Dec-06 4:52 
hi
when i compile the code for this project it is giving me an error message;
An Unhandled exception of type 'System.nullreferenceException' occured in controlle-appliance.exe
 
additional informtaion;objec reference not set to an instance of the object
 

in the line of code;
Public form1()
initiaize component()
axhwinterface.output(888.0);
 
how do i fix it?
thanks
 
lawscom

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 31 May 2004
Article Copyright 2004 by Taha Amin
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid