Set up the Integrated Development Environment to Talk to your Arduino

Tell the IDE what communications port to use. Use the/dev/tty.usbmodem**** port, where **** is any 4 digit number.

Select the tty.usbmodem port.

Open the Blink example code.

Open the Blink code.

Test your Arduino

Upload the Blink code to the Arduino by clicking the the Upload button.

The amber light on the Arduino should be blinking now.

Unplug the Arduino from the computer, and connect it to a power supply. The amber light should still be blinking, even when the Arduino is not connected to a computer.

Understanding What the Arduino is Doing

The Arduino is a very small, very low power computer, designed and built to be easy to use.

It only does two things:

When you first give it power, it goes through a setup routine.

Thereafter, as long as it has power, it runs whatever instructions are in the loop.

The two things an Arduino program does.

Arduino programs require the two functions, setup() and loop(). A function runs when you ask it to, and runs whatever instructions are between the {} curly braces. If the function doesn’t send any information back when it finishes, we say that it “returns nothing”, and we write “void” in front of it to let the compiler know not to expect anything back.

The Compiler

The Arduino doesn’t run the English language code we write. The IDE includes a program called a compiler that takes all the instructions written, translates them into machine code, and compiles them into an orderly list of machine codes so that the Arduino knows what to do.

The first compiler was written by Rear Admiral Grace Hopper in 1952. “Nobody believed that,” she said. “I had a running compiler and nobody would touch it. They told me computers could only do arithmetic.” In 1951 she discovered the first computer “bug.” It was a real moth, which she pasted into the UNIVAC I logbook.<source>

So any time you run any program on any computer, other than machine-coded plain arithmetic, thank Grace Hopper. Every time you push the Check or Upload button, remember she invented the very idea of them!

Rear Admiral Grace Hopper, USN, inventor of the Compiler.

Code Fundamentals

Here’s the Blink code you just compiled and uploaded to the Arduino. [[this has been updated in the new IDE release. Update this section to match]]

Blink example code.

[[brute-force-blink would be a good code to have here. the code with the fewest characters, as dead-simple as possible]]

Anything in grey is a comment. Comments are just for you, the complier ignores them, because they don’t actually tell the Arduino to do anything.

// makes the rest of a line into a comment

/* anything between these marks, for however many lines, is a comment */

Any lines before void setup() are general instructions to the compiler. In this case we have one line:

int led = 13;

We declare to the compiler that we want a variable that we will call by the name “led”. A variable stores information for us. We tell the compiler what kind of information we intend to store in this variable by declaring a type of variable, in this case, int, which is short for integer. An integer is a whole number, no fractions or decimals or letters or symbols will be allowed. We take the value 13 and store it in “led” by using a single equals sign to force 13 into “led”. We end the line with a semicolon ; This is hugely important, as the compiler relies on your to tell it where you want the line to end.

The next line starts our first of two mandatory functions, setup(). setup() does not return anything, so it is the “void” type of function. We declare it to the compiler as

void setup()

The pair of parentheses () tell the compiler that setup() is a function, and that we are not sending it any information when we call it to action. setup() tells the compiler to run its contents only once, when the power comes on.

Inside the curly braces {} of setup() we have

pinMode(led, OUTPUT);

pinMode() is a function that we send two pieces of information to. We send a pin number on the Arduino board, and the mode we want to set it to. The pin number in this case is 13, which we stored in “led” earlier. The OUTPUT mode tells the Arduino that it can set the voltage on that pin, so it can put out either 0V (LOW) or 5V (HIGH). Once you tell an Arduino what mode to set for particular pin, it remembers. We only need to set the pinMode() once, so we include it in setup().

We declare another mandatory function, loop(). This is the function that runs forever, as long as there is power.

void loop()

In loop(), we have two other functions, digitalWrite() and delay(). These functions are already known to the compiler, so we don’t have to declare a type.

digitalWrite(led, HIGH);

tells the Arduino to set pin 13 (or whatever pin number we stored in “led”) to HIGH, which is 5V.

That’s it. Now you know how to read a simple Arduino program, so you can figure out what it will do when it is running on the Arduino.

Improving on Blink: Good Coding Practices

Good code is easy for the programmer to read. Well written code tells you most of what you need to know without comments. Here’s a version of the Blink example code. It does the same thing, so the Arduino doesn’t care which one it gets. It follows better coding practice in that it is easier to understand and harder to break.

You’ll need to unzip it, and then open it within the IDE. It will complain that it needs to be in a folder. Click OK to create the folder.

Blink example code, rewritten to be easier to understand.

Kit Transue defines good code as follows:

A. it should do what you want it to

B. it should be clear

C. it should be prepared to evolve

A. You can help your code do what you want it to by making it clear to the compiler what you intend. If you want to store a number that shouldn’t change, declare it as a constant to the compiler. That way, if you, or someone else, tries to change it later, the compiler will object.

int const variablename

declares variablename to be a constant.

B. You can make your code more clear by creating variable names that are meaningful. I think that “led” is misleading, as you might use it to drive something else, and it isn’t really an led, it’s just an output pin that controls one.

int const output_pin

is easy to read, meaningful, and protected against accidental change.

C. You can prepare your code for later improvements by defining things in one place, so that if you find you are wrong, you don’t have to go searching for all the places in the code that you are wrong. You can fix it in that one place. Depending on the design of your output circuit, a HIGH might turn on the actual LED in the circuit, or a LOW might turn it on. Here I declare

int const on = HIGH;

and

int const off = LOW;

This way, if I find that I’ve got them reversed, I can change them each just once.

When I write

digitalWrite(output_pin, on);

it’s pretty clear that I want whatever is attached to that output pin to be on.

I’ve also named the interval variables so that the name tells me what units they carry, and separated them into high_interval_ms and low_interval_ms, so I have more flexibility later.

Writing your own Code

You can certainly copy and paste other people’s code. It’s a good way to learn.

If you want to be a good Arduino programmer, you must learn to write your own code, by hand, from scratch, to get command of the language. You will have a better understanding of what works and what doesn’t, what is easier to understand and what is less so, and what makes for code that is easier to change later.

Code Reference

The Arduino group maintains a very useful website devoted to understanding, programming, and using Arduino type microcomputers.

This part of the website tells you all the commands that you can use to program an Arduino.

You should always make a schematic for each of your circuits. You can do it on paper, or in a drawing package, but there is no substitute for a schematic for recording what you intend the circuit to do.

Test probe connected to Arduino Uno.

If you have installed the test probe in the correct polarity, connected to the correct pins, the LED in the test probe will blink.

Congratulations! You have now altered some code to make the Arduino control an external circuit.

You have created a voltage that varies with time, sometimes on, and sometimes off.

Since you only have two choices, on or off, you might think that you can’t have any output that is in between on and off. And you would be right. But maybe we can fake it.

Pseudo-analog Output

The Arduino outputs are digital. They are either on, at 5V, or off, at 0V. Suppose you want something that is only halfway on? You already know how to turn it on and off repeatedly, from the Blink example. What would happen if you turned it on and off so fast that it looked like it was halfway on? Would that be good enough? It wouldn’t actually be an analog output, but it would look like it, so we call it Pseudo-Analog. Let’s try it.

Any of the output pins on an Arduino that have a ~ before the number are capable of pseudo-analog output.

We use the analogWrite function to make the output turn rapidly on and off, so fast that we can’t tell it is blinking. If it spends most of the time on, it is bright. If it spends most of the time off, and only comes on for very short amounts of time, it is dim. So long as it flashes much faster than our eyes can respond, it looks just like it is continuously lit at different brightnesses.

Upload the code to the Arduino, and try it out. Now try entering different values for high_brightness and low_brightness. The values can be 0 to 255.

The amount of time the output is on we call the pulse width, which is really a pulse duration. When you change the pulse width, you change the brightness. We call the ability to change something modulation, and since we are doing it by changing the pulse duration we all it pulse-width-modulation, or PWM.

Programming a Trinket

The Trinket mini-Arduino by Adafruit has some unusual programming requirements. See this post.