Learning to Program the Arduino

This article should acquaint you with basic Arduino programming and show you how to write programs that interact with objects in the real world. (A mandatory disclaimer: the last time I really studied electronics was way back in high school, so this article focuses more on the programming aspects, rather than the electronic side of things.)

Physical Computing

Before I start talking about this really cool thing called Arduino (Italian for "good friend"), let me say a few things about the fascinating subject of physical computing. Physical computing has been defined in various ways, but the central idea seems to be the same: physical computing is concerned with developing software that interacts with the world beyond the host computer through a combination of hardware and software—it's aware of the world, so to speak. Such awareness makes these applications capable of sensing external events and responding to them in a predefined way. This is accomplished by the use of sensors and actuators (which I describe next).

Actuators and Sensors

The Arduino interacts with the world through actuators and sensors. Sensors are electronic components that describe the world to your application. One common way sensors work is that their electrical properties change (in a mathematically known way) as an effect of the changes in the conditions in which it's operating. For example, the resistance of a photo-resistor changes when the intensity of light incident on it changes. Thermistors are another example of such sensors whose resistance changes when the operating temperature changes. A flex sensor is a different category of sensor, where the resistance changes depending on the extent of the flex or "bend". Such changes can be read as electrical signals on the Arduino's input pin. Depending on the kind of sensor, the signal either can be digital (on or off) or analog (a continuous stream of values). The latter part of this article shows how to work with analog sensors.

Actuators, on the other hand, are electronic components that are used to react to an external event. For example, when it gets dark, the light should be switched on. Sensors and actuators, thus, are used to achieve complementary objectives: one senses, and the other reacts. Examples of actuators are solenoids and servos. Later in this article, I explain how to control a servo using Arduino.


The Arduino is an open-source electronics prototyping platform composed of two major parts: the Arduino board (hardware) and the Arduino IDE (software). The Arduino IDE is used to write the program that will interact with your Arduino and the devices connected to it. In the Arduino world, such a program is called a sketch, which has its origin in its mother language, Processing (see Resources).

The Arduino board is a small-form microcontroller circuit board. At the time of this writing, a number of Arduino boards exist: Arduino UNO, Nano, Mega, Mini, Pro and others (see Resources for a complete list). The Arduino UNO (Figure 1) is the latest version of the basic Arduino board, and you need one of these to follow this article (see Resources for the UNO's detailed specifications).

Figure 1. Arduino UNO (Courtesy of http://arduino.cc/en/Main/ArduinoBoardUno)

Besides the UNO, you need the following hardware to work through this article:

  1. Breadboard to set up the circuit.

  2. Some LEDs.

  3. Resistors: 330 Ohm (at least as many as LEDs), 10 kOhm resistors.

  4. Continuous rotation servo (SpringRC SM-S4303R continuous rotation servo: http://www.robotgear.com.au/Product.aspx/Details/482).

  5. Flex sensor.

  6. Linear potentiometer.

  7. Connecting wires.

One excellent way to get started with Arduino is the Arduino starter kit from Sparkfun. This starter kit contains all the hardware and more that you need to follow this article (except the servo).

If you haven't already opened up your Arduino and plugged it in to your USB port, plug it in. For the purposes of this article, it will be sufficient to use the power supply via the USB connection. If you connect more devices, you will need to connect an external supply.

You will program the Arduino in a language that looks very similar to C and is based on Processing. You can download the Arduino IDE from the Arduino Project Web site.

Arduino IDE

As you might guess, the IDE is as always the front end. The real pieces are the compilers, linkers and libraries that need to be present to communicate and program the AVR microcontroller-based Arduino. Depending on your Linux distribution, the exact names of the packages will vary, so I just list the software by name here:

  • The GNU C and C++ compiler for AVR.

  • AVR binutils.

  • AVR libc.

  • avrdude (a program for uploading code to the microcontroller board).

  • rxtx (for serial communication).

Once these packages are installed, fire up your Arduino IDE. Take a moment to explore the IDE. The buttons for compiling (verifying) and uploading the sketch are the important ones.

The communication between your computer and the Arduino will be via the USB cable that has been packaged with your Arduino board. Once you plug the USB cable in to your computer (with the other end plugged in to the Arduino board), it should show up in the Arduino IDE under Tools→Serial Port as /dev/ttyACMx. If you have more than one USB serial device communicated, be careful to select the correct one. You need to set up user permissions correctly to access the serial port (see Resources for distribution-specific instructions).



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Really interesting

marksen's picture

Arduino is a really intersting project because its way to use are limitless. It can be adapted to many things and it's quite easy to program.

I love the Arduino! Thanks

Pit's picture

I love the Arduino! Thanks for the interesting article.

Processing, C/C++ and Arduino

SomeTom's picture

...a language that looks very similar to C and is based on Processing.

Well, I'd rather say it's a language that looks like Processing (Arduino is using the Processing IDE) but is based on C/C++. There's AVR's version of gcc under the hood, while Processing is running on top of Java.
Although the simplified C/C++ inside the IDE is looking mostly like Processing, Arduino really is not Processing at all. If you used to play around with Processing before you tried to code some Arduino you might make some weird experiences, as it really seems to be the same - until you try to try to initialize arrays or do some string operations.
After hours of really weird behaviour, I looked a little deeper into the docs and was quite surprised to find out that I had unknowingly been running Java-like code through a C-compiler for half a day :)
The funniest thing about it was that it actually worked most of the time...

Thank you for the comment.

Amit Saha's picture

Thank you for the comment. Perhaps, "based on Processing" is not very true.

However to me, the language looks "very" C. And yes as you pointed out, its really gcc-avr down below as also pointed out in the later parts of the article. My experience with Processing is not beyond a basic level, so I came to Arduino "from" C, rather than processing.


Great article. Just a brief

Fabpicca's picture

Great article.
Just a brief observation.
Arduino doesn't stand for "good friend" in Italian (I'm Italian). As far as I know Arduino is the name of the cafè Arduino inventors where used to meet.

But Wikipedia says so?

AmitSaha's picture

Hello: Thanks for your comment. I am sorry if I got it wrong, But Wikipedia has this: "Arduino" is an Italian masculine first name, meaning "strong friend". The English version of the name is "Hardwin".(From: http://en.wikipedia.org/wiki/Arduino#History)


You're mistaken.

Anonymous's picture

Amit is a Hebrew name which has the meaning "Friend" but it's not correct to say that Amit is the Hebrew word for friend. Just because the name's origin is Italian doesn't mean it's a current Italian word for something.

Name of Cafe, Name of King

MoChaMan69's picture

The name Arduino comes from the cafe the founder(s) frequented where they thought of the project . Cafe Arduino takes it's name from medievel king that briefly ruled the area . I can't find my source right now but it's something like King of Ardo , therefore he was an Arduino , hence Cafe Arduino , hence the Arduino Project . Much of this comes from the IEEE article a few months ago .

Unfortunately, the lack of research in the first sentence makes me doubt the entire 5 page article . I'll glance at it later anyway in case there's something useful . - MC

I'm from Italy, therefore I

From_Italy's picture

I'm from Italy, therefore I can give a definitive answer. Arduino is the name of a bar in Ivrea (Piedmont, Italy) frequented by the (Italian) creators of the open source framework. The bar was named after Arduin of Ivrea, first King of Italy from 1002 to 1017. By the way, Arduino is not a common name nowadays in Italy


Roland's picture

The IO pins can't drive much: 5v and 40ma each, not to exceed 120ma total. You need a tiny relay to do anything more than power an LED, and even there you need to limit the current to prevent burnout. I have had luck with tiny reed relays connected directly to the IO pins. See www.elexp.com p/n 22RD5, with 5v/500ohm coils. These draw only 10ma. Buy more than you need and test them all. Two of the 12 I ordered had open coils. And put a diode across any relay coils. The Arduino already has pull-up/pull-down diodes on all the pins, which is why the AD pins read around 2.5v if not connected. Remember, when you stop applying any power to a coil, the magnetic field collapses which produces a brief voltage spike. Also, any of the above reed relays can also be used as magnetic sensors to detect an open window, for instance. Just glue a small magnet nearby.

Figures 2 and 3

OP's picture

The LEDs shown in figures 2 and 3 are shorted as illustrated. The edge rows on most (if not all) breadboards are buses.

Ground wires and tty

SomeTom's picture

Right. Also the ground wire of the rightmost LED does not connect anywhere. It should be in the same column as the LED it is supposed to ground.
If you are using a recent version of Arduino (and a newer original board) you are more likely to find /dev/ttyUSB0 that /dev/ttyACM0. (Just a typo, but /dev/tttyACM0 won't do any good at all).

Free Dummies Books
Continuous Engineering


  • What continuous engineering is
  • How to continuously improve complex product designs
  • How to anticipate and respond to markets and clients
  • How to get the most out of your engineering resources

Get your free book now

Sponsored by IBM

Free Dummies Books
Service Virtualization

Learn to:

  • Define service virtualization
  • Select the most beneficial services to virtualize
  • Improve your traditional approach to testing
  • Deliver higher-quality software faster

Get your free book now

Sponsored by IBM