Programming the Arduino

As we have seen, the Arduino IDE is free and can be downloaded from their site. Detailed instructions for installing and configuring the IDE can be found here.

What’s an IDE?

An IDE, or integrated development environment, is a software application that allows you to write code and test that code out in the programming language the IDE supports.

If you have experience programming, you may have used another IDE to write, test, debug, and turn your code into something the computer understands. If you haven’t, the Arduino IDE is a good place to start, as it is relatively simple and easy to understand.

ch3-codeflowchart-01

The Arduino team has designed an IDE for use with their devices that has the features you need. It has a built-in code editor, which is a program used to write the code files that you create when programming. You can test your code in the IDE and solve problems with the help of a message area that shows errors in your code and a console that provides more detail about the nature of these errors. It has buttons so you can check your code, save it, create a new code window, upload it to your Arduino and more.

ch3-blank-sketch-labelled-01-1

The sketch

In the Arduino IDE, your program is called a sketch. The sketch is the basic unit of Arduino programming. The Arduino IDE supplies example sketches, that cover many of the things you can do with an Arduino. Let’s open, save, then upload a simple sketch called Blink.

First, connect your computer to the Arduino with a USB A-B cable. Open up the Arduino IDE. When it is open, go to the File Menu > Examples > 01.Basics > Blink. This will open up the Blink Sketch.

ch3-menus-blink-sketch-01

Let’s take a look at the buttons in the IDE, at the top of the sketch. The verify button checks your code for errors, the Upload button sends your code to the Arduino, New creates a new code window, Open opens a previously saved sketch and Save saves your sketch.

ch3-buttons-labelled

Here’s a screenshot of the Blink sketch, with the three main sections annotated.

Comments are used to write notes to anyone who might read your code. This might be you, when you return to a sketch you created earlier, or others who you are sharing your code with. Comments have no effect whatsoever on how the computer interprets the text and are only there to remind you or clue someone else in on what you intended to do, or how the program functions as a whole.

The setup() function is where you set initial conditions for your code. setup() happens only one time, when you run the code or turn on your Arduino.

The loop() function is where you put code that you want to run over and over again.This is generally where all of your code that creates functionality lives. We will see, for example in the Blink sketch, the code that turns the LED on and off is here.

blink-sketch-01

Save and rename the sketch!

After you’ve taken a look at the Blink Sketch, save it and give it a new name. File Menu > Save As > MyBlink for example. You don’t want to write over the example files and also you want to make adjustments to the example file to understand how the code is working. Saving early and often is always a good practice.

Verify and Upload

Let’s verify the sketch, then upload to the Arduino. Click the Verify button to check your code. Even though this is an example sketch, it is good to get in the habit of checking your code. There will be a message in the bottom of the window that tells you a bit of information about your sketch. If there was a problem, it would also note it here. After verifying, you can upload your code, Make sure your Arduino is attached to the computer and that you have configured it properly. Then press the upload button. The LED on the Arduino near pin 13 should start blinking on and off.

ch3-verify-upload-sketch-01-1
ch3-uno-usb

setup() and loop(): the guts of the code

Now let’s look a bit closer at setup() and loop(). We have seen that setup happens once and loop happens over and over again.

ch3-program_flowchart-large-01

Inside of the setup() function, there is a line of instructions to the Arduino. It tells the Arduino to set the pin on the Arduino that is attached to the built-in LED on the board to be an output. In other words, pinMode() tells the Arduino to set LED_BUILTIN (pin 13 on the Arduino Uno) to be an OUTPUT.

setup-function-01

If we look at loop(), we see several more lines of instructions inside the function, These lines of code are what is turning the LED at pin 13 on and off. digitalWrite() sets LED_BUILTIN to HIGH, turning on the LED. delay() pauses the Arduino for 1000 milliseconds, or one second. The next line, digitalWrite(), sets the LED_BUILTIN LOW, or turns off the LED. This is followed by another delay(), which pauses the Arduino for 1000 or one second.

loop-code-blink-01

Try adjusting the amount of time that the LED is on and off. How do do that? Create a new pattern, perhaps a short time on, a long time off, a long time on. How would you do an SOS signal?