Arduino Beginner Episode 1: Hello World

Screen_shot_2010-05-15_at_10.39.51_PM.png

Hello all! In this walkthrough, I'm going to show you the basics of the Arduino and how to blink an LED. I know this has been done many times, but since I'm hoping to create a series of tutorials, this is where I'm going to start.

 

Step One: Purchase your Arduino.

There are many outlets where you can buy an Arduino. Some include Sparkfun, Solarbotics, Maker's Shed, and many others. If you are a total beginner to electronics and microcontrollers, I highly recommend the basic version (as of this writing, the Deumilanove). It comes at a pretty reasonable price (actually very cheap, if you remember things like the BASIC Stamp). The Deumilanove is a very flexible chip, and comes with plenty of pins for the beginner to be satisfied with. You could buy just the Arduino, but I highly recommend the project pack from Make because you get goodies like jumper wires, potentiometers, and switches.

 

Step Two: Set it up

What you will need to use your Arduino are a USB cable (the type you would use to plug into a printer, it has a big head) and the software.

Download the software from http://arduino.cc/ and set it up. It should come with drivers too.

Restart for the drivers to take effect, then plug your Arduino in to the computer. You should see a little green light turn on.

Launch the software. You will be presented with a simple IDE. Read on to step 3...

 

Step 3: Get going!

Alright, so we're getting there. Now we're going to blink an LED! First, we're going to blink the Arduino's built in LED.

Simply paste in this code, then hit the Upload button. If it doesnt upload, check around in the board and port menus and make sure that Deumilanove and the correct serial port is checked. If you don't know which port is your Arduino (and the software doesn't automatically find it), just try all of them. Also, note that the little LED has a resistor wired to it, something you should have on all your LEDs so they aren't damaged.

 


 

void setup() {

pinMode(13, OUTPUT);

}

void loop() {

digitalWrite(13, HIGH);

delay(250);

digitalWrite(13, LOW);

delay(250);

}

 


 

That's it! Now lets break it down so you understand it better.

 

void setup(){  }

Each segment of code between from the "void" to the last } is called a "function". The setup() function runs once when the Arduino starts up. We don't have anything that we need here, so the function is empty.

 

void loop(){  }

The loop() function runs over and over again, which is fairly self-explanatory.

 

digitalWrite(13, HIGH);

Now lets look more closely at the code. The digitalWrite() command basically tells the Arduino to turn something on or off. The first number in the command, in this case 13 (the LED on the board is wired to pin 13), is the number of the pin that the target component is connected to. The second part of the command, in our case either "HIGH" or "LOW" tells the Arduino to either turn that pin on (HIGH) or off (LOW). It's pretty simple.

 

delay(250);

And finally, lets look at delay(). This command tells the Arduino to halt the entire program for a given amount of time, which is specified in milliseconds in the parentheses. If you wanted the code to stop for a quarter of a second, you'd write "delay(250)", which we did. By delaying the code, we keep the LED on or off for a certain amount of time.

 

  ;

Another thing you might notice is the semicolons at the end of each line. This will be especially unusual if you came from a language such as BASIC. All they do is tell the controller that the end of the line is reached, and to move on to the next one. Notice that you'd only put these semicolons at the end of a line that is not the start or end of a function, so you'd never have something like "void setup() { ;  "

 

Alright, so that's it! If you have everything perfect, the little yellow LED (labelled L, I beleive) should blink on and off. Congratulations, your Arduino just said "Hello World"!

 

"But wait", you say. "How do I connect an external component like an LED to the Arduino?"

Here's how you do it. Take any ordinary LED and stick the shorter (negative) pin into GND (Ground, negative, its all the same here) and take the longer (positive) pin, connect it to a resistor, then stick the resistor in pin 13. Upload, and there you have it!

Thanks for reading and trying this out. Feedback would be greatly appreciated! I realize there may be mistakes, please don't be afraid to point them out so I can fix them.

Here’s how you do it. Take

Here’s how you do it. Take any ordinary LED (no Luxeons, they’ll destroy the board without proper drivers) and stick the shorter (negative) pin into the GND (Ground, negative, all the same) connector right by the #13 pin. Then, take the longer (positive) pin of your LED and plug it into the #13 pin. Upload the code, and voila! There you have it!

Also you must inform peoples that if you do plug an external LED to pin 13 it is best to write:

int LED = 13;

at the beginning of the program, thats just declare that you have the LED on pin 13. 

Also should know pin 13 have a built in resistor which is why you don’t need a resistor to test out an LED on that pin.

Pin 13 Does NOT have a built

Pin 13 Does NOT have a built in resistor to the female header, it has a resistor going to the built in LED, and external LED, will need a resistor…

use

#define LED 13  (no need for semi colons, or = signs when using the define)

 

instead of 

int LED = 13;

 

saves ram, because int, can be a variable, but we know the pin won’t change, so #define works here.

Yeah

Yep, you should have a resistor connected to the LED. I left this out because it just makes it simpler, plus it’s not really important in a circuit this basic.

And you have a good point with the LED variable, however I honestly don’t feel it is that important in what we’re doing now. However, I’ll start doing some higher quality code and more complete circuits next weekend or so… I’m thinking now that we’ve went over digitalWrite(), we could do analogWrite()? Then perhaps we’ll learn how to use sensors, drive motors, make a robot… etc etc.

So does this mean we should

So does this mean we should have a post on best pratices…

Yeah

Yeah, that would be a great idea! Although I’ve been saying that this is a beginner tutorial, I want to keep it simple, etc. etc., I feel bad that some people might look at this and think that’s how you should do everything. So yeah, I think that would definitely help (if anyone bothers to look at it, I know I wouldn’t have! :smiley: )

The code is broken…

Hi,

Sorry to be a downer but: by default, digital pins on the ardino are set to be inputs (e.g: http://www.arduino.cc/en/Tutorial/DigitalPins). You need to set the pin mode to output in the setup function. (e.g. “pinMode(13, OUTPUT);”)

Also, as people already pointed out - LEDs need resistors to operate safely…

I know you’re probably trying to cover “just the basics” here, but it’s not helpful to gloss over something essential just because you think it might be complicated. Einstein said something helpful here: “Make everything as simple as possible, but not simpler.”

Ohhh man

Yep, I totally forgot about the default input mode. I am so sorry folks! Thanks for catching that, Philbot.

And, yes, I’ll add the resistor. I personally never use resistors with LEDs, but apparently this is too important to skip.

 

Thanks,

“dent”

leds

If you don’t want to use a resistor you CAN PWM the led, look at the datasheet, and find the correct timings, but that get over complicated for beginner tutorial.

 

Also, if the led is supposed to work at 2v, and supply it with 2v, you don’t need a resistor, this is good for power saving applications, the resistor will waste useful energy as heat.

The led without a resistor WILL burn soon, and i’m not sure about this, but leds pull a lot of current when they have no resistor, i don’t know if it can damage an arduino pin