Sunday, November 4, 2012

Introduction to Arduino

In the second article, I introduce a cheap open source electronics prototyping platform Arduino, which will be used in many upcoming projects here at The article covers motivation, selecting and purchasing the board, setting up the development environment, and creating a simple programming example - just enough to get you prepared for next articles.

LEVEL: Beginner

Now that we have build a nice working environment (a small home electronics laboratory was set up in the first article) it is about time to start tinkering with something. So we need to acquire some sensors and stuff to play with. Time to go shopping! But what should we buy?


Sensors are the same thing to electronic devices than senses to human beings: they provide data for perception. Sensing is vital for proper operation in a physical world. For example, if you accidentally put your hand on a hot plate, serious damage would occur very quickly if skin wouldn't sense the heat and notify about it. In a similar manner, a cleaning robot could break itself if its proximity sensor wouldn't warn about the stairs ahead. Since we're at, naturally some sensors are on our shopping list. We will start with a temperature sensor, which is a rather cheap component and used in many designs - yet simple enough for an introductory project.

The first thing to understand is that a sense or sensor alone doesn't really do anything useful - it simply responds to some physical quantity, such as heat, pressure, light intensity or acceleration by alternating its output. Hence, a sensor must be connected to something that responds to different sensor readings in a meaningful way, just like your brain responds to skin's warning about heat and commands your hand to quickly retreat.

Let's take another example: a project might use a sensor for measuring ambient light intensity, and then turn on the outdoor lights when it gets dark. So in addition to a sensor ("it's dark"), a useful project typically needs some logic ("when ... turn on ...") and an actuator ("outdoor lights"). This is generalized in the picture below.

Typical entities in a sensor project.

In all but the most simple projects implementing the logic part means writing software code that runs on a CPU somewhere. A CPU was mentioned. Now you're thinking about your PC, right? Yes, you could use a PC for running the software. However, there are a few good reasons why you might not want to use a PC in a sensor project:

  • In projects that are designed to run 24/7, noise and power consumption must be minimized
  • Most projects benefit from small size, and PCs tend to be bulky
  • For many projects PCs are too expensive and a total overkill for the need
  • Connecting sensors and actuators to a PC is not straightforward due to lack of proper I/O
  • There's always the risk of serious damage i.e. frying your PC if something goes wrong

From this point of view, a much better solution would be to use silent, low-power, small, cheap, simplified mini-computer that has suitable inputs and outputs and protections for connecting sensors and actuators safely. Does such things even exist? Yes, a microcontroller on a prototyping board is just the perfect solution and comes in a price range of just $20-30.

In fact, there are a plethora of alternatives to choose from. Since microcontrollers are used everywhere, each manufacturer wishes to get people to use their product in their designs, in the hope of selling millions of components. Thus, the manufacturers usually provide nice prototyping boards and software tools for cheap, just to get you hooked on their ecosystem. For example, Microchip's PIC and Atmel's AVR have been extremely popular over the years.

However, from your point of view an open source alternative might be the best bet, especially when you're still learning things. The benefits are that there are many manufacturers so the price point stays low, component availability and support are good, and there is a vibrant community sharing ideas and designs and providing help.

It is typical to open source that there are many competing alternatives. However, by far the most popular one is Arduino, which kind of started the whole popularization of microcontrollers for non-techies by making things cheap and easy enough for beginners for the first time. So actually this is a simple choice: just jump on the wagon that everyone's on, at least for now. We'll add an Arduino to our shopping list.

In order to do something useful with a temperature sensor and software logic running on an Arduino board, we also need an actuator that somehow brings things back to the physical world. One of the most simple but useful actuator type is a buzzer, which can make simple sounds such as beeps. For example, to alert about a high temperature. Added to list.

Now that we have selected a sensor, a board for running the logic, and an actuator, we're ready to start shopping. But where can we buy this stuff?


It is likely that you won't find an Arduino from your local electronics shop, so it's time to go online. Arduino boards are available globally from many online stores, such as the great or its local distributors. Since I'm located in Finland, I prefer a Finnish shop that can provide the components in a day or two (I've had good experience with

When you look for Arduino boards, you'll notice that there are lots of different Arduino models, and in addition a great number of clones (or forks using the open source terminology) such as Freeduino or Seeeduino. These should be compatible yet might have some differences or at least a different prize.

Since we're just starting out and wish to keep things simple, we'll select the official Arduino and the latest revision of its classic model. Currently, that would be Arduino Uno - older similar revisions are called Duemilanove and Diecimila.

In addition, you need an A-B type USB cable for connecting the Arduino to a PC so that you can upload your program code to the board and also power the board via USB. The correct cable is the common one where the other end is square and the other one flat - if you have an external USB hard drive you probably have one already. I suggest buying an extra cable just for your Arduino projects.

For temperature sensor, I chose Sparkfun's TMP36. They describe it as follows:

The TMP36 is a low voltage, precision centigrade temperature sensor. It provides a voltage output that is linearly proportional to the Celsius temperature. It also doesn't require any external calibration to provide typical accuracies of ±1°C at +25°C and ±2°C over the −40°C to +125°C temperature range. We like it because it's so easy to use: Just give the device a ground and 2.7 to 5.5 VDC and read the voltage on the Vout pin. The output voltage can be converted to temperature easily using the scale factor of 10 mV/°C.

A temperature sensor couldn't be much simpler than that. For a buzzer, basically any piezo-electric buzzer that doesn't draw much power is fine, for example Pololu's 30mm Piezo1260. However, I have extracted plenty of buzzers from toys and greeting cards ("Happy Birthday to you..."), so I just picked one from my own stock.

A temperature sensor, Arduino board with A-B USB cable, and a piezo-electric buzzer.

Arduino IDE

When you here a slam from your mailbox, I bet you're anxious to start immediately. However, the first thing to do when you receive the components is to check that everything ordered are included and seem intact. 

Everything OK? Then we can proceed to setting up the development environment, i.e. the Arduino IDE (integrated development environment). The software includes a text editor for writing program code, compiler and linker for creating application binaries, and an upload mechanism that will send the binaries to your Arduino board and start updating it - all in one!

There's a good tutorial on how to get things running on Arduino's website, so I'm not going to repeat everything here, just list the basic steps (refer to tutorial in case you have problems).

Hello World, a'la Arduino:
  1. Open a web browser, go to and click tab "Download". Choose the latest version of the software (1.0.1 as of writing this) and your operating system (Windows/Mac/Linux). Wait until the download is finished.
  2. Install/extract the package, and start the IDE. For example, I'm on Linux and simply extracted the files to a directory and run "arduino" shell script to start it.
  3. Install drivers for the Arduino's USB-to-serial chip. For example, Arduino Diecimila uses an FTDI chip and requires FTDI drivers. Refer to tutorial for details. If you're on Linux, you probably have the drivers installed already and can skip this step.
  4. Connect the Arduino to your PC using the A-B type USB cable. Arduino's power LED should turn on. Note: If you have Arduino Diecimila, check that the jumper near the USB port is set to USB instead of EXT to ensure that Arduino is powered via USB.
  5. From Tools->Board select your board type, such as Diecimila or Uno.
  6. From Tools->Serial Port select the port where Arduino is connected to, for example on Linux it is probably /dev/ttyUSB0. Note: If the Serial Port menu is grayed out on Linux platform, you need to add your user account to 'dialout' group and logout/login.
  7. Select File->Examples->01. Basics->Blink. A new Arduino IDE window will show up (yes it's a bit weird) that'll contain a program code for an example that blink's the Arduino's internal LED (connected to digital I/O pin 13).
  8. Select Sketch->Verify/Compile or simply press CTRL+R. The IDE will now create binaries from the program code.
  9. Select File->Upload or simply press CTRL+U. The IDE will upload the code to your Arduino board. You should see RX/TX leds blinking on the board for a while.
  10. After upload is finished, the board is automatically reset and the uploaded program starts to run. As a result, the internal LED should start blinking i.e. change its status on/off once per second.

Arduino IDE on Ubuntu Linux.

Now you have a working development environment for Arduino projects and have tested your first Arduino program. Congratulations! Next, we'll proceed to the fun part i.e. make some use of the temperature sensor. You can now detach the Arduino from the PC for a while.

Alert, alert! It's getting hot in here!

Let's make a simple sensor project: a system that warns user by beeping, if temperature gets above set limit.

The first thing would be to connect the temperature sensor TMP36 to our Arduino board. The component seems to have three legs, which must be attached correctly to Arduino's connectors. But where should each leg go? To know that, we'll need to study the component's datasheet.'s product page for TMP36 provides a link to the datasheet of the component, which you can download from here as well. The datasheet shows the sensor's pin-out as follows:

TMP36 pin-out. Extracted from component's datasheet.

From the datasheet and pin-out image you can comprehend that the sensor's Pin 1 (+Vs) should be connected to Arduino's 5V pin to provide power to the sensor; Pin 2 (Vout) is an analog output whose voltage level depends on temperature and thus should be connected to one of Arduino's analog input pins 0-5; and finally Pin 3 (GND) goes naturally to Arduino's GND pin. And that's all.

WARNING: Notice that the pin-out diagram is the bottom view. If you accidentally think it's the top view, you'll swap pin 1 and 3 and hence reverse the polarity of +5V and GND! As a result, the component will probably get very hot and burn after a few seconds - it might even fry your Arduino. Always double check all connections before applying power to the circuit!

Usually we'd now take a piece of breadboard, push the sensor there and use a couple of wires to make the connections to Arduino. However, all the relevant pins happen to be so close to each other on the Arduino board that we can simply bend the sensor's legs a bit and then push it straight to Arduino board. I've bent leg 1 forward, leg 2 backward, and shortened leg 3 a little (see the picture below).

Bend leg 1 forward, leg 2 backward, shorten leg 3. All nicely in a straight line.

WARNING: Do not bend the legs near the component's case, as the legs will break very easily. Always use pliers for bending, not fingers!

Bending a leg right below the case will probably end up like this. Ready for trash can :(

A buzzer can be controlled nicely with a digital output pin that has PWM (pulse-width modulation) support, for example Arduino's Pin 11. I've plugged in the buzzer to Arduino using GND and Pin 11 (see the picture below). Positive side of the buzzer goes to Pin 11, negative to GND.

Temperature sensor and buzzer connected straight to Arduino board.

WARNING! Normally you can't just plug in any sensors and actuators, but need to build a circuit with some extra components such as resistors and capacitors, for example using a breadboard. In this introductory tutorial I wanted to keep things as simple as possible, hence these components were carefully selected so that such a simple connection is possible.

If you now power on the Arduino ...nothing interesting will happen. We haven't told Arduino what to do with these components, i.e. the logic board exists but the logic itself (software) hasn't been created yet! We'll do that next.

Open Arduino IDE, and copy-paste the following code to an empty project:

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication to PC at 9600 bits per second:

// the loop routine runs over and over again forever:
void loop() {
  // read the temperature sensor's output from analog pin 0
  int sensorValue = analogRead(A0);
  // convert the reading (which goes from 0-1023) to milliVolts (0-5000):
  float voltage = sensorValue * (5000.0 / 1023.0);
  // convert the voltage value to Celsius degrees:
  float temp = (voltage - 500) / 10;
  // print out current temperature in Celsius to PC:

  // alert from high temperature (>30 degrees Celcius):
  if (temp > 30.0) {
    // beep with buzzer for 100ms using pulse width 20 (0-255)
    analogWrite(11, 20); // sound on
    analogWrite(11, 0);  // sound off
  // wait 1 second before next measurement.

If you wonder where the formula for converting voltage to Celsius degrees came, remember that the component gives 10 mV/°C (hence we divide voltage by 10). From datasheet you can see that TMP36's measurement range goes from -40°C to +125°C, while at 0°C the output is 500 mV (hence we subtract offset 500 from voltage first).

Connect the Arduino board to your PC now. Check that correct board type is set at Tools->Board and correct serial port at Tools->Serial Port. Then hit CTRL-R and CTRL-U, the code will be compiled and updated to Arduino and starts to run immediately.

To observe if everything works, open Arduino IDE's serial console (click the magnifier icon near top right corner of IDE window). You should see that new temperature value is printed every second!

New temperature value is printed every second.

Finally, squeeze the sensor between two fingers and observe how the temperature values begin to rise in the serial console. When temperature hits 30+ degrees Celcius, the buzzer should start beeping. Alert, alert, it's becoming hot in here!

This concludes the introduction to using Arduino as a sensor prototyping platform. Now what? We'll continue with Arduino here at soon - meanwhile, get your hands dirty and turn the example to a useful project of your own:

  • If you're interested in using TMP36 sensor in a more realistic setting, take a look at the datasheet how you can put the sensor in the end of a long cable. Also remember that after a program is once uploaded to Arduino, PC is not needed anymore (you can use a 5V power source or even batteries to power the system). Check out TMP36 tutorial at - it's really nice. We'll come back to this component later here at as well, so stay tuned.
  • To learn more about Arduino, you might want to get familiar with the official Arduino website, or check out more examples that were bundled with the IDE (File->Examples). There's also lots of tutorials online (just google "arduino tutorial"). One good resource is LadyAda's Arduino page.
  • Also try out tuning the sounds made with the buzzer. You can easily create simple melodies!


In this article I've presented motivation for using a microcontroller prototyping platform for sensor projects, introduced the reader to Arduino and its integrated development environment, as well as created a very simple sensor project using a temperature sensor and a buzzer.


  1. Alert, alert! It's getting hot in here!

    code plz

  2. A very nice clean simple tutorial. Thank-You.
    For us non-metric (legacy) Americans, I edited the working lines...

    int tmp36 = analogRead(A0);
    float voltage = temp36 * (5000.0 / 1023.0);
    float celsius = (voltage - 500.0) / 10.0;
    float fahrenheit = (celsius * 9 / 5) + 32;

  3. Oops! on 'e' in tmp36 (second line).

  4. I have to interface a analog sensor whose output voltage varies from 1v to 5 v max. but it does not have any crucial stepping outputs, I mean to say, its not fix that for certain real world happening it will give this certain output