tags:

views:

5527

answers:

8

What are the easiest steps to make a small circuit with an LED flash from a C/C++ program?

I would prefer the least number of dependencies and packages needed.

  • What port would I connect something into?
  • Which compiler would I use?
  • How do I send data to that port?
  • Do I need to have a micro-processor? If not I don't want to use one for this simple project.

EDIT: Interested in any OS specific solutions.

A: 

Essentially, if you are writing executable code, you need to have a microprocessor. A compiler will have to be outputting code for that microprocessor family.

At some level in your code, you will be flipping a bit that controls a given IO pin that has been previously tied to a LED in some fashion.

Now, if you get a led, a timer IC, a power supply, a breadboard, and some components, you can avoid this whole software business and wiggle that light.

Paul Nathan
You can't simply send power to one of the output pins of some port on your computer?
Brian R. Bondy
If he is powering off a microprocessor he should use active low as most chips can sink more then they can source.
Tanj
Nope. Not unless you write a driver to interface into said port, and then have your program talk to that driver.
Paul Nathan
Brian and Vlion: I think you guys are getting confused. If I understand right, Brian is asking if the /device/ needs to contain its own microprocessor, while Vlion is saying that the /driver/ (the computer) needs to have a microprocessor, which of course it does!
Dan
Hey, he could use a VAX or PDP/10 or some other not-at-all-micro processor. I'm sure that's what he meant!
Thomas Padron-McCarthy
A: 

It also depends on the OS. On Linux, you could wire an LED directly to the parallel port (with an appropriate current-limiting resistor, of course) and simply use the C function "outb()" to turn it on and off.

On Windows, it's a lot more complicated because the OS doesn't let user applications talk to ports directly.

Andrew Medico
Interesting. I was not aware that Linux had a syscall to talk directly to the parallel port.
Paul Nathan
@Vlion, Linux doesn't have any parallel-port-specific syscalls. But it does have the ioctl() catch-all syscall. And there are ioctls specifically to talk to parallel ports: http://www.linuxfocus.org/common/src/article205/ppdev.html
Dan
A: 

You could try to put an LED and a 300 Ohm resistor on the serial port transmit (pin 3) to Ground (pin 5). Then send data to turn it on.

The serial port can probably only source 10mA.

Good luck.

Robert
A: 

the easiest port to do this on would be serial or parallel. Always remember to put a resistor in series with the LED or you will burn it out

Tanj
+11  A: 
mwilliams
Good links! The Arduino looks cool. I've soldered my own Atmel/PIC prototyping boards, and it's kind of a pain. But a microcontroller-based board is overkill if the OP really *only* wants to hook up a few LEDs!
Dan
The Arduino is a pretty fantastic device, and cheap too at $35. But the link above my Arduino mention links to doing it with a parallel port which is the obvious and much cheaper solution.
mwilliams
The Arduino is definitely slick.
itsmatt
+3  A: 

Which port? Parallel port is my favorite choice since it outputs +5V (TTL logic level) and is very straightforward to program. Most parallel ports have enough power to drive an LED. It's important to remember that computer ports in general are designed to only output signaling voltages, and not to produce enough current to actually power most devices.

Which compiler? Doesn't matter. This kind of hardware hacking is more fun and easy under Linux, though, so GCC is a good choice.

How do I send data? Depends on the port and the operating system. USB is frightfully complicated for a simple project, so forget it. Serial and parallel ports can be controlled via a variety of different interfaces. My preference is to use the ioctl() system call under Linux to directly control the parallel-port pins. Here's info on how to do that: http://www.linuxfocus.org/common/src/article205/ppdev.html

Do I need a microprocessor? No, you don't need a microprocessor in the external device (obviously your computer has a microprocessor :-P). If you use the parallel or serial ports, you can just use the LED and a resistor or two and the necessary parts to connect the LED directly.

(Also: The Linux Device Drivers book, available for free online, has information on interfacing simple electronic devices to parallel ports and writing kernel drivers for them.)

EDIT: There seems to be massive confusion in this thread about what the OP means by, "Do I need a microprocessor?" Emphatically, the parallel port alone can drive an LED based on the software in the computer. No microprocessor is needed in the device. However, if you want the device to be able to control itself without being connected to the computer, a microprocessor or some other digital logic is required.

Dan
+1  A: 

for quick and dirty operations, you have 2 easy options: serial or parallel port. The serial port is easier, but is limited in the number of LEDs.

To connect the LEDs, you need a shell connector (DB25/DB9) of the correct sex, the LED's and a resistor. You would have to look up the value for your resistor yourself.

The serial port has control-flow signals which are under programmer control. It's a simple matter of outputting the correct bits to the MCR register (after opening the serial port).

The parallel port is a little bit harder, in that there is a bit more handshaking to do, but is generally the same principle of writing to a register.

You may have to fight your OS to gain control of the port.

Using the Tx line is somewhat complex, as the signal coming out is the serial bitstream of the data written to the transmit register. I would stick to the CTS and DSR signals.

For quick-and-dirty debugging, I have just written to the registers and watched the modem lights.

dar7yl
I'd have to disagree about serial vs. parallel port: you can just use the parallel port as a raw TTL output device with no handshaking or anything. And it has the advantage of more pins available and TTL levels (+5V/0V) rather than the serial port's RS-232 levels (non-standard but often +-12V).
Dan
+1  A: 

If you want to blink an LED without a microprocessor (which implies no C/C++), a simple circuit using a 555 timer IC will do the trick. These are common projects in beginner hobbyist electronics books or kits because they're really simple and you can get the parts at any Radio Shack type of place:

If you want to do it in software, as Vlion mentions, everything depends on the hardware being used and the design of the circuit that hooks up the LED.

If you want to try and mess around with something on your PC, here's an article on how to blink LEDs that are hooked up to pins on the PC parallel port:

Michael Burr