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

Brian R. Bondy picture Brian R. Bondy · Oct 16, 2008 · Viewed 57k times · Source

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.

Answer

mwilliams picture mwilliams · Oct 16, 2008

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