Intro
In this workshop we'll take a look at electro-mechanical devices such relays, solenoids, motors (DC and stepper) and servos, as well as passive electronic components and programs to control these.
Transistors
When using electro-mechanical devices, we'll be dealing with currents that are larger than Arduino's IO pins can handle. Transistor is an electronic component that can help to amplify small electric currents (about 25mA) that the Arduino can provide.
There are two standard types of transistors, NPN and PNP. In this class we'll use NPN transistors to sink current from a positive voltage supply. Transistors usually have three leads, base (B), collector (C) and emitter (E). The rule is that small base current controls a larger collector current, so we can control high-current devices connected to collector by switching base current with the Arduino.


We'll use a simple program included with the Arduino examples to blink one or several LEDs connected via transistor to pin 13. In Arduino environment, open and run File > Sketchbook > Examples > Digital > Blink
int ledPin = 13; // LED connected to digital pin 13
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop() // run over and over again
{
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}
Relays
Relay is an electrical switch operated by an electromagnet. We can use a transistor to control the flow of current through the coil of relay's magnet, switching a pair of contacts on and off. Since relay contacts make a mechanical connection, it can be used to connect different types of signals (for example: AC voltage, audio, video) just like connecting and disconnecting a pair of wires.


Solenoid
Solenoid is a loop of wire, often wrapped around a metallic core, which produces a magnetic field when an electric current is passed through it. Essentially, it is the same thing as the electromagnet we find inside a relay, but substantially larger. Solenoids are useful for any kind of linear actuation, although radial solenoids also exist.

H-Bridge
Using a transistor, we can make a DC motor spin in one of two directions using an Arduino pin. In order to control the motor in both directions, an H-bridge can be used. An H-bridge is an electronic circuit which enables a voltage to be applied across a load in either direction. The term "H-bridge" is derived from the typical graphical representation of such a circuit, which can be built using four switches.

DC Motor with Half-H Driver
SN754410 is a quadruple high-current half-H driver designed to provide bidirectional drive currents up to 1 A at voltages from 4.5 V to 36V. The chip is designed to drive relays, solenoids, dc and bipolar stepping motors, as well as other high-current loads. It is functionally similar to the L293 driver IC, but is newer and cheaper.
.


A simple program to test running the motor in both directions:
int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
int enablePin = 9; // H-bridge enable pin
int ledPin = 13; // LED
void setup()
{
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(enablePin, HIGH); // set enablePin high so that motor can turn on
}
void loop()
{
// motor run forward
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
delay(2000);
// motor free
digitalWrite(enablePin, LOW);
delay(1000);
digitalWrite(enablePin, HIGH);
// motor run backward
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
delay(2000);
}
http://itp.nyu.edu/physcomp/Labs/DCMotorControl - SN754410 Tutorial
http://focus.ti.com/lit/ds/symlink/sn754410.pdf - SN754410 Datasheet
Stepper Motors
A stepper motor is a motor 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 very precise control of the motor: by proper pulsing, it can be turned in very accurate steps of set degree increments. They are used in printers, disk drives, and other devices where precise positioning of the motor is necessary.

We can the quad H-driver chip to control this type of motor as well.

Arduino's stepper motor library makes it really easy to try one out.
#include <Stepper.h> // include stepper library
#define STEPS 500 // the number of steps per revolution
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins
Stepper stepper(STEPS, 8, 9, 10, 11);
void setup()
{
stepper.setSpeed(20); // set the speed of the motor in RPMs
}
void loop()
{
stepper.step(STEPS);
delay(1000);
stepper.step(-STEPS);
delay(1000);
}
http://www.tigoe.net/pcomp/code/category/arduinowiring/51 - Stepper motor tutorial
http://arduino.cc/en/Reference/Stepper?from=Tutorial.Stepper - Arduino Stepper library
Servos
Servos are composed of an electric motor mechanically linked to a potentiometer. Pulse-width modulation (PWM) signals sent to the servo are translated into position commands by electronics inside the servo. When the servo is commanded to rotate, the motor is powered until the potentiometer reaches the value corresponding to the commanded position.

Since the servo package already includes motor driver electronics inside, we can control it directly from the Arduino. Red and black lines are power and ground and we'll connect the yellow line to pin 9.

Arduino comes with a library that already does all the PWM timing, so we can simply write the desired position value.
#include <Servo.h> // include the servo library
Servo myservo; // create servo object to control a servo
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
}
}
http://itp.nyu.edu/physcomp/Labs/Servo - ITP Servo Tutorial
http://arduino.cc/en/Reference/Servo - Arduino Servo Library
Arduino Motor Driver Shields
You don't have to build your own circuits from scratch to get motors hooked up to Arduino. Instead, you can use pretty inexpensive 'shields' designed specifically to match the Arduino pinouts.

This shield by Adafruit Industries costs $19.50 and is good for connecting 2 servos, 4 DC motors or 2 stepper motors.

Sparkfun's Ardumoto shield, priced at $24.95, is based around L298 H-bridge that can drive 2 DC motors.
Parts List
Bill of materials used in this workshop, minus the miscellaneous parts we already had in stock.
| ITEM | DESCRIPTION | PRICE |
|---|---|---|
| SN754410 | Quad Half-H Driver | $1.65 |
| G14197 | Small bi-polar stepper motor | $1.99 |
| G9332 | Johnson DC Motor (6V-24V) | $1.49 |
| G16036 | Spring-return Solenoid (12V-24V) | $0.99 |
| ROB-09064 | Servo | $12.95 |