IAN LANG ELECTRONICS

# CIRC-04

Go Back

You're going to need a separate power supply for this circuit as it takes some juice and the USB can't deal with it. You could use a 9V battery. I used a regulated mains adaptor running at 9V because, out of three 9V batteries I had lying about, none were any good. What're the chances, eh? Your servo that comes with the kit may do a lot of humming and clicking, don't worry, it's just that although it isn't a bad quality it's far from the best. But the best cost closer to £100, so let's forgive it it's little foibles.. Right then, let's look at the circuit.

Don't mix up the signal (white) wire and the 0V (black) wire when you connect the servo to the headers or you'll wonder what's happening. Assuming all has gone well, you'll hear the servo turning and if you look closely you'll see it too.

Put one of the supplied followers on to the gear of the servo and upload the code. If you do this step with the servo still turning you'll notice a surprising amount of torque (turning force) is present. If you upload the code supplied by Oomlout, Servosweep, you'll

see that the servo turns first one way and then the other, but it only does it halfway round. This is what a servo does. Those of you who have never met a servo before  may now be wondering what's the point? Well, a servo is used not for turning as a motor does, but for positioning. Here's an example. Take the supplied potentiometer, put one end terminal to ground and the other to 5V and the middle terminal to pin A5 via jump wires and upload the following code:

// Controlling a servo position using a potentiometer (variable resistor)

// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = A5;  // analog pin used to connect the potentiometer

int val;    // variable to read the value from the analog pin

void setup()

{

myservo.attach(9);  // attaches the servo on pin 9 to the servo object

}

void loop()

{

val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)

val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)

myservo.write(val);                  // sets the servo position according to the scaled value

delay(15);                           // waits for the servo to get there

}

Now turn the potentiometer. The servo  should follow your movements exactly. If you are turning clockwise and the servo goes anti-clockwise and vice versa, it means you have mixed up the jump wires on the end terminals, so just swap them over. Notice how when you stop turning the potentiometer, the servo stays where it is. Turn the potentiometer so that the servo is halfway through the maximum range, and pull out one of the wires connected to the end terminals of the potentiometer. The servo will go to the end of its range. Put the wire back, and the servo returns to where it was. Pull the wire out from the other end of the potentiometer, and the servo  goes right to the other end of its range. This is the main application of the servo; it can be electrically or manually turned to very high degree of precision and so its used to place things in position. It can be mechanically linked to other devices too, and so can move their positions. It could for example be used to operate a valve, as when you turn off the power, the servo (hooked up to potentiometer like this) will stay in the position it was placed. It has a gearbox inside, producing large amounts of torque. So how does it do this?

Inside the servo are the following: an electric motor, an operational amplifier and its associated circuitry, a gearbox and a potentiometer, which is just like the one we've just used to turn it. The potentiometer inside feeds the operational amplifier with the voltage running through it . A pulse is sent through the control wire and the width of the pulse determines the reference voltage recieved. if the reference voltage is greater than the feedback voltage, the servo turns one way, if lesser, the other. Depending on the make and model of the servo the pulse interval could be every 20 mS and the maximum width of the pulse (i.e how long it's high for) some 1.75 mS leaving a minimum off time of 18.25 mS.

Yes, I'm going to start banging on about PWM (Pulse-Width Modulation) again. Sorry. But if it helps, here's a lovely drawing:

The green spaces represent the width of the pulse:

at minimum width the angle the servo is at is 0 degrees.

at half width, the angle is 90 degrees

and at full width 180 degrees.

Minimum width is not zero because then the servo would not know when to expect the next pulse, and the whole thing goes belly-up. Pull out the jump wire supplying the control (white wire) and turn the potentiometer. Precisely nothing will happen.

The Arduino has a library file to control the servo and so we don't need to work out the PWM and code it up. It's called in the above line by this statement:

#include <Servo.h>

and this is a complex piece of  programming written by Michael Margolis which you will find in the libraries section of wherever you installed the Arduino software. You can open it and look if you want to.

Since the servo works on a pulse of  x  milliseconds delivered every y milliseconds it is of course possible to commune with it directly. Try the following code:

int servoPin = 9;

int   pulseTime;

void setup(){

pinMode(servoPin,OUTPUT);

}

void loop() {

for (int j=1;j<500;j++){

pulseTime = 1000;

digitalWrite(servoPin, HIGH);

delayMicroseconds(pulseTime);

digitalWrite(servoPin, LOW);

delay(25);

}

for (int j=1;j<500;j++){

pulseTime = 2000;

digitalWrite(servoPin, HIGH);

delayMicroseconds(pulseTime);

digitalWrite(servoPin, LOW);

delay(25);}

}

What should happen here is that the servo supplied should describe an arc of about 90 degrees. It should do this in two movements, one end to the other, staying at each end for about 12 seconds or so.

The position it occupies is determined by pulseTime, and the length of time it stays there is determined by the operator j in the for next loop. Play about with these numbers, if you make one j in a for next loop smaller than the other it'll stay in one position for longer than the other. If you change the pulseTimes, it'll change the position.

Personally I've always found making servos behave themselves one of the hardest things to do in electronics, but you can see how your Arduino makes it very much easier. But what if we want to make it sweep in the same way as we did when we used the library file servo.h ?

A subtle change to the code is what we need like thus:

int servoPin = 9;

int   pulseTime;

void setup(){

pinMode(servoPin,OUTPUT);

}

void loop() {

for (int j=1000;j<2000;j=j+10){

pulseTime = j;

digitalWrite(servoPin, HIGH);

delayMicroseconds(pulseTime);

digitalWrite(servoPin, LOW);

delay(25);

}

for (int j=2000;j>1000;j=j-10){

pulseTime = j;

digitalWrite(servoPin, HIGH);

delayMicroseconds(pulseTime);

digitalWrite(servoPin, LOW);

delay(25);}

}

You'll see that in the for/next loops j is being either increased or decreased by 10. These are what is controlling the speed of the sweep. If you change these to +1 and -1 it will go through very slowly indeed.

Copy & Paste Code from Here: