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

7

13

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.

Jackshaft answered 16/10, 2008 at 17:55 Comment(0)
K
17

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

Kaliningrad answered 16/10, 2008 at 18:11 Comment(4)
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!Seaton
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.Kaliningrad
The Arduino is definitely slick.Md
@Dan the OP didn't say they had a PC. You could say buying a PC and installing an operating system and compiler is overkill if they just want to blink a LEDThrashing
S
6

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.

Seaton answered 16/10, 2008 at 18:16 Comment(0)
E
2

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:

Eddie answered 16/10, 2008 at 18:25 Comment(0)
M
1

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.

Marou answered 16/10, 2008 at 18:10 Comment(0)
S
1

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.

Sclerotomy answered 16/10, 2008 at 18:19 Comment(1)
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).Seaton
L
0

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.

Lavish answered 16/10, 2008 at 18:9 Comment(1)
@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: linuxfocus.org/common/src/article205/ppdev.htmlSeaton
B
0

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.

Bono answered 16/10, 2008 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.