An Isolated MOSFET Serial Port Relay Controller

Using DTR (data terminal ready) and RTS (request to send) pins of a PC serial port (RS-232) without actually using the serial data pins, we can interface at least two relay devices with a computer. Windmeadow Labs has an excellent article on how to achieve this using a bipolar transistor (BJT). Here I will show you a similar relay control circuit built using an opto-isolator and a MOSFET.

Here is the circuit diagram:

An Isolated MOSFET Serial Port Relay Controller

The benefit of using an opto-isolator is that the rest of the controller circuit is totally isolated from the serial port and thus prevents the port from accidentally being damaged by wrong wiring or current surge due to a failed component. And by replacing the drive circuitry from using a BJT to a MOSFET, the switching capability is improved dramatically, making switching higher current load more reliable and more efficient.

Here is a picture of the finished board:

Circuit Board

Circuit Board

And here is a simple program that toggles the relay on and off at an interval of 0.5 second.

#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdlib.h>

#define _SERIAL_PORT "/dev/ttyS0"

int main(int argc, char **argv)
{
        int fd, status = 0, cmd = 0;

        if ((fd = open(_SERIAL_PORT, O_RDWR | O_NDELAY)) < 0)  exit(1);

        while(1) {
                if (cmd) cmd = 0;  else cmd = TIOCM_DTR;

                ioctl(fd, TIOCMSET, &cmd);
                ioctl(fd, TIOCMGET, &status);

                if (status & TIOCM_DTR) puts("ON"); else puts("OFF");
                usleep(500000l);
        }

        close(fd);
}
Be Sociable, Share!

5 Comments

  1. Alan says:

    hi kerry,
    i m a student of electronic communication and i have a very low knowledge on computer programming.
    i have an electronic project that required me to turn on and off an light and buzzer by using serial port from dos window.
    from the above project it is the perfect circuit for me because of the cause and the functionality. but i could not get it to run using the script you give above. is the above script written in c++ or linux? can i run those script in dos? or what software do i have to use to complier it?

    please help me out here, many thank in advance.

    Thank,
    Alan

  2. Dear Kerry,

    I am Ashish from Mauritius, I work for MCA The Mauritius college of the air,An organisation under the ministry of tertiary education of my country.
    We essentially produce Television programs for schools and colleges.
    We are actually in the process of making a Quiz contest for colleges.We have purchased a quiz kit from Buzzgold with wireless slammers.
    Production people want to have a lamp that lights in front of the team that answer first. The slammer produces a code in notepad.Eg(/901)
    How can i possibly use the the code with the serial port or a usb to light up the bulb.surely i need an interface.
    There will be two teams with three contestants each each having his slammer.
    Each slammer generates a different code.(/901,/902,/903….)
    Could you kindly give me some hints how to come up with a suitable circuit for it to work.I have seen your work for the isolated MOSFET serial port relay controller and can that be possibly be used.

    Thank you in anticipation

    ashish

  3. belal says:

    hi Kerry,

    do you have such code in vb2008.

    i have already mount a circuit like this and working fine with free software available on the net, but i would like to write my own in VB2008 Express.

    thanks

    Belal

  4. Rick Warner says:

    What is the diode that goes from the FET drain to +5v for?

Leave a Reply