Pretty image
You can create your first Arduino electronic project in a matter of minutes.

Arduino is an open source project consisting of both hardware and software. It was originally created to give designers and artists a prototyping platform for interaction design courses. Today hobbyists and experts all over the world use it to create physical computing projects, and you can too.

Although the Arduino is way more than a toy, it’s mostly about fun! We spend so much time in front of our computers every day. The Arduino lets us get hands-on again with computers in a way we haven’t been able to since the 1980s, when you could build your own computer. And Arduino makes developing handcrafted electronics projects ranging from prototypes to sophisticated gadgets easier than ever. Gone are the days when you had to learn lots of theory about electronics and arcane programming languages before you could even get an LED blinking. You can create your first Arduino electronic projects in a few minutes and without needing advanced electrical engineering coursework.

arduino/arduino_explained.jpg

In the first figure you can see an Arduino Duemilanove board and its most important components. With the USB port you can not only power the Arduino, you can also program it, and you can communicate with it serially. Using the digital and analog input and output pins you can connect a great number of sensors and other electronic parts to the Arduino. For example, you can attach temperature sensors, motors, LEDs, or alcohol sensors.

In this article I’ll show you how to take your first steps with an Arduino. You’ll get to know the most important tools first and at the end you’ll build a remote control that you can operate using your computer’s serial port.

The Software You Need

To make it as easy as possible to get started with the Arduino, the Arduino developers have created a simple but useful IDE (Integrated Development Environment). It runs on many different operating systems and before you can create your first projects, you have to install it.

Installing the Arduino IDE on Windows

The Arduino IDE is available for all the latest versions of Microsoft Windows, such as Windows XP, Windows Vista, and Windows 7. Installing the software is easy, because it comes as a self-contained ZIP archive, so you don’t even need an installer. Simply download the archive and extract it to a location of your choice.

Before you install the IDE you have to make sure that the drivers for the Arduino’s USB port are installed. On the most recent versions of Windows this happens automatically as soon as you plug the Arduino into a USB port. Lean back and watch the hardware wizard’s messages pass by until it says that you can use the newly installed USB hardware.

Older versions of Windows don’t find the drivers on Microsoft’s update sites automatically. If the hardware wizard asks you for a path to the right drivers after plugging in the Arduino, point it to drivers/FTDI USB Drivers in the Arduino installation directory.

After the drivers have been installed, you can start arduino.exe from the archive’s main directory by double-clicking it. Follow the instructions on the screen to install the IDE.

Please note that the USB drivers don’t change as often as the Arduino IDE. Whenever you install a new version of the IDE, check if you have to install new drivers, too. Usually, it isn’t necessary.

Installing the Arduino IDE on Mac OS X

The Arduino IDE is available as a disk image for the most recent version of Mac OS X. Download it, double-click it, then drag the Arduino icon to your Applications folder.

Before you can use the IDE with your Arduino you have to install drivers for the Arduino’s serial port. You can find them in the disk image, too, and they are available for both PowerPC Macs (FTDI Drivers for PPC Macs.pkg) and Intel Macs (FTDI Drivers for Intel Macs.pkg). Double-click the pkg file for your platform and follow the installation instructions on the screen.

You can check what kind of Mac you have by clicking the “About this Mac” menu item in the Apple menu at the top left corner of your screen. Chances are good that you have an Intel Mac, because the PowerPC Macs are getting old.

When installing a new version of the Arduino IDE you usually don’t have to install the FTDI drivers again (only when a more recent version of the drivers is available).

Installing the Arduino IDE on Linux

Installation procedures on Linux distributions are still not very homogeneous. The Arduino IDE works fine on nearly all modern Linux versions, but the installation process heavily differs from distribution to distribution. Also, you often have to install additional software (the Java Virtual Machine, for example) that comes pre-installed with other operating systems.

It’s best to check the official documentation and look up the instructions for your preferred system.

A First Look at the IDE

In our next figure you can see what the IDE looks like. It has no advanced features such as a debugger or code completion. You can only change a few preferences, and as a Java application it does not fully integrate into the Mac desktop. It’s still usable, though, and even has decent support for project management. Its toolbar gives you instant access to the functions you’ll need most. Their meaning—from left to right—is:

arduino/arduino_ide.jpg
  • With the Verify button you can compile the program that’s currently in the editor. So in some respects “Verify” is a bit of a misnomer, because the program is not only verified syntactically but also turned into a representation suitable for the Arduino board.

  • The Arduino can communicate with a computer via a serial connection. The Stop button stops the serial monitor.

  • The New button creates a new program by emptying the content of the current editor window. Before that happens, the IDE gives you the opportunity to store all unsaved changes.

  • With Open you can open an existing program from the file system.

  • Save saves the current program.

  • When you press the Upload button the IDE compiles the current program and uploads it to the Arduino board you have connected to your computer.

  • Pressing the Serial Monitor button opens a serial monitor window that allows you to watch the data sent by an Arduino and also to send data back.

The Hardware You Need

Now that we have the drivers and the IDE installed, we need some hardware to build this article’s projects:

  • An Arduino board such as the Duemilanove or the Diecimila.

  • A USB cable to connect the Arduino to your computer.

  • A regular LED.

  • An infrared LED.

  • A 100 Ohm resistor.

  • A breadboard.

  • Some wires.

That’s all we need to get our first projects up and running. You can find these parts at electronic shops such as Makershed, Adafruit, Digikey, Mouser, Radioshack, etc. Sometimes they do not have them all, so you might have to buy from more than one of them. Also it’s often advantageous not to buy a single part but a whole collection. For example, you can often get a whole box of LEDs or resistors for only a few bucks.

<blink>Hello, world!</blink>

To get familiar with the IDE we’ll start with the “Hello, world!” of Arduino programming; that is, we’ll make an LED (light-emitting diode) blink. LEDs are cheap light sources that you can easily integrate into electronic circuits.

arduino/leds.jpg

The LEDs that we need are through-hole parts; you can see some in the figure. They are named through-hole parts because they are mounted to a circuit board through holes. That’s why they usually have one or more long wires: first, you put the wires through holes in a printed circuit board. Then you usually bend, solder, and cut them to attach the part to the board. Where available you can also plug them into sockets as we have them on the Arduino or on breadboards.

The next figure shows how to attach an LED to an Arduino. Connect the short connector of the LED to the ground pin (GND) and the longer one to pin 13.

arduino/plug_in_led.jpg

Connect the USB cable to the Arduino and plug it into one of your computer’s USB ports. Then enter the following code in the IDE’s editor:

Line 1 const int LED_PIN = 13;
- const int PAUSE = 500;
- 
- void setup() {
5  pinMode(LED_PIN, OUTPUT);
- }
- 
- void loop() {
-  digitalWrite(LED_PIN, HIGH);
10  delay(PAUSE);
-  digitalWrite(LED_PIN, LOW);
-  delay(PAUSE);
- }

Let’s see how this works and dissect the program’s source code piece by piece. In the first two lines we define two int constants using the const keyword. LED_PIN refers to the number of the digital IO pin we’re using and PAUSE defines the length of the blink period in milliseconds. Digital pins act as a kind of switch and can be in one of two states: HIGH or LOW. If set to HIGH, the output pin is set to 5 Volts, causing a current to flow through the LED, so it lights up. If it’s set back to LOW, the current flow stops and the LED turns off.

Every Arduino program needs a function named setup and ours starts in line 3. Arduino calls setup once when it boots, and we use it for initializing the Arduino board and all the hardware we have connected to it. We use the pinMode method to turn pin 13 into an output pin. This makes sure that the pin is able to provide enough current to light up an LED. The default state of a pin is INPUT and both INPUT and OUTPUT are predefined constants. See the official documentation.

Another mandatory function named loop begins in line 6. It contains the main logic of a program, and the Arduino calls it in an infinite loop. Our program’s main logic has to turn on the LED connected to pin 13 first. To do this, we use digitalWrite and pass it the number of our pin and the constant HIGH. This means the pin will output 5 Volts until further notice, and the LED connected to the pin lights up.

The program then calls delay and waits for 500 milliseconds doing nothing. During this pause, pin 13 remains in HIGH state and the LED continues to burn. The LED is eventually turned off when we set the pin’s state back to LOW using digitalWrite again. We wait another 500 milliseconds, and then the loop function ends. The Arduino starts it again and the LED blinks.

Before you compile and upload a program to the Arduino you have to configure two things in the IDE: the type of Arduino you’re using and the serial port your Arduino is connected to.

Identifying the Arduino type is easy, because it is printed on the board. Popular types are Duemilanove, Diecimila, Nano, Mega, Mini, NG, BT, LilyPad, Pro, or Pro Mini. In some cases you also have to check what microcontroller your Arduino uses. Most Arduinos use an ATmega168 or an ATmega328; you can find the microcontroller type printed on the microcontroller itself. When you have identified the exact type of your Arduino, choose it from the “Tools -> Board” menu.

Now you have to choose the serial port your Arduino is connected to from the “Tools -> Serial Port” menu. On Mac OS X, the name of the serial port starts with /dev/cu.usbserial (on my MacBook Pro it’s /dev/cu.usbserial-A60061a3). On Linux systems it should be /dev/ttyUSB0, /dev/ttyUSB1, or something similar depending on the number of USB ports your computer has.

arduino/device_manager.jpg

On Windows systems it’s a bit more complicated to find out the right serial port, but it’s still not difficult. Go to the device manager and look for a “USB Serial Port” below the “Ports (COM & LPT)” menu entry (see illustration). Usually the port is named COM1, COM2, or something similar.

After you have chosen the right serial port, click the “Verify” button and you should see the following output in the IDE’s message area (the Arduino IDE calls programs “sketches”):

 Binary sketch size: 888 bytes (of a 14336 byte maximum)

This means that the IDE has successfully compiled the source code into 888 bytes of machine code that we can upload to the Arduino. If you see an error message instead, check if you have typed in the program correctly.

While modern computers load programs from a hard drive, microcontrollers usually have to be programmed. That means you have to load your software into the microcontroller via a cable, and once the program has been uploaded it stays in the microcontroller until it gets overwritten with a new program. Now press the “Upload” button and after a few seconds you should see the following output in the message area:

 Binary sketch size: 888 bytes (of a 14336 byte maximum)

This is exactly the same message we got after compiling the program, and it tells us that the 888 bytes of machine code were transferred successfully to the Arduino. In case of any errors, check if you have selected the correct Arduino type and the correct serial port in the “Tools” menu.

During the upload process the TX and RX LEDs will flicker for a few seconds. This is normal and it happens whenever the Arduino and your computer communicate via the serial port. When the Arduino sends information it turns on the TX LED. When it gets some bits it turns on the RX LED. Because the communication is pretty fast, the LEDs start to flicker and you cannot identify the transmission of a single byte (if you can, you are probably an alien).

As soon as the code has been transmitted completely, the Arduino executes it. In our case this means that the LED starts to blink. It turns on for half a second, then it turns off for half a second, and so on. Congratulations! You have just finished your first physical computing project!

Making LEDs blink is fun, but it’s more or less useless, right? Not if you let the right LEDs blink at the right frequency. If you replace the regular LED by an infrared LED and make it blink at the right frequency you can use it to control nearly every remote-controllable device in the world. So now I’ll show you how to control Apple’s Front Row with an Arduino. You can generalize this project to other kinds of remotes.

Build Your Own Remote Control

To control a device such as a TV set wirelessly you need a sender and a receiver. The receiver usually is built into the device to be controlled and the sender is part of a separate remote control. Although you can choose from a variety of technologies such as Bluetooth or WiFi, most modern remote controls still use infrared light for communication.

In the next figure you can see a circuit diagram for a simple infrared sender. You might ask yourself why we could not simply replace the LED from our “Hello, World!” example with an infrared LED and why we have to use a resistor, a breadboard, and some wires now.

arduino/ir_sender.jpg

There are many reasons for this. First of all, we no longer can use pin 13, because the library we’re using later on expects the infrared LED to be connected to pin 3. The distance between pin 3 and the next ground pin is too big for regular LED connectors, so you cannot directly plug the LED into the Arduino board. So we have to use a breadboard and some wires.

Why do we have to add a resistor, and what is a resistor? A resistor limits the amount of current that flows through an electric connection. In our case it protects the LED from consuming too much power, because this would destroy the LED. You always have to use a resistor when powering an LED! By the way: when we connected the LED directly to the Arduino we did not have to use a resistor, because pin 13 comes with an internal one.

Connecting parts such as LEDs directly to the Arduino is only an option in the most trivial cases. Usually, you will prototype your projects on a breadboard that you connect to the Arduino. A breadboard “emulates” a circuit board. You don’t have to solder parts to the board; instead, you can simply plug them into it.

Although the circuit looks more complicated now, you shouldn’t worry. Working with breadboards is very easy. They come in various types and sizes, but they all work the same way. They have a lot of sockets that you can use for plugging in through-hole parts or wires. That alone wouldn’t be a big deal, but the sockets are connected in a special way. In the next figure you can see how.

arduino/breadboards_flow.jpg

As you can see, most sockets are connected in columns. If one socket of a column is connected to a power supply, then automatically all the other sockets in this column are powered, too. On the bigger board in the photo, you can also see four rows of connected sockets. This is convenient for bigger circuits. Usually, you connect one row to your power supply and one to the ground. This way you can distribute power and ground to any point on the board.

So build the circuit, that is plug all wires and parts into the breadboard and into the Arduino. The direction of the resistor is irrelevant, but be careful with the LED. Connect its longer connector to the wire that goes into pin 3. Now we only need some software to bring our remote control to life!

The Remote Control Software

Using infrared light for transmitting signals has several advantages. It is invisible for human beings, so it does not disturb you. Also you can generate it cheaply with infrared LEDs that can be integrated easily into electronic circuits. So, for many purposes such as controlling devices in a typical household it’s an excellent choice.

But it also has some drawbacks. For example, it does not work through walls or doors and the distance between the remote control and the operated device is fairly limited. Even more important: the infrared signal is subject to interference with other light sources.

To reduce possible distortions caused by other light sources to a minimum the infrared signal has to be modulated. That means that you turn the LED on and off at a certain frequency, usually somewhere between 36 KHz and 40 KHz.

In practice it’s complicated to build a robust infrared remote control. The biggest problem is that many vendors invented countless incompatible protocols. They all use different frequencies and they all interpret data differently. For example, some interpret “light on” as a 1 bit while others treat it as 0. Also they all define their own commands that have different lengths, too.

We could try to generate the infrared signals ourselves, but that’d be tedious and error-prone. It’s better to use the existing implementation in the IRremote library. It hides all the nasty details and supports the most popular infrared protocols.

Many libraries for many different purposes are available already for the Arduino. Installing them is easy, because usually you only have to unpack a ZIP file and copy it to a certain folder. So download and extract the IRremote library file, copy the directory IRremote to ~/Documents/Arduino/libraries (on a Mac) or My Documents\Arduino\libraries (on a Windows box). Then restart your IDE.

Now we will use the IRremote library to create our own AppleRemote class that encapsulates all the gory remote control protocol details. The class looks like this:

 #include <IRremote.h>
 
 class AppleRemote {
 
  enum {
  CMD_LEN = 32,
  UP = 0x77E15061,
  DOWN = 0x77E13061,
  PLAY = 0x77E1A05E,
  PREV = 0x77E1905E,
  NEXT = 0x77E1605E,
  MENU = 0x77E1C05E
  };
 
  IRsend mac;
 
  void send_command(const long command) {
  mac.sendNEC(command, CMD_LEN);
  }
 
  public:
 
  void menu() { send_command(MENU); }
  void play() { send_command(PLAY); }
  void prev() { send_command(PREV); }
  void next() { send_command(NEXT); }
  void up() { send_command(UP); }
  void down() { send_command(DOWN); }
 };

As you can see the Arduino actually understands C++. People sometimes seem to be a bit irritated when it comes to the programming language they have to program the Arduino in. That’s mainly because the typical sample sketches look as if they were written in a language that has been designed for programming the Arduino. But that’s not the case; it is C++ (which implies that it supports C, too).

The code starts with an enumeration that contains all the constants we need; that is, the length of each control code (Apple Remote codes are 32 bits long) and the control codes themselves. I have used an infrared sensor and an original Apple Remote to detect the control codes. To learn more about this process have a look at my upcoming book Arduino: A Quick-Start Guide.

After the enumeration we define an IRsend object named mac that we’ll use to send commands using the send_command method. send_command uses IRsend’s sendNEC method because the Apple Remote uses the NEC protocol. This is only one of countless infrared protocols and for every remote control you want to emulate you have to check which protocol it uses (IRremote supports NEC, Sony, RC5, and RC6).

Now that we have established the basis we can implement all commands with a single function call, so implementing methods such as menu, play etc. is a piece of cake.

Using the AppleRemote class is easy, too. In the following sketch we use it to control a Mac via a serial port:

 AppleRemote apple_remote;
 
 void setup() {
  Serial.begin(9600);
 }
 
 void loop() {
  if (Serial.available()) {
  const char command = Serial.read();
  switch(command) {
  case 'm':
  Serial.println("menu");
  apple_remote.menu();
  break;
  case 'u':
  Serial.println("up");
  apple_remote.up();
  break;
  case 'd':
  Serial.println("down");
  apple_remote.down();
  break;
  case 'l':
  Serial.println("prev");
  apple_remote.prev();
  break;
  case 'r':
  Serial.println("next");
  apple_remote.next();
  break;
  case 'p':
  Serial.println("play");
  apple_remote.play();
  break;
  default:
  break;
  }
  }
 }

We define a global AppleRemote object named apple_remote and in the setup function we initialize the serial port. We set it to a baud rate of 9,600 calling the begin method of the Serial class. This is a standard class that comes with the Arduino software. It allows you to easily send and receive data via a serial port, that is the USB port you have connected the Arduino to.

In loop we wait for new data on the serial port calling available. Whenever a new byte arrives we read it by calling read and then we check if it’s one of the characters m, u, d, l, r, or p. Depending on the character we have received we send the control code for menu, up, down, previous, next, or play accordingly. Also we output the name of the key we’re emulating to the serial port.

arduino/serial_remote.jpg

Compile and upload the sketch and you can control a Mac using a serial terminal. You could use any serial terminal such as screen or Putty, but we’ll use the one that comes with the Arduino IDE. Click the right-most icon in the IDE’s toolbar. An empty serial monitor window will open. It allows you to send data to the Arduino. You only have to enter some text and press the “Send” button.

In the next figure you can see how the serial monitor looks after I have navigated to my favorite playlist in Front Row. I had to enter m, d, d, p, d, d, d, d, d, d, d, d, d, d, p, p. Don’t forget to press the “Send” button or hit the return key after entering every character.

So point the infrared LED to your Mac (you can bend it to a right angle, if you want to) and enter some commands. If it doesn’t work you should reduce the distance between your Mac and the LED. Also you should unpair any other remote controls in the security area of the Mac’s system preferences menu.

Where to Go From Here

In this article we could only scratch the surface of the exciting field of physical computing. Still you should know now what the Arduino project is all about. With minimal effort and costs you can build your own electronic gadgets that might even serve a useful purpose.

Making LEDs blink is only the beginning and you can build quite sophisticated projects ranging from toys to robotics or home automation. Also there are countless extension shields available for the Arduino. They make it extremely easy to add Ethernet or even an OLED touch screen to your project.

So the next time you think “It’d be cool if someone would build this,” just do it! Build it!

Maik Schmidt has worked as a software developer for more than fifteen years, creating solutions for large enterprises. He frequently writes book reviews and articles and is the author of Enterprise Recipes with Ruby and Rails and Enterprise Integration with Ruby. He is finishing Arduino: A Quick-Start Guide as this issue goes to press.

Send the author your feedback or discuss the article in the magazine forum.