3. Hello, world. (a.k.a. Blink)


When you learn a new programming language, your first program is “Hello, world,” in which you make your computer print that global greeting to the screen.

In this class, we assume that you have a background in Python programming, so you know how to write the Hello, world program in Python.

[1]:
print("Hello, world.")
Hello, world.

We will now start exploring the Arduino IDE by writing the “Hello, world” of programmable electronics, blink.

Hardware for your first exercise

We will first discuss the three additional hardware components you need.

LEDs

In your kit, you will find many red light-emitting diodes (LEDs). An LED is a component that permits current to flow through it in one direction and emits light when it does. Here is a picture of one with a clear cover, though the ones in your kits will mostly have colored covers.

led

Because of the directionality of the diode, it is important that the LED is properly oriented. LEDs typically have one long lead and one shorter lead. The long lead is the anode, which you connect to the higher voltage, and the shorter lead is the cathode, which you connect to the lower voltage (ground). In drawings of LEDs, the long lead is typically shown as a bent wire.

You should also control how much current goes through an LED so you do not destroy it. LEDs are rated with a maximum forward current, which is the maximum continuous current an LED can safely pass. For many LEDs, including those in your kit, the forward current is 25 mA. They also have a forward voltage, which is the voltage drop across the LED. For the LEDs in your kit, the forward voltage is about 1.8 V. The maximum voltage that Arduino can provide is 5 V, so if you are powering your LED with Arduino, you will hit the forward current with a resistance of about 130 Ω by Ohm’s law. As such, if you use 220 Ω resistors with the LEDs in your kit, they should not burn out.

Resistors

You also have lots of resistors in your kits. Resistors contain insulating material that restricts the flow of electrons through them. Different resistors can have different composition, which will lead to different degrees of effectiveness in restricting current, or resistance, which is measured in ohms (Ω).

Resistors are color-coded for their resistance. The color-coding works in either four-, five-, or six-band codes. The resistors in your kit are either four- or five-band. Here is a picture of several four-band resistors (Evan Amos, public domain).

resistors

It can be hard to determine the direction in which to read the bands. In many resistors, the outer band that is closest to its respective lead is the first. In some four-band resistors, there is a larger gap between the third and fourth band. Finally, if there is a gold or silver band, it must be the last band.

Each color corresponds to a number, as follows.

  1. Black

  2. Brown

  3. Red

  4. Orange

  5. Yellow

  6. Green

  7. Blue

  8. Purple

  9. Gray

  10. White

Additionally, pink, silver, and gold are respectively –3, –2, and –1.

To read the value of resistance from the color bands for a four-band resistor, do the following.

  1. The color of first band is the first digit.

  2. The color of the second band is the second digit.

  3. The color of the third band gives a multiplier as a power of 10. For example, if the color were orange, the multiplier is 10³.

  4. The color of the fourth band gives the tolerance of the resistor. Here the number-to-digit system is not used. Gold and silver, meaning ±5% and ±10%, respectively, are the most often encountered colors of the fourth band.

So, to get the resistance, form a number with the first two bands, then multiply by the multiplier given by the third band. For example, a red-red-brown-gold banded resistor is 200 Ω with a ±5% tolerance.

To read the value of resistance from the color bands for a five-band resistor, do the following, very similar procedure.

  1. The color of first band is the first digit.

  2. The color of the second band is the second digit.

  3. The color of the third band is the third digit.

  4. The color of the fourth band gives a multiplier as a power of 10.

  5. The color of the fifth band gives the tolerance of the resistor.

The 220 Ω resistor in the four-band example would have rings of red-red-black-black-gold.

For reference, below is a diagram of resistor color codes (Adim kassn, CC BY-SA 3.0 licensed). To learn more about the color coding, see the Wikipedia page. For a handy color chart and resistance calculator, check out Digi-Key’s utility.

Color code chart for resistors

Thinking exercise 1: Resistor values

  1. What four-band color pattern would I have for a 1±0.05 MΩ resistor? How about five-band?

  2. I have a resistor with band colors yellow-purple-red-gold. What is its resistance?

  3. I have a resistor of with band colors brown-black-black-brown-brown. What is its resistance?

The answers are at the end of this lesson.


Jumper wires

Jumper wires, also called jump wires, are plastic-coated wires that are used to connect components beyond the connections already present in the solderless breadboard. Jumper wires have different ends. The vast majority of jumper wires end in bare wires. Some specialized jumper wires have plastic ends that force the wire to rise orthogonally from where it is plugged in. Some of these special-ended jumper wires have male ends (exposed wire), or female ends (port for inserting wire).

Arduino sketches

Finally, before launching into the blink exercise, we need to discuss Arduino sketches generally. Arduino sketches are written in C++. If you know C or C++, the syntax will be familiar to you. If not, we will discuss syntax and concepts along the way as we work through the lessons.

Every Arduino sketch minimally has two functions, setup() and loop(). Upon upload of your sketch (or upon pressing the reset button on the board), the setup() function is run once. Then, the loop() function is run. When it finishes running, it runs again. And again and again and again. It keeps running over and over again until you upload a new sketch, press the reset button, or disconnect the Arduino. So, a minimal Arduino program is as follows.

void setup() {
}

void loop() {
}

It literally does nothing.

The content of the function, or any function in C++, should be contained in braces. When the brace that opens the code in a function is closed by a brace, that is the end of the function.

Note that void appears before both function definitions. This tells the compiler that these functions do not return anything. Note further that neither function takes any arguments. This must be the signature for setup() and loop() in any Arduino sketch.

Outside of setup() and loop(), you can define other functions and global variables and constants.