Simple Relay and Sensor

Embed Size (px)

Citation preview

  • 8/9/2019 Simple Relay and Sensor

    1/39

    Simple Relay and SensorInterface for PC

    Guided By

    Mrs. N.D. Mehta Maam

    Sunil S Pillai

    Department of Electronics & Communications

    Government Engineering College, Sector-26

    Gandhinagar

    10546, October 2007

  • 8/9/2019 Simple Relay and Sensor

    2/39

    ACKNOWLEDGEMENT

    Before we get into thick of the thing, I would like to add a few heartfelt words for

    the people who have been behind this project.

    First of all, few words for the Almighty God for showering his choicest blessings

    on me and enabling me to complete this Project report well in time and with desired

    quality.

    I would like to voice my sincere thanksgiving to Mrs. Neeta Maam Head of

    Electronics and Communication Engineering Department, for providing us with such a

    high potential opportunity. Maam, I hope I have utilized it to the best of your

    expectation.

    In continuance, I would like to mention about my parents. Thank you Mom and

    Dad for your continued encouragement and guidance.

    Finally I would like to express my gratitude to all my classmates for their constant

    feedback and support in improving this document and making it more reader friendly.

    Lastly I do not intend to forget all the people who painstakingly read the entire

    manuscript and provided valuable inputs to make it free from errors.

    Thanking You All.

    Relay & Sensor Interface For PC GENG2

  • 8/9/2019 Simple Relay and Sensor

    3/39

    INDEX

    1) Description 4

    2) Circuit Diagram 5

    3) Working 6

    4) Software 8

    5) Parts List 16

    6) Datasheet 17

    7) Bibliography 35

    Relay & Sensor Interface For PC GENG3

  • 8/9/2019 Simple Relay and Sensor

    4/39

    DESCRIPTION

    This is a simple and low cost interface circuit for operating more than 100 relays and

    sensing the TTL level output from over a dozen sensors using PC. The circuit is useful for home

    as well as industrial applications. The relay operation and sensor detection is carried out using

    software written in C Language. The circuit may be connected to parallel port of a computer

    via its 25 pin D connector.

    The parallel port of an IBM Compatible personal computer uses a 25 pin D Connector.

    This port is normally used for Connection to centronics port of printer. The base address of

    printer port may be 378, 278 or 3BC (all hex). This port is referred to as LPT1 port. Port 378 is

    an output data port while port 37A is a combined input output port.

    The output data lines D0-D7 (pin 2 through pin 9) with port address 378(hex) and bits

    D0 (pin 1) and D2 (pin 16) with port address 37A are used. Pin 1 (bit D0) of port 37A is used as

    input pin for detecting the status (logic 1 or 0) of sensors whose output confirm to TTL

    specifications. Pin 16 (bit D2) of port 37A is used as output data pin representing on (logic 1)

    or off (logic 0) state of relays to be energized / de-energized via this interface.

    Data lines D0-D7 from pin 2 through pin 9 of 25 pin D Connector are terminated on

    74HC245 octal transreceiver IC1. The buffered outputs from this IC are used as address lines.

    Buffered Data lines D4 through D7 are used as address lines A0 to A3 for 1-to-16 line

    decoder IC2 (74HC154). Here outputs O1-O3 only are used as chip select signals for IC4, IC7

    and IC10 respectively. Output O0 is not used while remaining outputs O4-O15 are available for

    further use/expansion. Buffered Data lines D0-D2 from IC1 are used by relay switching section

    while data lines D0-D3 are used by sensor section as address lines.

    Relay & Sensor Interface For PC GENG4

  • 8/9/2019 Simple Relay and Sensor

    5/39

    CIRCUIT DIAGRAM

    Relay & Sensor Interface For PC GENG5

  • 8/9/2019 Simple Relay and Sensor

    6/39

    WORKING

    Relay Switching Section

    Provision is made for operating 14 Relay Switching Sections with this Circuit. Each

    Relay switching section can accommodate Seven Relays. All Relays (in excess of 100) can be

    switched on or off independently with software control.

    Each switching section comprises three ICs. IC 74HC75 is a 4 bit Bistable latch used for

    latching the three address lines (D0-D2 from IC1) and relay status data received via pin 16 of 25

    pin D connector.

    In fact, for this circuit, these latches are not absolutely essential since the data output

    lines D0 through D7 of port 378 (hex) and all lines (open collector) of port 37A (hex) use 74374

    (octal) and 74174 (hex) D- latches inside the PC itself. The provision of additional latches could

    come handy in case a user wants to interface this circuit to parallel port of any other

    microprocessor based circuit having a parallel port without internal latches.

    After the relay address and its state (on/off) data is latched at the output of 74HC75 IC, it

    serves as a 3- bit address (A0-A3) and Data input for 74Hc259 IC (used in 8-Bit addressable

    latch mode). When chip select line connected to pin 14 (En) of this IC is taken to active-low

    logic level, the data present at its pin 13 is output at selected (addressed) output pin while all

    other output pins stay at their previous state. This feature is very useful for selectively switching

    on or off a relay without disturbing other relays.

    Although all eight outputs (Q0-Q7) are available for use, only the first seven outputs are

    used for driving relays via high current driver IC ULN 2003. External freewheeling diodes are

    not required as these are in-built within the IC itself. With ULN 2003 we may use either 12-volt

    or 24-volt supply for the relays and pin 9 of the IC.

    Relay & Sensor Interface For PC GENG6

  • 8/9/2019 Simple Relay and Sensor

    7/39

    WORKING

    Sensor Section

    Each Sensor section can accommodate 16 sensors. The sensor output must confirm to

    TTL logic specification. The sensor section consists of two ICs: a 4 Bit Bistable latch 74HC75

    IC and a16- input multiplexer 74LS150. The output lines of sensors are terminated at the

    multiplexer. Pin 10 of 74LS150 carries the information back to the parallel port of the PC.

    A 4 Bit Bistable latch 74HC75 IC is used for latching D0-D3 buffered outputs of IC1.

    These latched outputs are used as address lines A0-A3 for 16- input multiplexer 74LS150.

    Depending upon the address input and active low chip select signal present at its pin 9 (En -), a

    particular input line is selected.

    The digital input from the sensor available at the addressed input line is inverted and

    appears at its output pin 10. This serves as input to the computer via pin 1 of 25 pin D

    connector. The input data via pin 1 is sensed by the computer after negation. The double

    negation of logic output from sensor, once by 74LS150 and then by PC results in true state of

    sensor being acquired by the PC.

    Software is used to interpret the data acquired by the PC which controls the further

    activities including energizing / de-energizing the relays.

    Relay & Sensor Interface For PC GENG7

  • 8/9/2019 Simple Relay and Sensor

    8/39

    SOFTWARE

    The program starts by presenting three options on the monitor:

    1) Switching the Relay

    2) Getting Digital Signal from Sensor

    3) Exit from the Program

    Depending upon the selection made via the keyboard the program branches to either

    Relay Switching Function (relay) or Sensor Signal Detection Function (sensor) or quits.

    Under relay function, the program prompts for entering the relay number (relaynum)

    and whether it is to be turned off or on (onoff). From relaynum it deducts csl value which is

    used for determining the chip select line to be activated. Depending upon onoff value, program

    follows one routine or other. During both routines it outputs a number from port 378 (hex) which

    when decoded leads to the selection of chip select line and the relay number (address).

    The logic used is as follows

    (a) The chip select signal can be worked out from the following relationship:

    CSL = 16 (D4*1 + D5*2 + D6*4 + D7*8)

    Where D4-D7 are IC1 buffered outputs which may either assume a value of 1 or 0.

    (b) Relay Line Address = (D0*1 + D1*2 + D2*4)

    Relay number = relay line address 1.

    Where D0-D2 are IC1 buffered outputs which may either assume a value of 1 or 0.

    Relay status data is output from port 37A (hex) as FF (hex) or 00 (hex) depending upon

    whether relay is required to be switched on or off.

    For detecting the logic level present at sensor input lines (I0-I15) similar logic is used.

    The value of bits D0-D3 is used for determining the address of sensor, while D4-D7 are used for

    activating proper Chip Select Line. The output of 74LS150 pin 10 is connected to the pin 1 (D0

    bit) of port 37A. The sensor status so acquired is logically ANDed with 01 to analyse the value

    of D0 bit which indicates whether relay is on or off

    Relay & Sensor Interface For PC GENG8

  • 8/9/2019 Simple Relay and Sensor

    9/39

    Software listing for relay switching and sensor state detection is given below:

    #include"stdio.h"#include"conio.h"#include"dos.h"

    void relay(void);

    void sensor(void);void lastpage(void);void pl(char c);void pb(int n);void ht(int n);void bell(void);

    void main(){

    clrscr();int choice;char m_menu;do

    { m_menu= 'y';clrscr();pb(4);pl('@');pb(1);ht(1);printf(" Welcome to Relay switching and Receiving Digital Sensor

    Signal");pb(1);pl('@');pb(3); ht(3);printf(" Welcome to Main Menu ");pb(3); ht(2);printf(" ( 1 ) Relay Switching Section ");pb(3); ht(2);

    printf(" ( 2 ) Sensor Signal Detection Section ");pb(3); ht(2);printf(" ( 3 ) Exit ");pb(5); ht(1);printf("Enter your Preference ::");bell();scanf("%d", &choice);

    switch(choice){

    case 1:relay();break;

    case 2:sensor();break;

    case 3:lastpage();m_menu= 'n';break;

    default:pb(2); pl('$');pb(1); ht(1);

    Relay & Sensor Interface For PC GENG9

  • 8/9/2019 Simple Relay and Sensor

    10/39

    printf(" Invalid choice ");pb(2);printf(" Indicate your choice by pressing the option

    number displayed on the screen ");pb(1); pl('$');pb(2); ht(1);printf(" Press Any key to continue ");bell();getch();break;

    }// end of switch (choice)}// end of dowhile( m_menu == 'y');

    } // END OF MAIN FUNCTION/*-------------------------------------------------------------------------*/

    // FUNCTION TO SWITCH RELAYS

    void relay(void){

    int relaynum, onoff, csl,choice;

    RELAYSCREEN:clrscr();pb(3);pl('@');pb(1); ht(2);printf("( 1 ) Relay Switching Section ");pb(1);pl('@');pb(4); ht(1);printf("Enter Relay Number (1 - 100) :: ");bell();scanf("%d", &relaynum);

    if(relaynum < 1 || relaynum >100){

    pb(3);pl('#');pb(1); ht(3);printf("Invalid Relay Number ");bell();pb(2); ht(2);printf(" Please Enter Relay Number only between 1 - 100 ");pb(1); pl('#');pb(3); ht(1);printf("( 1 ) Main Menu\t\t\t ( 2 ) Relay Switching Section ");pb(3); ht(2);printf("Enter your Preference ::");bell();scanf("%d",& choice);

    if(choice==2)goto RELAYSCREEN;

    elsereturn;

    }

    pb(3);printf(" Enter switching action (To turn ON press 1, To turn OFF Press

    0):: ");

    Relay & Sensor Interface For PC GENG10

  • 8/9/2019 Simple Relay and Sensor

    11/39

    scanf("%d", & onoff);

    if(onoff != 1&& onoff != 0){

    pb(3); pl('#');pb(1); ht(3);printf(" Invalid Switching Action ");bell();pb(2); ht(2);printf(" To turn ON press 1, To turn OFF Press 0 ");pb(1);pl('#');pb(3); ht(2);printf(" Press Any key to continue ");getch();goto RELAYSCREEN;

    }

    if(relaynum>=1 && relaynum=8 && relaynum=15 && relaynum

  • 8/9/2019 Simple Relay and Sensor

    12/39

    {case 1:

    outportb(0x378,(relaynum-1+16));outportb(0x37a,0x00);pb(4); ht(1);printf(" Relay Number %d sucessfully switched off",

    relaynum);break;

    case 2:outportb(0x378,(relaynum-8+32));outportb(0x37a,0x00);pb(4); ht(1);printf(" Relay Number %d sucessfully switched off",

    relaynum);break;

    case 3:outportb(0x378,(relaynum-15+48));outportb(0x37a,0x00);pb(4); ht(1);printf(" Relay Number %d sucessfully switched off",

    relaynum);break;

    }// END OF SWITCH (CSL)}//END OF if (onoff==0)

    bell();pb(3);pl('*');pb(2); ht(1);printf("( 1 ) Main Menu\t\t\t ( 2 ) Relay Switching Section ");pb(2);pl('*');pb(3); ht(2);printf("Enter your Preference ::");bell();scanf("%d",& choice);

    if (choice == 2)goto RELAYSCREEN;

    return;} // END OF RELAY/*-----------------------------------------------------------------------*///FUNCTION TO ANALYSE DIGITAL SENSOR SIGNALvoid sensor(void){

    int sensor,signal,choice;

    outportb(0x378,0x00);

    outportb(0x37a,0x00);

    SENSORSCREEN:clrscr();pb(3); pl('@');pb(1);ht(2);printf(" ( 2 ) Sensor Signal Detection Section ");pb(1);pl('@');pb(4); ht(2);printf("Enter Sensor Number (1 - 16) :: ");

    Relay & Sensor Interface For PC GENG12

  • 8/9/2019 Simple Relay and Sensor

    13/39

    bell();scanf("%d", &sensor);

    if(sensor < 1 || sensor >16){

    pb(3); pl('#');pb(1);ht(1);printf(" Invalid Sensor Number ");bell();pb(2); ht(1);printf(" Please Enter Sensor Number only between 1 - 16 ");pb(1);pl('#');pb(3); ht(1);printf("( 1 ) Main Menu\t\t\t ( 2 ) Sensor Section ");pb(3); ht(2);printf("Enter your Preference ::");scanf("%d",& choice);

    if (choice == 2)goto SENSORSCREEN;

    elsereturn;

    }

    outportb(0x378,(sensor-1+48));signal=inportb(0x37a);

    signal=signal & 1;if(signal==0){

    pb(4); ht(1);printf(" Input Signal at Sensor Number %d is Low ==0",sensor);

    }if(signal==1){

    pb(4); ht(1);printf(" Input Signal at Sensor Number %d is High ==1",sensor);

    }

    outportb(0x37a,0xff);

    pb(4);pl('*');pb(2); ht(1);printf("( 1 ) Main Menu\t\t\t\t ( 2 ) Sensor Section ");pb(2);pl('*');pb(3); ht(2);printf("Enter your Preference ::");

    bell();scanf("%d",& choice);

    if (choice == 2)goto SENSORSCREEN;

    return;

    }//END OF SENSOR/*-------------------------------------------------------------------------*/

    Relay & Sensor Interface For PC GENG13

  • 8/9/2019 Simple Relay and Sensor

    14/39

    // FUNCTION TO DISPLAY LAST PAGE

    void lastpage(void){

    clrscr();

    pl('@');pb(0);ht(4);printf(" EXIT");pb(0);pl('@');

    pb(2);ht(4);printf("THANK YOU ");

    pb(3);ht(3);printf("FOR EVALUATING THIS PROGRAM");

    pb(2);ht(3);printf("ANY SUGGESTIONS/COMMENTS/QUERIES ");

    pb(2);ht(4);printf("DO CONVEY IT TO:");

    pl('$');pb(1);ht(4);printf("SUNIL - S");bell();pb(1);ht(2);printf(" Dept of ELECTRONICS & COMMUNICATION ");pb(1);ht(2);printf(" GOVERNMENT ENGINEERING COLLEGE, Sector-26");pb(1);ht(4);printf("GANDHINAGAR.");pb(1);pl('$');pb(2);ht(3);printf("PRESS ANY KEY TO DEPART.... ");

    printf("\a");getch();

    }/*------------------------------------------------------------------------*/

    /*-------------------------------------------------------------------------*/// FUNCTION TO PRINT A LINE OF SPECIAL SYMBOLS

    void pl(char c){

    int i,len;#ifdef scrlen

    len=scrlen;#else

    len=80;#endif

    printf("\n"); for(i=1;i

  • 8/9/2019 Simple Relay and Sensor

    15/39

    /*------------------------------------------------------------------------*/// FUNCTION TO PRINT A SPECIFIED NUMBER OF BLANK LINES

    void pb(int n){

    int i; for(i=1;i

  • 8/9/2019 Simple Relay and Sensor

    16/39

    PARTS LIST

    Relay & Sensor Interface For PC GENG

    Semiconductors

    IC1, 74HC245 Octal Bus Transreceiver

    IC2 74HC154 1-16 Decoder/Demultiplexer

    IC3, IC6, IC9 74HC75 4-Bit Bistable Latch

    IC4,IC7 74HC259 8- Bit Addressable Latch

    IC5, IC8 ULN2003 7 Channel High Current Driver

    IC10 74LS150 16 Input Multiplexer

    Resistors(All - Watt +- 5% Carbon unless stated otherwise )

    R1-R14 1 Kilo Ohm

    Capacitors

    C1-C10 0.1 uF Ceramic Disc

    Miscellaneous

    LED1- LED14 All Red LEDs

    RL1 RL14 12V, 200 Ohm 1 C/O Relay

    8 Pin male/female SIP Connector

    Flat Cable (20 way)

    20 pin FRC Connector

    16 pin male/female SIP Connector

    25 pin male/female D Connector

    16

  • 8/9/2019 Simple Relay and Sensor

    17/39

    DATASHEETS

    1) Octal Bus Transreceiver - 74HC245

    2) 1 16 Decoder / Demultiplexer 74HC154

    3) 4 Bit Bistable Latch 74HC75

    4) 8 Bit Addressable Latch 74HC259

    5) 7 Channel High Current Driver ULN2003A

    6) 16 Input Multiplexer 74LS150

    Relay & Sensor Interface For PC GENG17

  • 8/9/2019 Simple Relay and Sensor

    18/39

    Relay & Sensor Interface For PC GENG18

  • 8/9/2019 Simple Relay and Sensor

    19/39

    Relay & Sensor Interface For PC GENG19

  • 8/9/2019 Simple Relay and Sensor

    20/39

    Relay & Sensor Interface For PC GENG20

  • 8/9/2019 Simple Relay and Sensor

    21/39

    Relay & Sensor Interface For PC GENG21

  • 8/9/2019 Simple Relay and Sensor

    22/39

    Relay & Sensor Interface For PC GENG22

  • 8/9/2019 Simple Relay and Sensor

    23/39

    Relay & Sensor Interface For PC GENG23

  • 8/9/2019 Simple Relay and Sensor

    24/39

    Relay & Sensor Interface For PC GENG24

  • 8/9/2019 Simple Relay and Sensor

    25/39

    Relay & Sensor Interface For PC GENG25

  • 8/9/2019 Simple Relay and Sensor

    26/39

    Relay & Sensor Interface For PC GENG26

  • 8/9/2019 Simple Relay and Sensor

    27/39

    Relay & Sensor Interface For PC GENG27

  • 8/9/2019 Simple Relay and Sensor

    28/39

    Relay & Sensor Interface For PC GENG28

  • 8/9/2019 Simple Relay and Sensor

    29/39

    Relay & Sensor Interface For PC GENG29

  • 8/9/2019 Simple Relay and Sensor

    30/39

    Relay & Sensor Interface For PC GENG30

  • 8/9/2019 Simple Relay and Sensor

    31/39

    Relay & Sensor Interface For PC GENG31

  • 8/9/2019 Simple Relay and Sensor

    32/39

    Relay & Sensor Interface For PC GENG32

  • 8/9/2019 Simple Relay and Sensor

    33/39

    Relay & Sensor Interface For PC GENG33

  • 8/9/2019 Simple Relay and Sensor

    34/39

    Relay & Sensor Interface For PC GENG34

  • 8/9/2019 Simple Relay and Sensor

    35/39

    Relay & Sensor Interface For PC GENG35

  • 8/9/2019 Simple Relay and Sensor

    36/39

    Relay & Sensor Interface For PC GENG36

  • 8/9/2019 Simple Relay and Sensor

    37/39

    Relay & Sensor Interface For PC GENG37

  • 8/9/2019 Simple Relay and Sensor

    38/39

    Relay & Sensor Interface For PC GENG38

  • 8/9/2019 Simple Relay and Sensor

    39/39

    BIBLOGRAPHY

    ( 1 ) ELECTRONICS FOR YOU Magazine Nov 1997 Issue

    ( 2 ) www.datasheetscatalog.com

    ( 3 ) LET US C by Yashwant Kanetkar

    http://www.datasheetscatalog.com/http://www.datasheetscatalog.com/