/ /
The Interface:
© Royalty Free/Corbis

If you need Help with you program...
Hilite a command and then click on
Help menu and select
Reference:
Common functions
-
pinMode()—set a pin as input or output
- digitalWrite()—set a digital pin high/low
- digitalRead()—read a digital pin's state
- analogRead()—read an analog pin
- analogWrite()—write an "analog" value
- delay()—wait an amount of time
- millis()—get the current time
Arduino Programming Notebook
Basic Sketch Structure
- Declare variables at top
- Initialize values
-
setup()— run once at beginning, set pins
- Running
- loop()— run repeatedly, after setup()
LEDs are cool because they're tiny and long lasting (up to 10 years). Unlike incandescent bulbs, LEDs don't get hot. They also don't consume a lot of power.
LEDs come in many colors, brightnesses, sizes and shapes.
LEDs are polarized. The positive side (the side connected with the + side of the battery) is the longer leg and is referred to as the anode.
The shorter leg, the negative side, is called the cathode. You won't hurt the LED if you accidentally plug it in backwards. It just won't work.
The color of an LED is determined by the semiconductor material and not by the package (plastic body). Different colored LEDs require different amounts of voltage to light up. Red, green, and yellow LEDs typically require 2.2V-2.4V. Super-bright white and blue LEDs can require up to 3.4V.The optimum
voltage is referred to as forward voltage (V
F).
Most LEDs require 20mAh of current flowing from the battery. Most batteries supply more than 20mAh, so you need to use a resistor to limit the flow of current
to each LED so it won't burn out.
V
F Forward Voltage is also referred to as Voltage Drop. It is the minimum amount of voltage needed to light up an LED.
l
V Luminous Intensity is the amount of light emitted from an LED in a particular direction. It is measured in millicandela (mcd). You can consider this
property as brightess. The greater the millicandelas the brighter the LED.
I
F Forward Current is the amount of current the LED uses.
Viewing Angle is the spatial distribution or spread of light. It is expressed in degrees that measure the width of the light beam. LEDs with a small viewing angle produce a more focused beam,
and LEDs with larger viewing angles produce a softer more dispersed beam.
| Type |
Color |
IF |
VF Typ |
VF Max |
Luminous Intensity |
Viewing Angle |
Wavelength |
| Standard |
Red |
30mA |
1.7V |
2.1V |
5mcd@10mA |
60° |
660nm |
| Standard |
Yellow |
30mA |
2.1V |
2.5V |
32mcd@20mA |
60° |
565nm |
| Super bright |
Red |
30mA |
1.85V |
2.5V |
500mcd@20mA |
60° |
660nm |
UV LED's emit a light that you don't see, but that can damage your eyes. Be careful!
To use a resistor calculator you need the following information:
- Supply voltage, the voltage from your power source
- LED voltage Drop
- LED Forward Current
- Number of LEDs you want to connect
- Connection—
whether the LEDs are connected in series or in parallel
Exercise
Wire your board and code a program to perform the following sequence :
- LED1 on P8 ON, LED2 on P9 OFF
- Wait 2 seconds
- LED1 on P8 ON, LED2 on P9 ON
- Wait 1 second
- Both LEDs OFF
- Wait one-half second
- Repeat
Making motions with servo motors

A Servo is a small device that has an output shaft. This shaft can be positioned to specific angular positions by sending the servo a coded signal. As long as the coded signal exists on the input line, the servo will maintain the angular position of the shaft. As the coded signal changes, the angular position of the shaft changes. In practice, servos are used in radio controlled airplanes to position control surfaces like the elevators and rudders. They are also used in radio controlled cars, puppets, and of course, robots.
What's inside:

The servo motor has some control circuits and a potentiometer (a variable resistor) that is connected to the output shaft. This pot allows the control circuitry to monitor the current angle of the servo motor. If the shaft is at the correct angle, then the motor shuts off. If the circuit finds that the angle is not correct, it will turn the motor the correct direction until the angle is correct. The output shaft of the servo is capable of traveling somewhere around 180 degrees. Usually, its somewhere in the 210 degree range, but it varies by manufacturer. A normal servo is used to control an angular motion of between 0 and 180 degrees. A normal servo is mechanically not capable of turning any farther due to a mechanical stop built on to the main output gear.
The amount of power applied to the motor is proportional to the distance it needs to travel. So, if the shaft needs to turn a large distance, the motor will run at full speed. If it needs to turn only a small amount, the motor will run at a slower speed. This is called proportional control.
Angles of Rotation:
Continuous Rotation servos
The continuous rotation servos keep on rotating when pulsed with power. The
most common continuous rotation servos from Parallax rotate in
one direction when pulsed with power with a delay above 1500
microseconds and the other way when pulsed with a delay below
1500 microseconds.
This program will make the continuous rotation servo go 100 steps
one way then 100 steps the other way.
#define motoPin 9
void setup(){
pinMode(motoPin,OUTPUT);
}
void loop(){
for(int i = 0; i < 100; i++);{
digitalWrite(motoPin,HIGH);
delayMicroseconds(1850);
digitalWrite(motoPin,LOW);
delayMicroseconds(1850);
}
for(int j = 0; j < 100; j++);{
digitalWrite(motoPin,HIGH);
delayMicroseconds(1250);
digitalWrite(motoPin,LOW);
delayMicroseconds(1250);
}
}
What servos are good for
- Roboticists, movie effects people, and
puppeteers use them extensively
- Any time you need controlled, repeatable
motion
- Can turn rotation into linear movement
with clever mechanical levers
Where you find them


The underpinnings of one of the five transforming dresses featured in Hussein Chalayan's latest runway show. Wires within the tubes connect to motors at the bottom of the dress. The motors reel in wires attached to the outer layer of the garment, altering its shape.
When using the servo, it is helpful to mark the servo horn. This allows you to follow the rotation:

Connect the servo to the Microcontroller like this:
The computer can't really output analog values, but it can fake it.
It fakes it using Pulse Width Modulation or PWM.
This program moves a servo across its full range:
But doesn't Arduino have PWM?

Yes Arduino has built-in PWM on pins 3, 5,6, 9,10,11, and you can use
analogWrite(pin,value)
to write an analog value (PWM wave) to a pin. You can light a LED at varying brightnesses or drive a motor at various speeds.
After a call to analogWrite(), the pin will generate a steady wave until the next call to analogWrite (or a call to digitalRead or digitalWrite on the same pin). But it operates at a high, fixed frequency and isn't usable for servos. The PWM speed used for
analogWrite() is set to 490Hz currently. It's
not something changeable without understanding more about how AVRs work.
So when programming AVRs in C outside of Arduino, PWM speed can be set to just about any value.
Here's the good news, the Servo library takes care of this for you:
Exercise
Wire up the servo and a potentimeter:
You are going to drive the servo with a potentiomer.
#include <Servo.h>
Servo myServo;
int pos=0;
int analogSensor=0;
void setup(){
myServo.attach(9);
myServo.write(0);
Serial.begin(9600);
}
void loop() {
pos=analogRead(analogSensor);
Serial.println(pos);
pos=map(pos,0,750,0,180);
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
Servo Critters
From my class:
Others:
/ / Relays
Inside the little plastic box is an electromagnet that, when energized, causes a switch to trip. You can buy relays that vary in
size from teeny tiny to as big as a fridge. Each is capable of switching a certain amount of current.
- Wire up the following schematic
- Program the chip so that pin 2 is an output. You want to toggle pin 2 on and off
- Test the circuit
- Remove the red LED, and connect the motor in its place (bypass the 560 Ohm resistor). Change your code so the motor stays on longer
- Test the circuit/program
Relay Datasheet
Switching
#define RELAY_PIN 2
static int relayVal = 0;
void setup(){
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(9600); // open serial
Serial.println("Press 1 or 0 to toggle relay on/off");
}
void loop(){
int cmd;
while (Serial.available() > 0){
cmd = Serial.read();
switch (cmd){
case '0':
relayVal=0;
report();
break;
case '1':
relayVal=1;
report();
break;
default:
Serial.println("Press 1 or 0 to toggle relay on/off");
}
if (relayVal){
digitalWrite(RELAY_PIN, HIGH);
}
else{
digitalWrite(RELAY_PIN, LOW);
}
}
}
void report(){
if (relayVal){
Serial.println("Left on, right off");
}
else{
Serial.println("right on, left off");
}
}