/ / Sensors
So you've got a Freeduino, now what?
There are two types of sensors:Analog
| Potentiometers | Potentiometers (pots) provide a variable resistance; twisting the shaft changes the resistance to allow more or less current through. This should control the brightness of the light bulb,or the volume of the buzzer.![]() ![]() |
| thermistors | Change resistance in reaction to varying temperature.![]() |
| photoresistors | Change resistance in reaction to varying light.![]() |
| force sensitive resistors | Change resistance in reaction to being pressed.![]() |
| piezoelectric devices | Create a varying voltage in reaction to slight changes in pressure.
|
| flex sensors | Change resistance in reaction to being bent or flexed.![]() |
Digital
| accelerometers | Analog sensor with digital interface to measure tilt![]() |
| digital potentiometers | They’re digital because you can control the resistance over its range programmatically, by sending it commands over a 2-Wire (I2C/TWI) serial interface. That means that you can hook it up to the Arduino, and adjust the resistance in a program. The DS1803-010, means it has a range of 0-10K Ohms.![]() |
| digital compass reader | The compass uses a magnetic field sensor, which is sensitive enough to detect the Earth's magentic field. Two of them mounted at right angles to each other can be used to compute the direction of the horizontal component of the Earth's magnetic field.![]() |
What is I2C?
IC
I2C uses only two interface pins to talk to up to 127 devices.
The Wire library operates the ATmega's hardware I2C port which has been incorporated into the Arduino software.
- Analog pin 4 doubles as the I2C SDA (serial data) pin, and analog 5 doubles as SCL
(seri:
/****************************************************************************** * I2C_gpio * Keith Neufeld * May 26, 2008 * * Prototype I2C interface to TI 9535 and 9555 GPIO expanders. * * Arduino analog input 5 - I2C SCL * Arduino analog input 4 - I2C SDA * ******************************************************************************/
- Import the Wire library:
#include <Wire.h>
- Set your I2C device address(es)
// I2C device address is 0 1 0 0 A2 A1 A0 #define DIP_ADDRESS (0x4 << 3 | 0x0) #define LED_ADDRESS (0x4 << 3 | 0x7)
- An I2C device address is seven bits.
The I2C section of your datasheet will talk about
the eight-bit address byte including the data direction bit (R/W),
but according to Keith Neufield, this is a trick.
The Wire library will take care of the data direction bit
for you automatically on each operation you perform,
so you need to give it only a seven-bit address.
Initialize the Wire libraryvoid setup() { // ... Wire.begin(); // ... }
Here's a sample program by ITP's Tom Igoe that uses I2C and a digital compass:
/*
CMPS03 compass reader
language: Wiring/Arduino
Reads data from a Devantech CMP03 compass sensor.
Sensor connections:
SDA - Analog pin 4
SCL - Analog pin 5
created 5 Mar. 2007
by Tom Igoe
*/
// include Wire library to read and write I2C commands:
#include <Wire.h>
// the commands needed for the SRF sensors:
#define sensorAddress 0x60
// this is the memory register in the sensor that contains the result:
#define resultRegister 0x02
void setup(){
// start the I2C bus
Wire.begin();
// open the serial port:
Serial.begin(9600);
}
void loop(){
// send the command to read the result in inches:
setRegister(sensorAddress, resultRegister);
// read the result:
int bearing = readData(sensorAddress, 2);
// print it:
Serial.print("bearing: ");
Serial.print(bearing/10);
Serial.println(" degrees");
// wait before next reading:
delay(70);
}
/*
setRegister() tells the SRF sensor to change the address pointer position
*/
void setRegister(int address, int thisRegister) {
// start I2C transmission:
Wire.beginTransmission(address);
// send address to read from:
Wire.send(thisRegister);
// end I2C transmission:
Wire.endTransmission();
}
/*
readData() returns a result from the SRF sensor
*/
int readData(int address, int numBytes) {
int result = 0; // the result is two bytes long
// send I2C request for data:
Wire.requestFrom(address, numBytes);
// wait for two bytes to return:
while (Wire.available() <l; 2 ) {
// wait for result
}
// read the two bytes, and combine them into one int:
result = Wire.receive() * 256;
result = result + Wire.receive();
// return the result:
return result;
}http://www.nearfuturelaboratory.com/2007/01/11/arduino-and-twi/
/ / Servos
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:
What they're 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 a Hussein Chalayan's 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:
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:
#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:
/ / DC motors
DC motors are all around you. They allow your cellphone to vibrate, they run laptop fans and they run the dvd drives.
Parameters of motors
- direct-drive vs. gearhead—built-in gears or not
- voltage—what voltage it best operates at
- current (efficiency)—how much current it needs to spin
- speed—how fast it spins
- torque—how strong it spins
- size, shaft diameter, shaft length,etc.
Characteristics
- When they first start up, DC motors draw a lot more
current, up to 10x more.
-
They draw a lot of current when they stall
- They can operate in either direction, by
switching voltage polarity
Polarity controls the direction - They usually spin very fast: >1000 RPM
- To get slower spinning, you need gearing
To drive a motor, apply a voltage.
The higher the voltage, the faster the spinning.
Just as voltage causes rotation, rotation causes voltage.
The circuit for motors is more complex than prior circuits you have worked with. In addition to using familiar components, you'll also be using a DC Motor, diode , and a transistor.

Diodes
Diodes allow current to flow only in one direction. Motors often create current spikes as they turn on and off. These spikes can damage the transistor, so you use a diode to protect the transistor from the spikes. Diodes have polarity, so the direction you install them matters. The bar in the schematic diagram corresponds to a white stripe on the actual diode. Current can only flow in the direction of the triangle. Since motors can act like generators, you need to prevent them from generating kickback into the circuit.Once a motor starts spinning, its inertia keeps it spinning, this turns it into a generator and thus can generate a kickback voltage. The kickback diode routes that voltage harmlessly back into the motor so it can't damage the rest of the circuit.
Kickback is also called back EMF
(EMF == electromotive force == voltage)
Transistors
Transistors have three terminals: The base is the input that you use to open and close the switch across the collector and emitter.On this type of transistor (called an NPN, you need to make sure the collector is always more positive than the emitter. Generally you do this by connecting the emitter to ground.
Because the motor requires much more power than the Arduino is capable of providing, you use batteries to power the motor. The transistor allows you to control the amount of current that flows from the battery through the motor.
Transistors are used to run loads larger than 5V.


Typical Tip120 circuit

More on Controlling Motors
You can control speed of motor with analogWrite() just like you can control the brightness of a LED.If you use a small motor, you can wire it up on the same breadboard:
Here's the Arduino code:
int motor=___;
char val;
void setup(){
pinMode(_____,OUTPUT);
Serial.begin(19200);
Serial.println("enter speed number 0-9");
}
void loop(){
val=Serial.read();
if(val>='0' &&val<='9'){
val=val-'0'; //converts from char to num
val=28*val; //converts num to 0-255
analogWrite(____,___);
Serial.println("enter speed number 0-9");
}
}
Bigger Motor
Here's a DC Motor with a pot

H-Bridges
The SN754410 is a handy IC that allows you to control the speed and direction of a DC motor with only one PWM output and two digital outputs from your Arduino board
The first half of the 270° degree turning angle of the potentiometer will make the motor run forward. The motor's speed will decrease the more the knob is turned toward the potentiometer's center position. The second half of the turning angle will make the motor run in reverse, with increasing speed toward the 270° mark. the percentages in the following picture are related to the motor's speed.


/*
* Arduino code for SN754410 H-bridge
* motor driver control. A potentiometer's
* 270 degree rotational angle is used to
* control a DC motor in the following way:
* 0-135 degrees - motor runs forward, speed
* getting slower the higher the angle, motor
* stops at 135 degrees.
* 135-270 degrees: motor runs backward,
* speed is getting faster the higher the
* angle, motor speed is 0 at 135 degrees.
*
* copyleft Feb. 2008, Fabian Winkler
*
*/
int potPin = 0; // analog input pin 0 for the potentiometer
int speedPin = 9; // H-bridge enable pin for speed control
int motor1Pin = 4; // H-bridge leg 1
int motor2Pin = 3; // H-bridge leg 2
int ledPin = 13; // status LED
int val = 0; // variable to store the value coming from the potentiometer
void setup() {
// set digital i/o pins as outputs:
pinMode(speedPin, OUTPUT);
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH); // status LED is always on
val = analogRead(potPin); // read the value from the potentiometer
val = val/4; // convert 0-1023 range to 0-255 range
if (val <= 127) {
// put motor in forward motion
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
// control speed based on angle of potentiometer
analogWrite(speedPin, 254-(val*2)); // output speed as PWM value
// this value needs to go from 254 to 0 for input values
//from 0 to 127
}
// put motor in backward motion
else {
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
// control speed based on angle of potentiometer
analogWrite(speedPin, (val*2)-256); // output speed as PWM value
// this value needs to go from 0 to 254 for input values
// from 128 to 255
}
}
/* simpleMotor using th HBridge
* ----------------
*/
//the H bridge takes two outputs from the Arduino to control the motor.
int motor1Pin = 4;
int motor2Pin = 3;
// there is one switch
int switchPin = 1;
//declare the state variable
int state = 0;
void setup() {
//the motor control wires are outputs
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
//the switch is an input
pinMode(switchPin, INPUT);
}
void loop() {
//read the switch
state = digitalRead(switchPin);
//based on the state of the switch alternate the control pins to change he direction of the motor.
switch (state){
case 0:
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
break;
case 1:
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
break;
}
}
/ / Steppers
Stepper motors are good for (semi-)precise control.
Stepper motors move in a series of steps. A stepper motor is controlled by a series of electromagnetic coils. The center shaft has a series of magnets mounted on it, and the coils surrounding the shaft are alternately given current or not, creating magnetic fields which repulse or attract the magnets on the shaft, causing the motor to rotate.
This design allows for precise control of the motor: by proper pulsing, it can be turned in very accurate steps of set degree increments (for example, two-degree increments, half-degree increments, etc.). They are used in printers, disk drives, and other devices where precise positioning of the motor is necessary. Steppers usually move much slower than DC motors, since there is an upper limit to how fast you can step them (5-600 pulses per second, typically. However, unlike DC motors, steppers often provide more torque at lower speeds. They can be very useful for moving a precise distance. Furthermore, stepper motors have very high torque when stopped, since the motor windings are holding the motor in place like a brake.
In order to wire a stepper motor, you need to know whether the motor is bipolar or unipolar. If the motor has 4 wires, assume it's bipolar and if it has 5 or 6 wires, it's unipolar. To figure out which wires go where, you'll need to do some investigation.
Unipolar Motor
If you have 6 wires, two of those will be the center taps and should both be connected to your voltage. If you consider just one of the coils, there are 3 wires involved - the center tap, and the two ends. The resistance between the two ends should be twice the resistance between the center tap and either of the ends.In 5 wire steppers, 2 center taps are already connected for you, so it's difficult to say definitively which wires belong to which coils from a simple test of resistance. In any event, the resistance between the center tap and each of the other wires should be half the resistance between any 2 of the other wires. In this case, you don't know which of the 4 wires are paired, so as long as you get the center tap in the right place, a little trial and error will be the best strategy. might stutter a bit, but it won't hurt the motor.
example 1:

example 2:
bipolar
#include <Stepper.h>
// change this to the number of steps on your motor
#define STEPS 100
#define ledPin 13
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 8, 9, 10, 11);
// the previous reading from the analog input
int previous = 0;
void setup()
{
// set the speed of the motor to 30 RPMs
stepper.setSpeed(30);
pinMode(ledPin,OUTPUT);
blink(3);
Serial.begin(9600);
}
void loop()
{
// get the sensor value
int val = analogRead(0);
Serial.println(val);
// move a number of steps equal to the change in the
// sensor reading
stepper.step(val - previous);
// remember the previous value of the sensor
previous = val;
}
// Blink the reset LED:
void blink(int howManyTimes) {
int i;
for (i=0; i< howManyTimes; i++) {
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
}
}
/*
Stepper Motor Controller
language: Wiring/Arduino
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 and 9 of the Arduino.
The motor moves 100 steps in one direction, then 100 in the other.
Created 11 Mar. 2007
Modified 7 Apr. 2007
by Tom Igoe
*/
// define the pins that the motor is attached to. You can use
// any digital I/O pins.
#include <Stepper.h>
#define motorSteps 200 // change this depending on the number of steps
// per revolution of your motor
#define motorPin1 8
#define motorPin2 9
#define ledPin 13
// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2);
void setup() {
// set the motor speed at 60 RPMS:
myStepper.setSpeed(60);
// Initialize the Serial port:
Serial.begin(9600);
// set up the LED pin:
pinMode(ledPin, OUTPUT);
// blink the LED:
blink(3);
}
void loop() {
// Step forward 100 steps:
Serial.println("Forward");
myStepper.step(100);
delay(500);
// Step backward 100 steps:
Serial.println("Backward");
myStepper.step(-100);
delay(500);
}
// Blink the reset LED:
void blink(int howManyTimes) {
int i;
for (i=0; i< howManyTimes; i++) {
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
delay(200);
}
}/ / Paper Puppets
This project was inspired by work found on the Knotology of Heinz Strobl site:http://home.tiscali.nl/gerard.paula/origami/knotology.html
- Gather the following:
- paper
- wire cutters
- wire
- popsicle stick
- cd case
- servo
- dowel
- tape
- drill
- glue gun
- Collect strips of paper at least 1 inch wide. Using colors helps keep the strips straight, but is not essential
- Fold 1 inch squares in each strip
- Cut the strips down into lengths of six squares each.
- You will need 9 six-square strips of each color.
- Lay two strips down like the image below. Pay attention to the number of squares in each direction.
- Add the third strip:
- Fold the third strip around like this:
- Fold the second strip over the top like this:
- You should stop folding and tucking when you have a shape like this with 2 squares of the first strip on one side and one square of the same strip on the other:
- Make nine boxes:
- Get a strip the length of 10 squares. I find it's easier to attach the boxes when this strip is a little narrower than 1 inch:
- Fold the 1 square of strip one of the box over one square of the ten-square strip.
- Then fold the two squares of the box over and through itself:
- Attach the second box like this:
- You should have a chain like this:
- Add the third box on the inside. This will be the center later on:
- Here's the chain:
- Add the fourth on the outside:
- Add the fifth also on the outside. This will be the bottom:
- Here's the chain:
- Add the sixth to the outside
- Your chain should look something like this:
- Thread the strip through the middle box:
- Add another box:
- Thread the strip through the bottom of the top box:
- Get twos strip of 7 square lengths. Again, a little narrower makes threading the strip easier.
- Thread the strip through two outside boxes and attach another box between them:
- Tuck the seven-square strip into the adjacent boxes
- You should have something like this:
- Repeat the process for the other side:
-
Here's the finished shape
- You can create a square by pulling either of the last two boxes down. This is great because a servo can automate this for you.
- Separate the two sides of a CD case and use the longer side:
- Drill holes in the plastic or whatever you are using for your base:

- Drill holes in the popsicle stick:

- Attach the popsicle stick to the servo. Use the screw and add some hot glue to keep it fixed in place
- Cut your wood so that once the servo is attached, the popsicle stick can rotate from 0° to 180° without hitting the base:

- Cut wire and thread it around one of the outside boxes
- Then through the hole in the base:
- Attach to the popsicle stick the popsicle stick:
- Do the same for the other side:
- Adjust the lengths of the wire so that the boxes form against the top of your base when the wires are pulled down:

/ / Finger
- Collect the following:
- Tubing
- Something to cut through the tubing
- Something to mark the tubing
- Wire
- Servo
- Popsicle stick
- Wire cutters
- x-acto knife
- Base
- Cut off a 6 inch piece of tubing
- Make marks near the end of the piece for the joints:
- Cut v-shapes out on the inside for the first joint:
- Repeat for all joints:
- Cut some wire
- Thread the wire through the tube and make a loop through the last knuckle:
- Tape the end:
- Wrap the wire around the other end, then tape:
- Cut another piece of wire and thread it through the loop at the bottom
- Connect a popsicle stick to the back, just behind the bottom joint:
- Tie a knot
- When you pull the wire, the finger should bend
- Make a slit in your base for the popsicle stick
- Also make a hole for the wire. Stick both through the base.
This view is from underneath:
- Mount your servo inside your base and attach the wire:
- When the servo moves, your finger should bend and straighten:








