User Tools

Site Tools


courses:intro.prototyping.spring.2012.mar26

Servos and motors

Servos

A servo is an electric motor usually used in radio controlled vehicles and contraptions. Useful because of the high degree of control you can have over it's position. They come in different sizes and strengths, but mostly, you get to choose between two functions.

The normal function is that a servo will be able to turn 180 degrees and in your programming you can decide what degree the servo should position itself on, and it will move there as fast as it can. Which is not that fast.

To control the servo you use functions from a prepared library of functions called servo.h that now is a part of the Arduino programming environment. The functions in this library are:

attach(pin#) == specify which pin controls your servo

write(degrees) == position the servo

writeMicroseconds(microSeconds) == another way to position

read() == gives you the current angle

attached() == checks if a servo is attached

detach() == release the servo

There are also “full rotation” or “continous” servos, that has no stop and will potentially turn forever. You can use the same commands as for a normal servo, but instead of a position in degrees, your number will decide the speed and direction your servo will move. 90 will mean standing still, 0 full speed one way, and 180 full speed the other. And numbers in between….well you get it. Because servos are based on analog electronics, standing still might not be EXACTLY 90. You might have to tweak that number to find what stands still for your servo.

What's neat about servos is that you can run them off the arduino itself.

A simple example of just making the servo move degree by degree:

#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 
  } 
} 

BUT that's pretty lame. Instead come up with an example on how to control the servo. Connect a sensor, button or potentiometer as input. Go on. I dare ya!

Motors

Another means of motion is the regular DC motor that comes with many toys. You can't really control them in detail, but you get speed! In the workshop we also have DC motors connected to small gearboxes that will give you slower but powerful action. Running DC motors uses more power than is healthy to squeeze out of an arduino. Luckily we have a bunch of motorshields to use.

Motorshields can help you run motors (and also servos) but will make use of an external powersource without punishing the arduino. Another good reason to use the motorshield is that to make a DC motor turn the other way you physically have to change polarity (reverse plus and minus), which is difficult if you're not doing it yourself. But the motorshield does that FOR you.

To make it easier to use servos and motors with the motorshield, there is a library with prepared functions. Download the zipfile and extract it in a folder called “libraries” in the folder you keep your projects in.

Here's a how to make a DC motor do..well…pretty much all you can do with it. You try!

#include <AFMotor.h>
 
AF_DCMotor motor(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
 
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");
 
  motor.setSpeed(200);     // set the speed to 200/255
}
 
void loop() {
  Serial.print("tick");
 
  motor.run(FORWARD);      // turn it on going forward
  delay(1000);
 
  Serial.print("tock");
  motor.run(BACKWARD);     // the other way
  delay(1000);
 
  Serial.print("tack");
  motor.run(RELEASE);      // stopped
  delay(1000);
}
Stepper motor

There is a third kind of motor called Stepper motor which is sort of inbetween. You can control it's movement in a fairly detailed way, but you have less control of the exact position than with a servo. Regrettably, the motors I had planned to have in time for the lecture haven't arrived, so I'll get back to that.

Done? Bored? Do you want to try the printer?

Resources
courses/intro.prototyping.spring.2012.mar26.txt · Last modified: 2012/03/23 06:30 by 130.239.235.163