Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C#

Control Electrical Appliances using PC

Rate me:
Please Sign up or sign in to vote.
4.67/5 (56 votes)
30 May 2004CPOL5 min read 469.5K   16.2K   155   102
Control electrical appliances using PC.

Sample Image - circuit

Introduction

Hi there, I was searching the web for a way to allow me to control electrical appliances using a PC, and I found it here. Here is a circuit (Figure 1) for using the printer port of a PC, to control applications 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 pieces of equipment.

The interface circuit shown in figure 1 is drawn for only one device, being controlled by the 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 an opto-coupler ensures complete isolation of the PC from the relay driver circuitry.

Background

A parallel port is a simple and inexpensive tool for building computer-controlled devices and projects. The simplicity and ease of programming make parallel ports popular in the 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 a parallel port is, where it can be found, and for what it is being used. The primary use of a parallel port is to connect printers to the computer, and it is specifically designed for this purpose. Thus, it is often called a printer port or Centronics port (this name came from a popular printer manufacturing company, 'Centronics', which devised some standards for parallel ports). You can see the parallel port connector on the rear panel of your PC. It is a 25 pin female (DB25) connector (to which the 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 was published in 1994, defines five modes of data transfer for parallel ports. 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 with almost all types of parallel ports and can be used without any problems (not tested, just out of confidence!).

Hardware

The pinouts of the DB25 connector are shown in the picture below:

Image 2

The lines in the DB25 connector are divided into three groups:

  1. Data lines (data bus)
  2. Control lines
  3. Status lines

As the name suggests, 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 the 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 a program, one can easily read or write to a 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#.
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)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralHi,Inductive loads bad Pin
Robin Golden30-May-04 19:27
Robin Golden30-May-04 19:27 
GeneralRe: Hi,Inductive loads bad Pin
Ryan Beesley3-Jun-04 23:15
Ryan Beesley3-Jun-04 23:15 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.