ME 387

From Matt Bilsky

Introduction to Arduino


The online home of the Arduino is []. From here you can download the development environment along with view the Arduino language reference and the community forums.

All of the Lessons and implementations below are designed around the Arduino IDE version 0023. Although version 1.0 is the latest, due to changes in the back-end files it is easiest to use the older version to maintain compatibility.

Once you have an understanding of the basics of Arduino development in version 0023, it will be a nearly seamless switch for you to begin developing your own projects in version 1.0.

The majority of introductory tutorials are from the site []. She does an excellent job describing each step, and includes the code to make each example work along with solutions to the "code it yourself" sections.

Lesson 1 - Install Arduino IDE

The first step is to download and extract the Arduino IDE (Integrated Development Environment).

  1. Download the Arduino IDE version 0023 from the Arduino website. It is half way down the page under the "Previous IDE versions" heading. We want the version for windows, avalable here: [Arduino downloads page].
  2. Save the file to your computer
  3. Next we need to extract the .zip archive. Right click on the file and select Extract All. Select a easy to locate destination (I usually use my desktop, but for network computers you can use your personal share drive)
  4. To launch the Arduino IDE, navigate to the location you extracted the zip file. There should be a directory named "Arduino-0023". Inside this folder double-click Arduino.exe and the program should launch.
  5. Ignore the message that there is a newer version available. To disable this message navigate to File-> Preferences and un-check "Check for updates on startup". Select OK
  6. Congratulations! The Arduino IDE is all set up.

I like to have a shortcut to the Arduino IDE on my desktop. To do this navigate to the Arduino.exe file. Right click on it and select Send to-> Desktop (create shortcut). Now you can launch the Arduino IDE without having to open the folder.

Lesson 2 - Testing your Arduino

For this lesson we will be using Ladyada Lesson 0 "Getting Started." It is available here: [Lesson 0]

When the lesson was written, the current version of the Arduino was the Duemilanove. Since we are using an Arduino UNO, there will be a couple of changes in the procedure.

  1. It is no longer necessary to download drivers for the Arduino. There are located in the Arduino IDE folder Arduino-0023/drivers. When windows prompts for the drive to install, select "Browse my Computer for Driver Software" and navigate to the location of the Arduino IDE/drivers folder. Windows should take care of the rest for you. Note the Serial port number (COM#) that the board is installed to. You will need this later.
  2. For most computers today an external power supply will not be necessary for these tutorials.

Lesson 3 - Blink Arduino demo

The goal of this lesson is to compile and run your first program on an Arduino. The material covered here is similar to Ladyada lesson 1, but since it was written for older Arduinos, I have made a video below demonstrating the steps necessary using an Arduino UNO and IDE version 0023.

Here is a summary of what is happening in the video demonstration:

  1. Plug your Arduino into your computer's USB port (Same process as in Lesson 2, but this time the drivers should already be installed). In general, your Arduino should automatically attach itself to the same COM port each time you connect it.
  2. Launch the Arduino IDE (arduino.exe file or desktop shortcut)
  3. Navigate to File-> Examples -> Basic -> Blink. A new window should appear with the code of the blink example
  4. Now we need to tell the IDE which board version we are using and what port it is attached to. This is a step you will use time and again every time you change which version of the Arduino you are using or the COM port it is attached to.
    • Go to Tools-> Board and select the board that matches the one your are using (In this case Arduino UNO)
    • Also under Tools go to Serial Port and select the COM port your Arduino is attached to (COM9 in my cthe ase)
  5. Press the button that looks like a Play symbol to test compile your code. It should return no errors.
  6. Finally to upload the sketch to your Arduino select the second button from the right, Upload
  7. At the bottom you should see the verbose output from the compile process. Any errors will appear here. Ladyada has a section on what errors mean here: [Help]
  8. If the sketch uploaded to the Arduino correctly you should see the words "Done uploading."
  9. On your Arduino the LED labeled "L" should be blinking

Here is a Video showing the above steps being executed:

Here is a link to the Ladyada Lesson [Lesson 1] If you scroll down, there are videos at the bottom showing what your Arduino should look like with the blink demo running. It is also a good idea to go read through her tutorial and see where it differs from the steps above. The majority of the differences will be obsolete steps as we are using newer boards and software.

Lesson 4 - Modifying the Blink demo

In this lesson we are going to begin modifying sketches in the Arduino IDE. Go through [Lesson 2] on the Ladyada site. You should be able to follow the steps without any issues (the syntax of the code has remained the same throughout all revisions).

Also try the Exercises at the bottom to test your understanding.

Lesson 5 - LEDs and Breadboards

This lesson will introduce the idea of interfacing circuits on a breadboard with the Arduino. For this go through [Lesson 3] on the Ladyada site.

There is no need to purchase a prototyping shield for the Arduino, just use a prototyping breadboard (she shows the tutorial using both methods).

A common source of error is inserting the LED backwards. Note the section where she describes the 2 legs of an LED, and that the longer one is the positive lead.

Also, if you do not have different color LEDs, it is fine to use a single color LED for the later examples.

Lesson 6 - Serial Communication

Now let's have the Arduino send information back to our computer using Serial. This process is describes in Ladyada [Lesson 4]

Another aspect of this lesson is an introduction to variable types.

Lesson 7 - Inputs

So far we have dealt with data being output from the Arduino. Let's start reading some data in. In Ladyada [Lesson 5] she introduces how to connect a button switch for input, and how to make the Arduino chance it's output based on the button state.

Also in this lesson is an introduction to the different statement structures available in the Arduino language (If,While,Do, ect).

Lesson 8 - Temperature Sensor

Now that you know how to read the digital input from a switch (it's either on or off 1 or 0), let's read from an analog sensor. Unlike digital signals which are either high or low (for Arduino 5 volts or 0 volts), analog signals are based on the voltage being sent to the Arduino. An analog input on the Arduino can read a voltage between 0 and 5 volts. It then scales the value between 0 and 1023. This is done by the analog to digital converter (ADC).

The first sensor we are going to use is a temperature sensor that outputs a voltage that changes linearly based on the ambient temperature. We are using the LM335A from [LM335A]. Datasheet: [LM335A datasheet]

If you browse through the datasheet, you will notice that there are many different ways to wire this sensor into your circuit. The packaging type for our sensor is TO-92, and the pinout can be viewed on page 3 of the document.

While it is possible to simply use the sensor without connecting the ADJ (adjustment) pin (Fig 569802, datasheet), the output will not be accurate as the sensor will not be calibrated. We are going to implement the circuit described here [Dustin Andrew's correct version]

Below I have made a diagram of how I wired the circuit and connected it my Arduino. For the adjustment potentiometer, I used a 10k-Ohm 15-turn pot from RadioShack [271-0343 10k-Ohm Cermet Potentiometer]. I also used a 2.2K Ohm pull-up resistor.

Breadboard Circuit:

Temp demo bb.jpg


Temp demo schem.jpg

My implementation:

Temp demo.jpg

Now for the code. Below I have included my Arduino sketch. Notice how in the Setup function I specify the pin that the sensor is attached to as an INPUT. Also, this time we are using the analogRead() function to get our sensor value. The voltage that is read in corresponds to the temperature in Kelvin. We then convert it to Celsius and Fahrenheit. The sketch sends the temperature in Fahrenheit to our computer over serial so we can view it using the serial monitor.

void setup(){
  //Set the temperature sensor pin to INPUT mode
  pinMode(A0, INPUT);

  //Begin serial communication at a BAUD rate of 9600
void loop() { 

  //analogRead from the temperature sensor pin then convert it to Kelvin 
  float Kelvin = (((analogRead(A0) / 1023.) * 5.) * 100.);  

  //Convert it to Celsius
  float Celsius = Kelvin-273.;  

  //Convert to Fahrenheit
  float Fahrenheit=(Celsius)*(9./5.)+32.;  

  //Print the Fahrenheit temperature to the Serial port and send a new line


The final step is to calibrate the sensor. If you open up the serial monitor you should see the same number being sent repeatedly. This is the current temperature in Fahrenheit. My multi-meter had a temperature probe that outputs in either Fahrenheit or Celsius, but if you can only get a reference temperature in Celsius or Kelvin, then output that temperature to the serial port. Using a screwdriver turn the small screw on the potentiometer. You should notice the numeric output changing. Turn clockwise or counter-clockwise to adjust the temperature output until it matches the reference value from your thermometer. Once the two match, your sensor is calibrated! According to the datasheet the measured temperature should not vary over 1 degree from the actual value.

Lesson 9 - LCDs

The final introductory topic for Arduino is the use of a LCD. Ladyada demonstrates here in her [Char LCD tutorial] the correct method to wire and program a LCD as a display for your Arduino.

At the bottom she discusses creating your own characters. We will do this below in the next exercise.

To create a custom symbol such as degree symbol for this exercise, we need to use the createChar command. This is a good point to introduce the power of the Reference section on the Arduino website. [Arduino Language Reference]. On this first page is a list of all the basic commands of the Arduino language. Clicking on a command brings up a page describing what it does, required arguments, similar commands, and often times a demonstration. In the bottom left is a link to the library reference.

Following this link takes us to a list of the Libraries that are included with the Arduino IDE. Select the LiquidCrystal library. Looking at the commands select createChar. This takes us to the description of the command [CreateChar reference]. In the example we see that they are creating a byte array called smiley containing the definition of a smiley face. To create our degree symbol, we need to look up the necessary bytes. Ladyada suggests this website [HD44780 LCD User-Defined Graphics]. If you draw a degree symbol into the simulator, the 3rd line down on the right will give the Binary numbers for that character.

Here is my output:

In decimal: 28,20,28,0,0,0,0 In hex: 0x1c,0x14,0x1c,0x0,0x0,0x0,0x0 In binary: %11100,%10100,%11100,%0,%0,%0,%0

Referencing this number in Binary versus the smiley example, we see that numbers are defined using B11100 vs %11100. Thus we need to replace the % symbol with B then simply use these new numbers instead of the old ones.

To put it all together, Let's make a digital thermometer that reads the current temperature from your sensor then displays it on the LCD. Include the degree symbol before putting the units after the temperature. Try it on your own by combining this lesson with the temperature sensor lesson.

For reference the code for my solution is available here: [LCD_temp_demp.pde].

Below is a photo of my circuit and the LCD display:

LCD temp demp.jpg

Thermal Control Chamber

Step 1 - Control a Single LED

As per the pin layout above, I have attached my first LED, the red one, to pin 7 on my Arduino UNO. The goal is to just illuminate the LED. For most LEDs a single 1k pull-up resistor should work fine. For reference check out the Blink demo in #Lesson 3 - Blink Arduino demo. Modify it such that the LED is on continuously. A solution is available here: [tempChamber_01.pde]

Step 2 - Add a Button

The first thing to try is to wire the Button as depicted here: [Arduino Debounce Demo]. Change the LED pin to 7 (assuming that is the pin you are using in Step 1). For the moment digital pin 2 is fine for the time being. The Arduino sketch can be loaded from the examples -> digital -> Debounce menu in the Arduino IDE. Download it to the Arduino and when you press the button the LED should come on, and turn off when you release it.

Once you have the button working correctly, we need to modify how we are attaching the buttons. Due to the limited number of Digital pins, we will need to us analog pins for our buttons on this project. Going back to the basic difference between digital and analog reads we need to add an extra step of logic since we will no longer be receiving a simple 1 or 0 from the button. analogRead() returns a value between 0 and 1023. We can take 0 to mean 0, and 1023 for 1. To account for minor variations in voltage, lets use the following logic: if the value is below 512, the switch is released and conversely if it is above 513, then it is pressed.

Modify the Debounce demo to include this change and attach the switch to Analog input A2.

I have included my updated code for the project which now includes the button turning the LED on and off here: [tempChamber_02.pde]

Step 3 - Turn on the Light

Since the heat source we are using for this project is an AC halogen lamp, we simply cannot send a digitalWrite command to the Arduino and power it directly. To accomplish this task we use a device called a relay which is essentially an electrically controlled switch. With a 12 volt DC signal we can control the 120 volt light. For my box I am using a simple RadioShack relay [12V relay]. You can find it's datasheet here: [Relay Datasheet].

Since the lamp is AC we don't need to worry about ground and hot for the lamp and source side. Attach the 120V source wires to pins 3 and 4, and attach the lamp wires to pins 5 and 6. Be sure to heat shrink your connections as this is high voltage! Also heat shrink pins 1 and 2 to prevent shock on these pins. We do however need to wire the DC correct. Attach the positive wire to pin 7 and the ground to pin 8.

Because the relay is driven by 12 volts, we still cannot connect it directly to the Arduino. Instead we use a MOSFET transistor to allow us to use a 5V signal to turn the relay on and off. Connect the source pin of the MOSFET to ground and the drain to the ground wire of the relay. The gate pin can be directly connected to the Arduino. Use digital pin 3. Finally connect the positive wire to the 12V source, and voila your circuit is wired.

Before trying to code the relay control in Arduino, try controlling it manually. If you take the gate pin and put it to 5v, the relay should click and assuming your light is plugged in to 120 volts, it should turn on. As soon as you disconnect the gate from 5V the relay should click off and so should the light.

Now try using a simple digitalWrite() command to turn the relay and light on and off. Instead of controlling the LED with the push button, try controlling the heat source.

I have posted my update code for the project here: [tempChamber_03.pde]

Step 4 - Add Serial Communication

Now we want to send some data from the Arduino to our computer using Serial communication. Add a Serial.begin(9600) command to the setup function and then print out the state of button 1 to the Serial port using the println() command. After uploading the sketch open up the serial monitor. When the button is released 0s should appear. While the button is depressed 1s should be seen.

Here is the updated working code: [tempChamber_04.pde]

Step 5 - Send Temperature Over Serial

Let's add a temperature sensor to the mix. Wire up a temperature sensor as show here #Lesson 8 - Temperature Sensor. Send the temperature in Fahrenheit over serial. Calibrate the sensor using the potentiometer to room temperature.

Here's the updated Arduino sketch [tempChamber_05.pde]

Step 6 - Graph Temperature Using Processing

To begin it is first necessary to install Processing available here: []. Select the package for Windows (not the version for without java).

Extract the zip archive just as you did for the Arduino IDE.

Switching back to Arduino open the Communication->Graph example. Half way down there will be a commented out section called "Processing Code for this Example." Select only this section (stop at the section for MAX/MSP). Open Processing and paste this code in. Look at the part about setting the COM port. For most users it should automatically work. If you have multiple serial devices look at the command display at the bottom of processing for a list of serial ports in use and change the number used from the default value of 0 to the one your Arduino is attached to.

Change the map command from 1023 to the upper limit of the temperature you are going to send from the Arduino. 150 should be fine for right now.

To execute the sketch press the play button in the top left. Assuming your Arduino is attached to the computer and is running the code in Step 5 a graph of the temperature should be shown.

Here is the Processing sketch if needed: [tempControlGraph_01.pde]

Step 7 - Add a LCD Display

In addition to the graph in Processing, we want to have the temperature displayed on a 16x2 Character LCD. Try to follow #Lesson 9 - LCDs and integrate it into your code from the steps above. I am using digital ports 8-13 for the example code.

Here is the updated sketch: [tempChamber_07.pde].

Step 8 - Add a Second Button and Create a Setpoint

This time add a second button to the system and combined with the existing button use them to raise and lower a setpoint variable. For debugging purposes you can display this instead of the current temperature on the LCD. Make sure that each time a button is pressed the value increases or decreases by only one. A cool feature to possibly include (I didn't) is the ability to change the setpoint by a larger range if the button is depressed for an extended period of time (like setting an alarm clock).

Here is the updated sketch: [tempControl_08.pde]

Step 9 - Add the Setpoint to the Graph

First we need to send both the temperature and setpoint over serial. Using print() print the data and separate them by a comma, and terminate with a "\n" character.

Now we want to display the setpoint on our graph in Processing. Since we are already sending the data over serial seperated by a comma it is now necessary to deconstruct the string on the Processing side. Look at the [Processing language reference], specifically the split() function. Using this expand the existing Processing sketch to read both data. To graph the setpoint look at the point() function and to change the color look at stroke().

Here is the updated Processing sketch: [tempControlGraph_02.pde]

The Arduino Sketch: [tempControl_09.pde]

Step 10 - Configure 3 Status Indicator LEDs

Now add 3 indicator LEDs to show if the temperature is too hot, cold, or just right. Connect the blue (cold) to digital 5, green (just right) to 6, and the red led for too hot should already be connected to pin 7. It is a good idea to put a range on the green led (setpoint +/- 2 for example).

Don't forget pull-up resistors for the added LEDs!

Here's the updated Arduino sketch: [tempChamber_10.pde]

Step 11 - Add a Temperature Cut-out Switch (OPTIONAL)

A good practice for devices with a heat source is to add an overheat cut-out. One simple way to do this utilized a thermo cut-out resistor. When the set temperature of the resistor hits, it opens the circuit. If this was put in line with the relay it would automatically shut the light off when the temperature gets too hot.

Step 12 - Control the Fan with PWM

To control the fan which operates at 12 volts, a MOSFET is necessary just as in #Step 3 - Turn on the Light. I used the same mosfet and attached it to digital port 3 which is a PWM port. For testing purposes a simple digitalWrite() should turn the fan on. To control the fan's speed an analogWrite() may be used.

Here is the updated Arduino sketch: [tempChamber_12.pde]

Step 13 - Read the Fan Speed

Step 14 - Add Air Intake Temperature Sensor