How can I put power to some wires [possibly connected via Serial Port?], by using a script? I use Ubuntu. I want to schedule a time [using cron] to automate turning on something, say, an LED, a small motor, anything like that. I just need the power to go on when I run the command, and I can use a transistor or whatever for more power.

How can I do it? My laptop doesn't have a serial port, but I can get a usb-to-serial adapter.

  • 2
    \$\begingroup\$ Would the Num-Lock LED satisfy "a" LED? :) \$\endgroup\$ – W5VO May 7 '11 at 4:47
  • \$\begingroup\$ @W5V0 no, I meant an "external LED" that I've wired, connected to some port on my laptop \$\endgroup\$ – Matt May 7 '11 at 13:57

What is the something that you want to turn on? You said LED in your question, but then said something in the question. :) I only ask because an LED vs. a solenoid can have very different power requirements. Is there a budget in mind? Do you want something turnkey?

I was going to recommend trying the TI Launchpad, which gives you a micro with a handful of I/O, plus a built-in USB-to-serial adapter, and is Linux-compatible, for a reasonable $5. The price is so good, it's unbelievable. And the tools are free. But if you want to make anything that requires more current than an LED turn on with a serial command sent via cron through Linux, then you'll have to wire up additional components to make it work.

If you don't have much of a budget, and want something that looks like it would work out of the box, take a look at this module from Phidgets to see if it's easy enough to get working in Linux. They provide plenty of source code, and have Linux drivers available.

There is also the obvious other option, Arduino, which from what I've read works on Linux. I'm just not a fan of the tools, nor the price when compared to the Launchpad.

EDIT -- a totally unrelated comment that I just had to share -- today I wanted to get more of the female headers for the Launchpad from Digikey. Well, the two headers cost $1 apiece. I ended up buying another Launchpad, because I would get both headers, as well as a USB cable, the dev board, and an extra MSP430G2 for an extra 3 bucks! Seriously, TI? :)

EDIT (regarding USB-to-serial adapter) -- I am actually having a hard time getting information that this works on the Launchpad. From this post, it sounds like it's possible, but I'm a little confused because some responses talk about the ez430, instead of the Launchpad. I definitely have the MSP430 application UART appear in my Device Manager, but haven't tried to use it in any way. I'll try to play with this next week to see if it really does work.

  • \$\begingroup\$ I don't think that I/O on the Launchpad or the Arduino can be controlled via the USB port. \$\endgroup\$ – Leon Heller May 7 '11 at 5:29
  • 1
    \$\begingroup\$ the USB port is actually a USB-to-serial adapter, and you can implement your own serial command interpreter on the MSP430. At least, that's what I've read. If for some reason that's really not true, then he would have to use some of the software serial implementations available on the net. But if you plug a Launchpad into the PC, it will come up as both a USB-to-serial adapter, as well as a USB FET for debugging. \$\endgroup\$ – Dave May 7 '11 at 5:53
  • \$\begingroup\$ I'll check for more information, as I don't want to give the OP any bad information here. I hope I didn't recall something incorrectly. \$\endgroup\$ – Dave May 7 '11 at 6:00
  • \$\begingroup\$ Arduino does work on Linux, and I was considering buying one. But this thing you have here is a lot cheaper, does it have lots of the same features? Could I make a Strobe light? A POV clock? or POV display, etc? that would be really awesome \$\endgroup\$ – Matt May 7 '11 at 12:59
  • \$\begingroup\$ Agreed - right now, the TI Launchpad is the ridiculously cheap way to go. \$\endgroup\$ – Toybuilder May 7 '11 at 13:41

If you only want to switch a single LED, programming a microcontroller is overkill. Use a USB->serial adapter, and toggle DTR or RTS pin using the serial API of your scripting language. (I recommend Python + pySerial, but every mainstream language will have some way of addressing serial ports.) This will only supply a few mA, probably enough to dimly light an LED. You can get more current by adding a transistor. The details of that are out of scope for this question.

edit: For ubuntu, first install pyserial:

sudo apt-get install python-serial

Then try something like the following:


import serial

  • \$\begingroup\$ Can you give me an example code? that's kind of what I want! If I can find my serial adapter.. then I could try it \$\endgroup\$ – Matt May 7 '11 at 13:55
  • \$\begingroup\$ @Matt, example code is not normally given on here. We are about designing the electronics, if you just want to know how to make the RTS go high on an RS232 cable then you will get great responses on SO. @markrages may have code available, but he may not even have ubuntu. \$\endgroup\$ – Kortuk May 8 '11 at 1:31
  • \$\begingroup\$ oh.. I see. well I posted the Q here because it was electronic-related. lol \$\endgroup\$ – Matt May 8 '11 at 13:42

Connect a suitable MCU with USB, such as a PIC18F2455, to the laptop via the USB port. Interface an LED to the PIC, and write some software to control it via a Linux program running on the laptop. I've done something like it which works with a Windows program, based on Brad Minch's work. Here are details of how to compile the code for the PIC and Windows, and details of the PIC hardware, including where to buy the PCB that I designed. You just need to write some Linux software similar to the Windows code. A lot more can be done with this hardware than simply toggling an LED, of course, as a sizeable prototyping area is provided.

The simplest and cheapest way to do it is to emit a tone from the headphone port on the laptop, amplify it (a single transistor) and rectify the amplified signal, and use that to control an LED via another transistor. This should cost under $1 to build. By generating different frequencies and detecting them more complex control is feasible.


Is this what you're looking for? Uses handshake line from serial port. Also has code in C for Linux & Windows to control it.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.