Hey Arduino, say hello to the world!

Last updated on Mon, 2010-07-12 19:15. Originally submitted by fabio on 2010-06-09 12:54.

Ok, here we are. As any programmer knows, the first thing you have to do in order to learn a new programming language / environment is trying to print or display some simple text, usually the words Hello World.

Unfortunately, a stock Arduino doesn't have any screen so it's pretty common to connect an LED to the Arduino and just blink it. This is how microcontroller programmers are used to say hello world!

Creating the Blinking LED circuit

Ok, let's go.. First we have to connect an LED to the Arduino board. We can't just connect the LED randomly. LEDs do have polarity: the longer leg has to be connected to power (+) while the shorter one to the ground.

We will use the pin 13 as the LED power source, so we will connect the LED to the Arduino inserting the longer led into pin 13 while the shorter one to GND.

NOTE: Pin 13 is a special one: it has a on board LED and 1K Ohm resistors attached in series to it. This means that it is safe to directly connect an LED to it. If we would have used another Digital Pin to power the LED, we would had needed to attach a 1K Ohm resistor to it.

Inserting the LED into pin 13 and GND

Hello World circuit

Using the breadboard to create our circuit

Instead of directly connecting the LED to the board we could use a breadboard.

The breadboard I use

A breadboard let you create circuit prototypes by simply inserting wires into the pins. No soldering is needed so it's really fast to create a circuit!

It works like this: pin lines marked with + and - are connected horizontally while the pin lines marked with letters are connected vertically. In the breadboard above there is no connection between letter groups A-E and F-J. Breadboards really make making series and parallel circuits really fast and easy! Cool stuff.

So, this is our circuit created using a breadboard. Note that the LED positive leg (the longer one is connected to the yellow wire).

Our Hello World Circuit mounted on a breadboard

The Blinking LED Program

This is the program we will use to blink the LED.

/**
* Intermitelly blinks a LED connected to LEDPIN
*/

#define LEDPIN 13


void setup() {
  pinMode(LEDPIN, OUTPUT);
}

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

Ok, pretty simple stuff.

We first define a constant called LEDPIN and we set its value to 13. This is to avoid using Magic Numbers.

setup() is executed once at the beginning and it's used to setup how the Arduino board will work. In this program we set the board to use LEDPIN (defined as pin #13 in the #define statement) as output, so that will be able to output current on that pin.

loop() is executed continually by the Arduino board.
It does the following in a loop:

  1. set LEDPIN to HIGH: this means that we are delivering 5V on pin 13. The LED will turn on
  2. wait for a second
  3. set LEDPIN to LOW (this means that we are not delivering current on 13). The LED will turn OFF.
  4. wait for another second

Simply by inserting the program above into the Arduino IDE editor and by uploading, the Arduino board will receive it and it will start blinking! Cool!

Blinking without using delay()

In the code above we used delay() to delay the exectution of the following lines of code. Unfortunately there is a downside of this approach: the whole board will be unusable while waiting for a delay() call. So you want be able to eg. blinking another led for example.

The code below, taken from Arduino tutorial, does exactly the same of the above one, but without using the delay() function.

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

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

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

Nothing too complex: if you have any programming background you probably already saw somehting quite similar to the above code.

Blinking 2 LEDs

This is a variation of the first program above. We now have 2 LEDs connected to LEDPIN1 and LEDPIN2 whose blinks intermittently. This is just a simple test I did to do some additional wiring.

As I already explained above, Pin 13 doesn't need a series resistor (it's built into the board), while Pin 12 will need it. I'll use a 2.2K Ohm but also a 1K Ohm will be OK. Our circuit will looks like:

Circuit for an Arduino Hello World using 2 LEDs

We can use the code below to intermittently blinks the two LEDs:

/**
* Intermitelly blinks two LEDs connected to LEDPIN1 and LEDPIN2 
*/

#define LEDPIN1 13
#define LEDPIN2 12


void setup() {
  pinMode(LEDPIN1, OUTPUT);
  pinMode(LEDPIN2, OUTPUT);
}

void loop() {
  digitalWrite(LEDPIN1, HIGH); // turns the LED on
  delay(500); // waits for a second
  digitalWrite(LEDPIN2, HIGH); // turns the LED on
  delay(500); // waits for a second
  digitalWrite(LEDPIN1, LOW); // turns the LED off
  delay(500); // waits for a second
  digitalWrite(LEDPIN2, LOW); // turns the LED off
  delay(500); // waits for a second
}

Ok, I think this is enough for my first programming steps. Let's continue next days.

Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a personal or company website insert its address in the form http://www.example.com/ .
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <small> <del> <img> <h2> <h3> <h4> <b> <video> <sub> <sup>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • You may insert videos with [video:URL]
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.

More information about formatting options