It all starts with this little microcontroller!

The concept is pretty simple though it can be very intimidating at first. Simply put, A microcontroller is a device used to interact/interface hardware and software. In our case the Ardiuno is a powerful little device programmed using a set of C/C++ functions.
The quick and dirty "Why is the Arduino so cool?"
Its a simple to use, easy to program physical computing environment.
What is Physical Computing? Simple, computers that interact with the physical world through an number or inputs and outputs(Servos, IR Sensors, Buttons, RFID Tags etc...)
Key Features:
- Inexpensive
- Simple programming IDE
- Cross Platform
- Open source (Hardware AND Software! How freakin' cool is THAT!)
Here is a pic of all the shwag we will be playing with!

Getting Started
- Downloading and installing Software
- Connecting to the PC
- Hardware basics
Basic
- "Hello World"
- Button Mashin' Madness!
- The Pot is calling the kettle!
- Range me Seymore! Part A & B
- All UR Servos are belong to... (ok I'm running out of ideas!)
Advanced (But not really)
- Pan and Tilt Servo Control with Pots
- Combining a Servo and Range sensor (Your own mini RADAR!!!)
- Serial Input/Output
- Advanced Hardware options (Clones and other versions)
Getting Started
Downloading and Installing the Software:
Download the Ardunio IDE
Download the sketches of all code used in this tutorial
- Windows & MacOSX
1. Extract the compressed zip archive to the folder of your choosing
2. Install the FTDI USB Drivers (c:\xxxx\ardunioxxxx\drivers\FTDI USB Drivers on windows)
3. Start the Arduino IDE
- Linux
This is a little bit of an advanced topic and covered in detail @ http://www.arduino.cc
Connecting the Arduino to your PC
Ok sit down, take a breather. This is going to be the hardest part of our journey
... Plug in the USB-B connector to the Arduino
... Plug in the USB-A connector to your PC
... voila! its like taking candy from a baby... (Wait my son wouldn't like that)
*Note: This is an over simplification. However, For our purposes it gets the job done.
There are several versions of Arduino and its clones in a variety of packages yours may be different!
Arduino & Arduino Compatible Hardware
Arduino Pro

Arduino Pro Mini

Roboduino

Seeeduino

Arduino Duemilanove

The official Arduino Duemilanove(Which means "2009" in Italian) such as the one I'm using here has 14 digital input/output pins (6 can be used as PWM/Servo outputs), 6 analog inputs, a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. It can be powered by USB, battery or wall wart. The Arduino micros you will see most commonly will have an ATMEGA168 or ATMEGA328 (you will see the 328 more and more it doubles the amount of memory available for applications and the cost difference is negligible compared to the 168). More will be discussed on compatibility and alternate hardware at the end of the tutorial.
Here is another version of the "shwag" shot, this time I have labeled the photo

1. (2)Arduino Micro controllers
2. Standard breadboard
3. "Ultra Brite" LED
4. Momentary Pushbuttons
5. Linear Potentometer
6. Ultrasonic Range Sensor
7. Red Laser (lead wires have been removed)
8. Short Range IR Sensor
9. Long Range IR Sensor
10. Various jumper wires
11. Lynxmotion Base Rotate + Pan & Tilt Kit + Servos (Not Required! Just for fun)
Lets get started!
The Basics
My micro controller version of "Hello World"
We are going to take an LED (Any will do) and generate "Hello World" using "Morse Code"
You don't necessarily need a breadboard or even an LED, In my case the Arduino has an LED built onto the board but I won't be using it.
Hardware
With the Arduino unplugged (Try your best not to go plugging and unplugging things while its powered, This is how the magic blue smoke gets let out of electronics and getting it back in is near impossible), Plug in the LED(the long side is the + side), Anode(+) side gets plugged into 13 and the Cathode(-) side get plugged into Gnd(Ground).
Below is my LED connected to a breadboard and then to the Arduino with jumpers


Whew that was tough wasn't it? See how intimidating these little buggers can be!
If you don't know what Morse Code is... Welcome to the communications age, How was the stone age? For info about Morse code see Wikipedia.org.
This is the dot-dash conversion of "hello world" .... . .-.. .-.. --- .-- --- .-. .-.. -..
Many Thanks to Webnet77 and their online Morse code generator (If nothing else I'm lazy)
Software
Now for the REALLY fun part code bashing! I like to use open source code as a base cut and then out the parts I don't like and rework the parts I do like. I usually end up with completely different code than what I started with but it always helps me to have a base to start with. So in this case we are going to take the Arduino "Blink" example and compile and download it to the Arduino. This will blink our LED on and off and serve as a basic test.
- Open Arduino Environment
- Open "Blink"
- Compile
- Upload
- Here comes another hard part... Look for the blinky!
Now that we have the most basic of basics down and we know our Arduino is working lets get back to Morse and Hello World huh?
Disecting the code...
Code:
int ledPin = 13; // This is what tells the micro where we plugged the LED in
void setup() // When the sketch starts this runs ONLY one time
{
pinMode(ledPin, OUTPUT); // Sets the pinMode for "ledPin" which is "13" as output
}
void loop() // run over and over again, just like a loop!
{
digitalWrite(ledPin, HIGH); // sets the LED on // HIGH translates to 5v ON
delay(1000); // waits for a second // Listed in microseconds
digitalWrite(ledPin, LOW); // sets the LED off // LOW translates to 5v OFF
delay(1000); // waits for a second // Listed in microseconds
}
From this point its pretty straight forward, Below I have the different syntax I used to create dots dashes and spaces. The code is rather long so I have only included direct examples of each.
The full sketch, along with source for all other sketches is available to download as a compressed zip file
Code:
//Begin Dot
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(100);
//End Dot
//Begin Dash
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(100);
//End Dash
//Begin "Space"
delay(150);
//End "Space"
And now if you can read Morse code you can talk to yourself or your neighbors at night on that off chance you both get snowed in... ok, ok, ok maybe not...Up Next Buttons!
Button mashing madness!
Hooking up a button to your Arduino now allows it to accept input from the user! This is a VERY basic example.
Hardware

- Connect Digital Pin 2 is connected to the Gnd side of the button
- Then connect to a 10k ohm resistor (doesn't matter the orientation)
- Then from there jumpered to Gnd
- The other side of the button is connected to 5v via a jumper
- (a more detailed schematic is provided @ arduino.cc)
Software
The following code basically detects the button press and returns a couple of values to a terminal session based on certain "States"
- Copy the code into a blank sketch and save.
Code:
/*
created 27 Sep 2005
modified 17 Jun 2009
by Tom Igoe
http://arduino.cc/en/Tutorial/ButtonStateChange
*/
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 13; // the pin that the LED is attached to
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter, DEC);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
- Verify/Compile and Upload it to the Arduino
- Open up a Hyperterminal session @ 9600 8 n 1
- Start pressing the button!
Your Hyperterm screen should start like this:
on
number of button pushes: 1
off
on
number of button pushes: 2
off....
Button mashing madness is over
.... next!
The Pot is calling the kettle!
Using a potentiometer to control the blinking of an LED
Hardware

- With the pin side of the pot facing you (confirm with your relevant datasheets) the left most pin is positive, center is signal, right is Gnd
- The pots center pin is the signal, connect it to Analog 0
- Connect the positive and Gnd pins as well
- I have used the breadboard to make life easier, jumper wires from the Arduino to the + and Gnd Rails on the breadboard are then jumpered to the corresponding pins of the pot.
Software
Pretty simple but certainly a good example of interacting with the physical world. The LED will blink a varying rates based on the potentiometers position.
- Copy the code into a blank sketch and save.
Code:
Created by David Cuartielles
Modified 16 Jun 2009
By Tom Igoe
http://arduino.cc/en/Tutorial/AnalogInput
*/
int sensorPin = 0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}
- Verify/Compile and Upload it to the Arduino
- Turn the pot, Notice the LED blinks faster or slower depending on the direction the pot is turned
The Kettle agrees, The pot doesn't know what its talking about...
Range me Seymore! Part A.
I'm actually going to break this down into 2 parts Infrared(IR) and Ultrasonic(US). Both are similar but different and use differing approaches, then I'll wrap it up with a quick introduction to "Smoothing".
The basics here are a little more complex and as such I have chosen to give you 2 examples and a clever trick to help with noise. IR range sensors usually have a more precise beam than most Ultrasonic sensors, However you should always check the datasheet for your sensor for specifics.
Hardware
Long and Short Range IR Sensors
Most IR sensors have fixed distances for example the GP2D12 detects objects in the 4-30 inches range, while the GP2Y0A02YK can see 8-60 inches. and their beam is much more cohesive and the US. The IR beam gets a slight bit larger than the emitter/detector lens but not by much.
What is a PIR? or Proximity Infrared Sensor? Think home/office alarm system motion detectors, they have a wide area of coverage but suffer from the same precision issues US sensors do only multiplied, they are not intended for range sensing just detecting IR radiation. So in our case and in robot navigation useless.

Sorry for the horrid photo I had a nightmare of a time with this shot
- Connect Analog Pin 2 is connected to the Signal wire of the IR sensor
- The black wire is jumpered to Gnd
- The red wire is connected to 5v via a jumper connection on the breadboard as well
Software
The following code basically reads the voltage value to and converts to cm then sends to a terminal session.
- Copy the code into a blank sketch and save.
Code:
int tot = 0; // the running total
void setup()
{
Serial.begin(9600);
}
float read_ir(byte pin) {
int temptot;
temptot = analogRead(pin);
if (temptot < 3)
return -1; // invalid value
return (6787.0 /((float)temptot - 3.0)) - 4.0;
}
void loop()
{
tot = read_ir(2); // Assign the value of function "read_ir" (passing the
// variable 2 is used to tell the function what pin the
// IR signal is connected to) to variable "tot".
Serial.println(tot); // print the tot variable to the serial connection
delay(500); //delay 1/4 seconds.
}
// this is based on code: http://www.arduino.cc/playground/Main/ReadGp2d12Range
Verify/Compile and Upload it to the Arduino
Open up a Hyperterminal session @ 9600 8 n 1
Your Hyperterm screen should start like this:
20
20
20
20
19
19
19
19
20
20
...
or something similar it just depends on whats in front of your sensor and how far it is. Try pointing it around the room, I clamped mine into a "helping hands"
Up next Part Duex! Ultrasound, And no guys, No need to run! We aren't talking about THAT ultrasound ( and besides they're not SO bad, My kid hasn't driven me crazy yet... just to futzing with electronics, talking to myself and constantly bumping into things, oh wait!...)
Range me Seymore! Part B.
Part B will cover Ultrasonic Range sensing and a bit about "Smoothing". Ultrasonic range sensors have a different detection area than the IR sensor. Their beam is more like a cone, Some have been tuned for varying beam widths so be sure to check your sensors datasheet for details.
Hardware
Maxbotix, Devantech, Parallax and others manufacture several models for you to choose from.
I have a Maxbotics EZ-0 sensor, with a set of male header right angle pins

- Connect digital pin 5 to the PWM connection of the sensor (Green in photo)
- The orange jumper wire is to Gnd
- The power is connected to 5v via a red jumper connection on the breadboard as well
Software
The following code reads the signal and converts to cm then sends to a terminal session. Just like the IR code from the previous part except that this one will implement smoothing. What is Smoothing? Averaging sensor output, some may call it normalizing the data. In this case we are using a simple array and averaging the values there by normalizing any noise or spikes.
- Copy the code into a blank sketch and save.
Code:
//ULTRASOUND SETUP
unsigned long echo = 0;
int ultraSoundSignal = 5; // Ultrasound signal pin
unsigned long ultrasoundValue = 0;
//ULTRASOUND END
//SMOOTHING SETUP
// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 15;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
//SMOOTHING END
void setup()
{
Serial.begin(9600);
pinMode(ultraSoundSignal,OUTPUT);
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
unsigned long ping(){
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(50); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(50); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
ultrasoundValue = (echo / 147);
return ultrasoundValue;
}
void loop()
{
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = ping();
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer (as ASCII digits)
Serial.println(average, DEC);
delay(250); //delay 1/4 seconds.
}
The heart of this code is a code bash from Smoothing and Ping
- Verify/Compile and Upload it to the Arduino
- Open up a Hyperterminal session @ 9600 8 n 1
Your Hyperterm screen should start like this:
20
20
20
20
19
19
19
19
20
20
...
or something similar it just depends on whats in front of your sensor and how far it is. Try pointing it around the room, I have used a metal "L" bracket and some double sided tape and attached it to my lynxmotion base rotate for a mini radar(more on this later!). Up next.. Servos!
All UR servos are belong to us!
Hardware
Servos are one of the basic tools of robot builders, converted to 360deg rotation they can be used as motors. They can also be used as sensor actuators, pan & tilt etc.

- Connect Yellow signal pin on the servo cable to digital pin 10 (PWM)
- Red pin on the servo to the 5v (or another battery positive connector)
- Black pin on the servo to the Gnd (or another battery Gnd connector)
Software
Simple back and forth sweep.
- Copy the code into a blank sketch and save.
- Or Arduino -> File -> Sketchbook -> Examples -> Library-Servo -> Sweep
Code:
// Sweep
// by BARRAGAN <http://barraganstudio.com>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
- Verify/Compile and Upload it to the Arduino
Your servos should start sweeping side to side.
Pan and Tilt with Pot controls
Pan and Tilt with Pot controls
Hardware
Control your servos with a more tactile feel, Use a pot or 2!

- Basically a combo of the potentiometer tutorial and the servo tutorial
- Servo signal pins connect to digital pins 6 and 10 (or any other PWM pins)
- Pot signal pins connect to analog pins 0 and 1
Software
- Copy the code into a blank sketch and save.
Code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo1;
int potpin = 0; // analog pin used to connect the potentiometer
int potpin1 = 1; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int val1;
void setup()
{
myservo.attach(6); // attaches the servo on pin 9 to the servo object
myservo1.attach(10);
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val1 = analogRead(potpin1);
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
val1 = map(val, 0, 1023, 0, 179);
myservo.write(val); // sets the servo position according to the scaled value
myservo1.write(val1);
delay(15); // waits for the servo to get there
}
Based on the code of Michal Rinott <http://people.interaction-ivrea.it/m.rinott> bashed for 2 pots controlling 2 servos
- Verify/Compile and Upload it to the Arduino
... Next Mini Radar
Combining Servos and sensors (Mini Radar)
Combining Servos and sensors (Mini Radar)
Hardware
Combine the following for a mini radar system!


- Servo Signal cables connect to digital pin 9
- US Sensor PWM signal pin to digital pin 5
Software
The software will sweep the servo from side to side and take readings from the Ultrasonic sensor every quarter second printing the results to a serial connection
- Copy the code into a blank sketch and save.
Code:
#include <Servo.h>
Servo myservo; // new servo object
int pos = 0;
//ULTRASOUND SETUP
unsigned long echo = 0;
int ultraSoundSignal = 5; // Ultrasound signal pin
unsigned long ultrasoundValue = 0;
//ULTRASOUND END
void setup()
{
Serial.begin(9600);
pinMode(ultraSoundSignal,OUTPUT);
myservo.attach(9); // attach to digital pin 9
}
unsigned long ping(){
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(50); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(50); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
digitalWrite(ultraSoundSignal, HIGH); // Turn on pullup resistor
echo = pulseIn(ultraSoundSignal, HIGH); //Listen for echo
ultrasoundValue = (echo / 147);
return ultrasoundValue;
}
void loop()
{
Serial.println(ping(), DEC);
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
delay(250); //delay 1/4 seconds.
}
Pretty simple code, To be effective it really needs to get a lot more complex (maybe that will be my next tutorial).
- Verify/Compile and Upload it to the Arduino
- Open up a Hyperterminal session @ 9600 8 n 1
Your Hyperterm screen should start like this:
20
20
20
20
19
19
19
19
20
20
...
It may vary greatly depending on what is in the sensors range obviously.
Serial Input/Output is up next!
Serial Input/Output
Software
This will read in keyboard input and respond to the same terminal
Code:
...
// Using Switch/Case to read input over a serial connection and print a response to the connection
void setup() {
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
int input1 = Serial.read(); //reads in a value from Serial
//This is where we programatically tell is what to print (or do) based on the input
switch (input1) {
case 'a':
Serial.println("a pressed");
break;
case 'b':
Serial.println("b pressed");
break;
case 'c':
Serial.println("c pressed");
break;
case 'd':
Serial.println("d pressed");
break;
case 'e':
Serial.println("e pressed");
break;
default:
Serial.println("press a,b,c,d or e");
}
}
}
This can be used to control the pan and tilt servos via keyboard! Great for aiming a camera or mech weapons.
- Verify/Compile and Upload it to the Arduino
- Open up a Hyperterminal session @ 9600 8 n 1
Your Hyperterm screen should start like this:
press a,b,c,d or e
a pressed
b pressed
...
... Last but not least a little bit more in depth on the advanced hardware options available.
Advanced Hardware options
Hardware
Below is a list of Arudino compatible boards that offer different packages bringing out different pins etc but still compatible with the Arduino IDE.
Freeduino
Boarduino
Orangutan Robot Controllers
LEDuino
Seeeduino
Roboduino
and more!
These all have pluses and minuses, For more info about Arduino compatibility
The End! I hope you enjoyed and are inspired to pick up an Arduino, one of its clones or one of its competitors!
I just hope you start playing with micro controllers you don't know what fun your missing! The possibilities are almost endless!
Just imagine what Macguyver would have dome with an Arduino, a Peizo vibration sensor and a slinky!
I hope you enjoyed my madness and maybe even learned something comments and suggestions welcome!