Create Your First Arduino Program

Write your first program and watch it work! In this excerpt from Arduino Programming in 24 Hours, Sams Teach Yourself, learn how to use the Arduino IDE software package to create, compile, and upload an Arduino program. You then learn how to interface your Arduino with external electronic circuits to complete your Arduino projects.

Now that you’ve seen what the Arduino is and how to program it using the Arduino IDE, it’s time to write your first program and watch it work. In this hour, you learn how to use the Arduino IDE software package to create, compile, and upload an Arduino program. You then learn how to interface your Arduino with external electronic circuits to complete your Arduino projects.

Building an Arduino Sketch

Once you have your Arduino development environment set up, you’re ready to start working on projects. This section covers the basics that you need to know to start writing your sketches and getting them to run on your Arduino.

Examining the Arduino Program Components

When you use the Arduino IDE package, your sketches must follow a specific coding format.

This coding format differs a bit from what you see in a standard C language program.

In a standard C language program, there’s always a function named main that defines the code that starts the program. When the CPU starts to run the program, it begins with the code in the main function.

In contrast, Arduino sketches don’t have a main function in the code. The Arduino bootloader program that’s preloaded onto the Arduino functions as the sketch’s main function. The Arduino starts the bootloader, and the bootloader program starts to run the code in your sketch.

The bootloader program specifically looks for two separate functions in the sketch:

setup

loop

The Arduino bootloader calls the setup function as the first thing when the Arduino unit powers up. The code you place in the setup function in your sketch only runs one time; then the boot-loader moves on to the loop function code.

Just place the code you need to run at startup time inside the setup function code block.

After the bootloader calls the setup function, it calls the loop function repeatedly, until you power down the Arduino unit. The loop function uses the same format as the setup function:

void loop() {
code lines
}

The meat of your application code will be in the loop function section. This is where you place code to read sensors and send output signals to the outputs based on events detected by the sensors. The setup function is a great place to initialize input and output pins so that they’re ready when the loop runs, then the loop function is where you use them.

Including Libraries

Depending on how advanced your Arduino program is, you may or may not need to use other functions found in external library files. If you do need to use external libraries, you first need to define them at the start of your Arduino program, using the #include directive:

#include <library>

The #include directives will be the first lines in your sketch, before any other code.

If you’re using a standard Arduino shield, most likely the shield library code is already included in the Arduino IDE package. Just choose Sketch > Import Library from the menu bar, and then select the shield that you’re using. The Arduino IDE automatically adds the #include directives required to write code for the requested shield. For example, if you select the Ethernet shield, the following lines are imported into the sketch: