HOME - - - - - - - - - Other material for programmers - - - - - - - - - Arduino Tutorials Table of Contents

Arduino Programming: First Steps, using Arduino

This copy of this page is no longer the prime copy.

Please click on this link to be taken to the copy which is more certain to be up to date.



"Let's start at the very beginning...."

Ten-year-olds working at home without help should be running their own programs on Arduinos, after applying themselves to the following... if I've done it right! A quick word to those of you who are NOT new to programming: May I ask you to skim through the "Level One" material anyway? There are some things you might find useful. I'm working on a Windows XP machine.

Everyone: Please make use of the good helpfiles that come with your Arduino! My tutorials are not meant to supplant those, merely to take you through what is there in an easy (I hope) sequence.

"First catch your rabbit", in this case download and install the free software. I know: Its a hassle. It will take disc space. There are risks. Well, the Arduino software installed for me with minimal hassle. As the man said... Try it, you (may) like it. And at least in this case, you have some reassurance that it "should" work.

Launch Arduino.exe, the Arduino application. I've renamed my shortcut to that "Arduino Development Environment", to distinguish it from all the other Arduino stuff that will accumulate. If this is not the first time the program has been run, there may be text in the window below the "File .. Edit.. Sketch...." menu bar. Click on File|New|. The Arduino will set a default name for what you are creating. That name will start "sketch", then there will be an underscore, then today's data (really! Look at it closely. See?), then a letter, to distinguish all the things you do today without changing the default name.

"Sketch" is, I think, simply an Arduino name for what I'd call a program. You'll see it used thus elsewhere, too. The "Sketch" menu item covers things related to doing things to your "sketches".

After doing "File | New", you should have a blank page beneath the menu bar. Enter the following on it. Be advised: the Arduino programming language is "case sensitive". In other words, "PINMODE" won't do for "pinMode". Even "PinMode" won't do! (The "p" must be lower case.)

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

void loop()
{
  digitalWrite(13, HIGH);
  delay(600);
  digitalWrite(13, LOW);
  delay(600);
}

This is a Very Badly Written program... but it should work!

It should make the Arduino's digital pin 13 go high/ low/ high/ low... Why would we want to do that? Well, if you have a Diecimila, you should be seeing a little yellow LED wink on and off... about 0.6 seconds in each state.

If you don't have a Diecimila, then... carefully... don't short out adjacent pins... put a voltmeter on digital pin 13 (Which isn't, by the way, "pin 13" of the IC.) You should see it going high/ low/ high/....

And it is really easy to connect an LED to the pin, if you don't have a Diecimila. Just be sure to remember to include a suitable resistor.

Pin - to LED - to resistor - to Ground.

So... assuming you've got it working on YOUR Arduino (it's all well and good me saying it works on MINE!), what's so Bad (in the pre-1980 sense, as in "UNsatisfactory") about it? Why not "If it works, use it"?

It is bad because if you don't bother with some of the things you'll learn from these tutorials, although you'll sometimes get little things working, sort of, some of the time, you'll never build big things, and even the medium-sized things you manage will take ages.

It is bad because it doesn't have "comments". These are things that the programmers put into the text of their source code to help them remember what is going on. Comments make no difference to the end result! But they are still important. Revise the first program as follows....

/*MyFirstPrgm
ver 9Feb08

Make pin 13 go on/ off/ on/ off.
Will drive a Diecimila's onboard LED, and/or an external one.
Remember to include current-limiting resistor
      if connecting external one.
*/

void setup()
{
//Every program has one. It happens once.
pinMode(13, OUTPUT);  //Make pin 13 an output
}

void loop()
{
//Every program has one. It happens repeatedly.
  digitalWrite(13, HIGH); // "digitalWrite" is a built in word.
  delay(600);
  digitalWrite(13, LOW);
  delay(600);
}

This illustrates the two ways of embedding a comment.

The first is a multi-line comment. Everything between the "/*" to the corresponding "*/" is a comment. The Arduino compiler will ignore it.

The "//" you see on several lines says "The rest of this line is a comment."

You don't have to have any comments, so you certainly don't have to follow the proactive I am about to recommend... but it is a good idea.

Make the first comment of a program tell you the name the program is saved under. (More on this in a moment!)

Make the second comment of a program start with "ver" or "version", and after that indicate which version of the program this is. The easiest versioning scheme I know is simply to use the date. If half way through a given day you need to "up" the version "number", just add a letter to the version description.

I mentioned saving a program. Windows users would probably assume that they simply use "File | Save"... but no, that's not best. If you do, you will carry on using the default name that the Arduino development environment assigned when you did "File | New". For your first Save, use SaveAs, and give the program a meaningful name. For our example, judging from the first comment, I'd be using "MyFirstPrgm".

Probably because the Arduino isn't limited to nasty old Windows, there are a few things that may surprise Windows users. You can't have a space in a file name. You can't even have a hyphen. But you can have an underscore.

When you've established a sensible name for the program, you can save and re-save, over-writing previous versions, with a simple "File | Save", or by using the corresponding button.

So. Include comments in your code.

In general, they can go just about anywhere that makes sense.... But! Do NOT put one between a "void NAME()" and the "{" following it. Not only will it (sometimes!) cause your program to refuse to compile, but the resulting error message makes no sense to me. Not yet, anyway. For now, when I see an "unreasonable" error message, I look for a comment between a function heading and the first { marking the start of the function's code.

Another thing that is Bad about the program as it stands is that "13" which appears three times. Modify the program as follows....

/*MyFirstPrgm
ver 9Feb08

Make pin 13 go on/ off/ on/ off.
Will drive a Diecimila's onboard LED, and/or an external one.
Remember to include current-limiting resistor
      if connecting external one.
*/

#define ledPin 13  //no ; here

void setup() //Every program has one. It happens once.

{
pinMode(ledPin, OUTPUT);  //Make pin 13 an output
}

void loop() //Every program has one. It happens repeatedly.

{
  digitalWrite(ledPin, HIGH); // "digitalWrite" is a built in word.
  delay(600);
  digitalWrite(ledPin, LOW);
  delay(600);
}

Now everywhere that we refer to the pin where the LED is, instead of the cryptic "13", we have ledPin. (It could have been "thePinWithTheLED", or whatever you need to stay clear about WHICH pin.)

Not only does this make it easier to understand what is going on... wait 'til you have a big program with LEDs on 5 pins!... but it also makes improving or adapting programs easier. Want to change the pin the LED is attached to, say to pin 12? In the Good version of the program, you simply replace one reference to "13" with "12", and, effectively, all through your program everything, and only things, that need changing gets "changed".

A little aside: Semicolons. Yes. You need them. For now: If in doubt, put one in. But! Not after a #define something. In the rather lovely words of the help file, doing so will cause the compiler to "throw cryptic errors further down the page".

Don't worry. Most of the time when you've done something wrong, the compiler will be more helpful that "cryptic... down the page" implies!

Once you have saved your work, as explained above, and connected up your Arduino, press ctrl-U (I.e. hold down the ctrl key, keep it down, press and release the U key, then release the ctrl key. Be quite certain to keep ctrl down until after you release the U key. It is easy to let the ctrl key up sooner than you think you are releasing it.)

The Arduino should say, in the bar below the panel where you entered the program, "Uploading to I/O board". (The "I/O board" is your Arduino hardware). If you have no typos or other errors, the program you've created and transferred to the microcontroller should just start doing its thing. The LED should, in this case, be winking.

If you did have some error in your program, there will be error messages for you in the messages panel, the one below the one you used to enter the source code.

Just keep checking and re-checking until you get the program PERFECT... and then ctrl-U will lead to the desired result!

As you graduate to writing your own programs, one of the skills you need to learn is how to get from "No Program" to "Finished Program" by a series of small steps. By developing in small steps, when something throws up an error message you have a pretty good idea of where the error lies, because you've only made a few more little changes since the last time your program was "working".... well, doing as much as you've built so far, anyway.

Now for some "mere fun" in this first lesson... try changing the "600"s. What happens if you make the first 100 and the second 1200? Can you make the LED do what you want it to do? Get it to wink out the Morse code for "SOS", for instance. (You'll need to add lines to the program... but only lines similar to lines already present.)

I hope you've enjoyed that? Or maybe it was a bit like leaning to swim... not all that much fun. But think some more about that "learning to swim" analogy. Starting to swim can be a bit tedious.... but once you get past the floundering stage, there's a lot of fun to be had!


Okay... play time over....

The following is very important. It may seem a bit esoteric at the moment, but take it onboard as thoroughly as you can.

Every Arduino program will have the following in it, in the following order....

void setup()
{
;
}

void loop()
{
;
}

Everything there was in all of our little programs. Everything there will be in the biggest Arduino program written.

The trick is learning about what you can add to that "skeleton", and the rules about making those additions.

The reason I am laboring these points is that if you keep firmly in mind that there are rules, and you keep the rules in focus, then you will waste much less time than will be the case otherwise.

While the skeleton above is correct, and, literally, everything you see there will be in every Arduino program, the following reduces it even further to a conceptual level....

The Rule: Every Arduino Program Consists of a "Setup" function and a "Loop" function.

There... that wasn't so awful, was it?

The two functions may be supported by further functions. They may also be supported by variable declarations.

If you can cite The Rule, and the preceding paragraph, you know "all about" the language used in the Arduino!

So... what's a "function"?

Again- easy! For now, a function is...

At the risk of sounding like a "Why, why, why..." five year old, I'm now going to ask and answer "What is an identifier?" and "What is a function body". Hang in there.

An identifier is...

Something that.....

... and is not a keyword. (Keywords are the words "built in" to the Arduino, e.g. setup, loop, digitalWrite, #define. You'll meet some... not too many... as we go along. The link from "keyword" above takes you to a comprehensive list.)

So.... "anExample", "an_Example", "example5" are all legitimate identifiers. "an Example" isn't, because of the space in it. "an-Example" isn't because of the hyphen in it. "AnExample" is fine, but it seems in what I've seen so far that Arduino people rarely capitalize the first letter in an identifier name. Not that "anExample" and "AnExample" could both exist in the same program, but it would be a REALLY Bad Idea to use both. To the computer, because it cares about the case of the letters, "anExample" and "AnExample" would be as different as "Chalk" and "Cheese", but I doubt that any human could work on such a program productively.

We're getting there... really... almost done now. All that's left is to say what "the body of the function" is.

It may be a simple statement like...

pinMode(ledPin, OUTPUT)

It may be several simple statements, "glued" together with semicolons, like....




  digitalWrite(ledPin, HIGH); // "digitalWrite" is a built in word.
  delay(600);
  digitalWrite(ledPin, LOW);
  delay(600)

(Note that there is always a semicolon before the closing curly bracket, but, if you look closely, you'll see that is provided for in the rules I've stated.

We haven't met a variable declaration yet... haven't needed one.

There are other things that can be part of the function body, but we'll come to them. They do nothing to upset the "grand scheme" that has been explained to you above.

Any questions?




There ought to be one.....






What about the...

#define ledPin 13  //NO ; after this, remember. Unusual.

... thingie?

Well, that's not really part of the Arduino programming language. What it is is something that the compiler knows about. With a line saying....

#define IDENTIFIER VALUE

... we can say "everywhere I write identifier, I want you, compiler dear, to treat it as if I'd written value when you put the program together.

Now... and this is the last bit, and you don't have to follow it if it doesn't come easily.... the Really Cool thing is that the body of a function may itself consist of stuff very like the overall program. Between the curly brackets, you can have, again, something that looks very like the whole program, although you won't re-encounter the setup and loop functions. But- remember?- the main program can have OTHER functions. It isn't limited to the tow special ones. AND within one or more of the bodies within the functions, there can be further things that look like the whole. In other words, Arduino programs can be fractal, and in other other words, it can be like what you see in a "hall of mirrors"....

But for now don't worry about that last paragraph.... just take my word for it that it's cool.

DO worry about taking onboard the idea that there is a very definite and, essentially, simple structure to every Arduino program. The more you can keep that in mind, and the better you manage to see that structure in the programs you work on, the faster your Arduino programming skills will develop!


Whew! It gets easier... really it does!

When you're ready, you can return to the tutorials list for the next one!


Please also note that I have two other sites, and that the following search will not include them. They have their own search buttons.

My Sheepdog Guides site.
My Arunet site.

Go to the sites above, and use their search buttons if you want to search them.
To search this site....

Search this site or the web powered by FreeFind

Site search Web search
The search engine merely looks for the words you type, so....
*    Spell them properly.
*    Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....

You can also search this site without using forms.
Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.


Want a site hosted, or email? You can also help me if you sign up via this link to 1&1's services. (I wouldn't recommend them unless I was happy after several years as one of their customers, but yes, they do pay me if you use this link! As do the Google advertisers, about whom I know nothing, of course.)



Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Also, I have some of my pages' traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Why do I mention the script? Be sure you know all you need to about spyware.
Editor's Main Homepage
How to email or write this page's editor, Tom Boyd

....... P a g e . . . E n d s .....