Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Using Perl to make a hot-lead from any port

by Petras (Friar)
on Oct 04, 2005 at 09:27 UTC ( #497167=perlquestion: print w/replies, xml ) Need Help??
Petras has asked for the wisdom of the Perl Monks concerning the following question:

Hi, everybody!

This question is somewhat Perlish and somewhat hardwareish. And it takes me way beyond my skill level. I'm working with a volunteer group starting schools and orphanages in south-east Asia. The school wants a bell. That makes sense. They want it timed automatically, which also makes sense. If I were back in my sunny Southern California home I'd pick up something solid state, digital, and nice. No such luck on this little swampy island. I've set up a server (for files to be kept in one place) and it is always on so I thought, "If I could make Perl light up a USB port for x number of seconds it could throw a normally-open, 2.6 volt relay into closed position, which could then complete the circuit for a 220volt bell." Simple enough in theory....

So I started nosing around the Monastery. I searched for USB and found an interesting thread on Minidisk players. That's where I read, (paraphrased) "Doing anything in any language with USB is difficult." "Hmm, that makes it sound difficult." So I went to SoPW and the top post was a question regarding Win32::SerialPort. The existence of that module sounded promising, so I went to CPAN and found the module. WAY THE HECK OVER MY HEAD!!!! So, I thought I would at least post and ask if what I'm trying to do is possible. Is what I'm thinking possible? If you're taking the time to answer, "Yes!" and want to add a few seconds of your time to say how or where to start I'd greatly appreciate it.

And the children would appreciate it. For the love of God, think of the children!


Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.

-Howard Aiken

2005-10-04 Retitled by planetscape, as per Monastery guidelines
Original title: 'OT: Using Perl to make a hot-lead from any port'

  • Comment on Using Perl to make a hot-lead from any port

Replies are listed 'Best First'.
Re: Using Perl to make a hot-lead from any port
by marto (Bishop) on Oct 04, 2005 at 09:39 UTC
Re: Using Perl to make a hot-lead from any port
by zentara (Archbishop) on Oct 04, 2005 at 13:48 UTC
    Just some advice from an old technician, who knows that things often can go wrong. :-) Use an opto-isolator to read off of whatever port you choose to use. The isolator will only load the port with a simple led, and will have a few thousand volts of "insulation" between the port and the actual relays. You will blow out your computer, if for some reason (lightning, rats chewing wires, idiot janitors,etc) causes a short in the bell relay and pumps 110 or 220 vac into your port. Opto-isolators are very cheap and easy to setup. Do a google search for "opto-isolator computer port".

    I'm not really a human, but I play one on earth. flash japh
Re: Using Perl to make a hot-lead from any port
by samizdat (Vicar) on Oct 04, 2005 at 14:21 UTC
    As zentara just said and I posted in Re: Does Perl ring a bell?, a simple optoisolator will help you out with a minimal # of components. A 4N27 is a good choice, and it's dirt cheap, less than $0.50USD in single quantities. Current capacity of the phototransistor is only 50 ma, so you need to keep your driver relay small. A larger one, with a FET output phototransistor, is an NTE 3085. This one can handle 100ma. In either case, you can pulse a higher-current relay safely, but be aware that the length of the pulse needed is longer that the actual "clap" you will end up with, due to the mechanical nature of the system.

    Another possibility to explore is Opto-22 Solid-State Relays and similar generic products. These are commonly available on the surplus market, and can be wired up (in carefully sealed boxes!!!) to control such things as coffeepots and other AC appliances. I buy them surplus for $5 - $10USD and they're just as easy to use as the little DIP versions. Take care, though. Playing with AC can be deadly if you do not protect your wiring. I usually mount the SSR inside a steel 4"x4" deep handi-box along with a regular outlet and 3-wire plug on SJ cord.

    Be aware that once you start making your computer go "thump!" in the night (or "ding!", or "buzz", or ???), it's hard to stop. Embedded systems are some of the most fun computing systems around; they're highly addictive.
      As mentioned by a few people in Does Perl ring a bell?, one of the oldest and easiest (and hackiest) way to get digital control out of PC style hardware is the parallel port. If you don't have one, a USB one should work since the windows or linux dirvers will take care of making it seem like a normal printer port. shows a simple circuit that uses only a single 5v relay and one resistor or very simple circuits using transistors to run a 12v relay. As others have said though, you should probably add in an opt-isolator. You could use the output of a low voltage one to drive a transistor output stage much like the non-isolated 12v on on the page linked.

      One of the big beneifts of the parallel port over USB is that it supplies 5v, which means you have a lot more relays/opto-thingamies that you can use.

      Ah this makes me remember why electronics was so much fun when I was a kid :)

      PS: Great stuff with the volunteer work, just don't electrocute yourself in the process!

        One of the things to be careful of when using TTL-level signals is that 'low' is not 'off'. Low is typically 'anything less than 1.3 volts', which will often leave you with a measurable current draw even if your relay does not trip. The serial port goes from +12 to -12, and so an LED behaves as a diode, passing zero current when your bit is 'off'.

        The parallel port is indeed a great place to tie digital signals; but it is HIGHLY recommended that you use an optoisolator there as well. Most ports are implemented on the motherboard these days, and motherboards still cost a bit more than a bucket of sand. :D
Re: Using Perl to make a hot-lead from any port
by InfiniteLoop (Hermit) on Oct 04, 2005 at 13:27 UTC
Re: Using Perl to make a hot-lead from any port
by Moron (Curate) on Oct 04, 2005 at 12:01 UTC
    As simple as it is, you need an external controller for that switch that will be signalled to turn the bell on or off. Only when the controller is specified is it possible to say what the computer has to do.

    If you have a custom computer supplier (instead of the usual off-the-shelf trained monkeys) , they should be able to tell you which controller to get and it will have a manual that will tell you how to operate it from the PC.

    On second thoughts, because you are going to use a relay, if using unix or linux you just have to write lots of FFFF (hex) to the relevant device in /dev.

    More update: I also thoroughly agree with the replies that suggest isolating the port from the bell circuit. I had an idea for isolating with a rectifier although I am scratching that because it would only (literally) half work for a/c.


    Free your mind

      A diode is a semiconductor version of a rectifier, but it does not isolate the circuit against any of the nasties that lurk out there in The Real WorldTM. First off, any diode (or old-style germanium rectifier, for that matter) has a breakdown voltage, which is the point where the little bitty lectrons jump the PN junction chasm anyway, creating a closed circuit. Typically these 'reverse breakdown voltage' thresholds are on the order of 50-100v, and so either 110VAC or any level of static electricity is enough to pass a diode. The static charge generated by walking across a carpet can easily be more than 800v, although the current is rarely worth worrying about. A lightning strike nearby or any heavy rotating machinery like generators or motors can play havoc with the ground potential, though, so there are a number of very real potential trouble sources. In many of the third world countries, as in (still!) parts of the US, grounding is neither sufficient or complete, so extra care is highly recommended.
Re: Using Perl to make a hot-lead from any port
by jkao (Beadle) on Oct 05, 2005 at 02:43 UTC
    You should look into the websites and mailing lists of various amateur robotics groups, particularly the Seattle Robotics Society.

    The simplest way to activate hardware via your computer is by parallel port, which is capable of sinking enough current to drive a transistor. The nice thing about a parallel port is you get 8 digital outs, with no decoding required. This means you can hook up directly to analog electronics. As other posters pointed out, you probably want that to be an optoisolator, connected to a transistor, which in turn is connected to your relay.

Re: Using Perl to make a hot-lead from any port
by mattr (Curate) on Oct 05, 2005 at 15:14 UTC
    My first thought was how about using the speaker and using perl to drive some midi or wav player.

    But how about this freshmeat registered project on sourceforge? "usb-robot lets you quickly prototype USB device drivers in almost any language Perl, Python or whatever. You can even try out your ideas by directly typing in USB commands from the console."

    Likewise, perl-libusb is a Perl XS/XSUB interface to the libusb library, to allow you to program/talk to USB devices from user space under Linux or FreeBSD.

    Also check out Perl Serial Programming Resources and here is a document on connecting circuits to your computer via parallel port, usb, etc. I'm not a hardware guru but it has examples of safe circuits and bad ones, covers windows and dos, controlling the port from the web, optoisolators and solid state relays for controlling high power levels, circuit diagrams and links to other resources and kits, etc. You may be able to use one of the CPAN modules with it though it also provides some C code and tells you how to use it. Maybe someone with hardware experience would like to mention their opinion of this apparently comprehensive doc.

    Finally, this Single Channel Control Line linked from the above article looks like it might be the answer to your question. It describes construction of a simple alarm driving circuit, with photos, circuit diagrams, a parts list, how to create the circuit board, and so on. Regardless of what country you are in, my guess is that any university or electronics company could handle the circuit side for you if you are not experienced with it, but it also has links to an online electronics course e.g. on parallel port interfacing (the Printer Port section in the robotut linked page).

    Hope this helps. How about reporting back after you solve your problem?

Re: Using Perl to make a hot-lead from any port
by Adrade (Pilgrim) on Oct 05, 2005 at 05:38 UTC
    I may be missing the point here, but to accomplish your task, couldn't you just play a bell to a speaker - it would give you full control over what the students were hearing, and would seemingly be a lot simpler. You could then even extend the script to include things like morning announcements, etc.


    P.S. Whereabouts specifically are you in SE Asia? Kudos to you on a wonderful (!!!) project.

    By a scallop's forelocks!

Re: Using Perl to make a hot-lead from any port
by aufflick (Deacon) on Oct 12, 2005 at 23:14 UTC
    When/if you get this up and running, it would be really cool to have you post how it all went!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://497167]
Approved by jkva
Front-paged by sauoq
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2018-01-22 05:39 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (231 votes). Check out past polls.