In certain cases you may need to use motors for different purposes. Depending on the experiment or the device you are building, the type of motor to use can be different. In this series of posts I will show you how to use those motors with Arduino.
Servo Motors
Servo motors are motors for which you can control accurately enough the speed and the angle of rotation. They can be used to precisely move parts in your device. For examples, in physics experiment you can use a servo motor to hold a ball in a given position and release it at a given time to study its motion.
As most of the motors, servo motors can just rotate around their axis. However, with a bit of mechanics you can realise any kind of movement. To have an idea of what you can do with a rotating axis and some mechanics, just read the article on http://makezine.com/2015/04/20/understand-1700-mechanical-linkages-helpful-animations/.
Controlling a servo motor is incredibly easy with Arduino. Servo motors have three wires: usually coloured in black, red and white. The black one must be connected to the GND pin of the Arduino, while the red one to the 5V pin. They are used to feed the motor with some power. The third wire is used to control it. Any servo motor works receiving voltage pulses of a given width. Pulses are repeated at regular intervals (e.g. 20 ms), while the duration of a single pulse determines the rotation angle of the motor around its axis or its speed.
Given the working principle of a servo motor, you will not be surprised to know that you need to connect the control wire to one of the PWM (Pulse Width Modulation) pins of Arduino. PWM pins can be configured such that they provide voltage pulses at given interval with the chosen duty cycle. The duty cycle of a PWM pin is the faction of time for which the pin is at its highest voltage. An excellent tutorial on PWM pins is given here.
Putting the pin to zero makes the PWM pin to stay off, while putting it at its maximum value of 255 makes the pin to stay always on, i.e. with a duty cycle of 100%. In order to observe a square wave on the pin you must put it at 128, such that f=128/255=0.5 and the duty cycle is 50%. Any other number makes the pin to produce a square wave whose pulse width ranges from 0 to T, where T depends on the Arduino flavour. On the Arduino UNO T is about 2 ms.
With Arduino you don't need to compute the values yourself to control a servo motor. The Servo library is in fact available for use and provides all the relevant configurations automatically. Consider the following sketch:
#include <Servo.h>
Servo myservo;
void setup() {
myservo.attach(9);
}
void loop() {
myservo.write(90);
delay(1500);
myservo.write(0);
delay(1500);
}
The attach() method in setup() just tells Arduino to which pin the motor is attached, represented by the object myservo, defined as a member of the main class (Servo myservo). In the example the servo is attached to pin 9. You can then provide an angle, in degrees, to the write() method. Its effect depends on which type of servo motor you have.
There are, in fact, two types of servo motors: continuous rotation and standard. The write() command makes a standard servo rotate of the specified angle. A call to
myservo.write(90);
with a standard servo makes it rotate of 90 degrees. With the above sketch a servo of this type rotates of 90 degrees, then holds that position for 1.5 s, before returning at its initial position, where it stays for 1.5 s. Being in the loop the above rotations are repeated continuously.
When a servo motor of this type is powered and is receiving a train of pulses, it resists to forces applied to it holding its position. Of course you cannot apply forces too large, if you do not want to damage your servo. The maximum torque you can exert on the servo axis is one of the characteristics you can find on the servo data sheet.
The maximum torque is usually measured in kg·cm and is the product of the mass of a weight suspended at a given distance on a beam mounted on the motor axis. Using a servo whose maximum torque is 10 kg·cm you can suspend 10 kg at a distance of 1 cm from the axis as well as 1 kg at 10 cm. The larger the distance, the lower the weight the servo can tolerate. Needless to say, it is not a good idea to use a servo close to its limit. If you need a given torque, choose a servo that can tolerate at least twice that torque.
In order to compute the torque in N·cm it is enough to multiply the torque in kg·cm by 9.8, i.e. by the gravitational acceleration. In this case a servo whose maximum torque is 10 kg·cm can tolerate a force acting 1 cm far from the axis of 10·9.8=98 N, or a force of 9.8 N acting at a distance of 10 cm from the axis.
Continuos rotation servo motors, instead, rotate clockwise or anticlockwise until they are powered. The number passed to the write() method represents the rotation speed: numbers from 0 to 89 make the motor axis rotate anticlockwise (seen from above), while numbers from 91 to 180 make it rotating clockwise. The maximum speed is attained for 0 or 180. The higher the number, the slower the speed if the number is less than 90. On the contrary, if the number is larger than 90, the speed increases with the number. Setting the speed at 90 makes the servo stop rotating. In the above sketch, then the servo rotates anticlockwise for 1.5 s, then stops for other 1.5 s and so on.
Here you can find the motor we used for our tests, but it is plenty of them in the Farnell Electronics store. Below is a video showing our continuous rotation servo working with the sketch outlined above.
This post appears on my personal blog, too (https://giovanniorgantini.wordpress.com/) and its content will be part of my freely available publication Scientific Arduino available here.