Brian R. Bondy
Brian R. Bondy

Reputation: 347216

Steps to make a LED blink from a C/C++ program?

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.

EDIT: Interested in any OS specific solutions.

Upvotes: 13

Views: 68804

Answers (7)

Tanj
Tanj

Reputation: 1354

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.

Upvotes: 0

dar7yl
dar7yl

Reputation: 3747

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.

Upvotes: 1

Dan Lenski
Dan Lenski

Reputation: 79752

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.

Upvotes: 6

Michael Burr
Michael Burr

Reputation: 340198

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:

Upvotes: 2

mwilliams
mwilliams

Reputation: 9978

Here's a tutorial on doing it with a parallel port.

Though I would recommend an Arduino which can be purchased very cheaply and would only involve the following code:

/* Blinking LED
 * ------------
 *
 * turns on and off a light emitting diode(LED) connected to a digital  
 * pin, in intervals of 2 seconds. Ideally we use pin 13 on the Arduino 
 * board because it has a resistor attached to it, needing only an LED

 * 
 * Created 1 June 2005
 * copyleft 2005 DojoDave <http://www.0j0.org>
 * http://arduino.berlios.de
 *
 * based on an orginal by H. Barragan for the Wiring i/o board
 */

int ledPin = 13;                 // LED connected to digital pin 13

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}

alt text

http://www.arduino.cc/en/Tutorial/BlinkingLED

Upvotes: 17

Robert Deml
Robert Deml

Reputation: 12532

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.

Upvotes: 1

nobody
nobody

Reputation: 20173

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.

Upvotes: 0

Related Questions