text: Objectives
  - What is a servo?
  - Understand the difference between a servo and a DC motor.
  - Introduce the C++ library concept.
  - The map() function.
  Bill of materials
  WHAT IS A SERVO?
  Normally what the usual motors do is to transform electrical energy (or chemical) in a continuous rotation we can use to develop mechanical work.
  In the chapter 13 we use a small DC motor and regulate its rotation speed by means of an Arduino PWM signal.
  The servos are also DC motors, but rather than be designed for a continuous rotation we can make good use of (to move a wheel for example), they are designed to move to a fixed angle in response to a control signal, and remain fixed in that position.
  Think of, for example, the rudder of an airplane or boat. We do not want a continuous rotation, but indicate the rotation angle in degrees and keep that orientation until we give an order to the contrary.
  These servos or actuators are very common in model airplane making and robotics, because of their ability to move to a specific angle and stay there. In fact they are often designed so that the rotation angle be proportional to a PWM signal, being their control very accurate.
  A servo is usually composed of:
  - The drive gears.
  - A DC motor.
  - A control circuit
  In practice it behaves as a functional block that positions its shaft at a precise angle based on the control signal.
  Servos usually has a margin of operation, i.e., they can move between 0 and a given angle, which is usually 180 degrees. But there are commercial models of all imaginable features, including 360º servos.
  Typically these small servos operate on 5V and their control is performed by a PWM control signal, wherein the pulse width indicates the angle we wish the shaft to adopt.
  A servo has a 3-wire connector: 5V (red), GND (black or brown) and control (yellow or white).
  We could write a small program in Arduino to control these pulses and relate them to the angle sought. But don’t panic, servos are so common that many people have written these programs before and have put them at our disposal.
  Thus we can handle a servo without worrying at all about how to control signals, or its internal operation, simply by installing these programs in a special form called library.
  - An Arduino library is a set of definitions and functions written in C ++, we can use in our programs. We can take a look at the code of these libraries, but usually a medium-high level in C ++ is required to understand its operation.
  The advantage of working in a platform as well established as Arduino, is that there are many people in the world developing libraries, for almost anything you can imagine.
  And there is something better, they are free.
  In further chapters we will use libraries for many things, but today we will start off with the first one, the servo library.
  USING OUR FIRST LIBRARY: SERVO
  Servo is a standard Arduino library. That means that is built-in when the IDE is installed, unlike other libraries that we must look for in Internet, download and install to use.
  To use a standard library, we just need to tell the IDE to include it in our sketch. Go to Sketch \ Include Library and you will see a dropdown list with all the IDE installed libraries.
  For now we are only interested in the Servo library, the third from the bottom in the above picture. Click on it and the following line will be included at the beginning of your sketch:
  #include <Servo.h>
  From now on we can use the available functions in the library.
  - Normally to use a library you have to read the documentation first, which describes how to use it, which are the available functions, which type of parameters we must pass them… etc.
  - As this is your first library, and we have already read the manual, we will take you by the hand. You will see that is easy as abc.
  We will assemble a prototype to move the servo in controlled manner, and we will see which instructions do we need.
  CIRCUIT SCHEMATIC DIAGRAM
  Let’s connect the servo first to GND and 5V and then the control pin to an Arduino PWM pin (remember this, otherwise it will be difficult for you to find out the problem). Bear in mind that is the pulse width which controls the angle.
  We will also connect a potentiometer to the pin A0 to play with the servo.
  The wiring schematic diagram is equally simple:
  THE CONTROL PROGRAM
  Let’s start with a line that already know, the include statement, and a new one:
  #include <Servo.h> Servo servo1;
  To use the library we must define a Servo type object, that we have called servo1.
  C ++ is an object-oriented language, this means that we can define objects (such as servos) just declaring a new object of the type we want. In programming jargon we say we create an instance of an object or instantiate a new object.
  Thus, the second line means that we will create a new instance of a Servo type object, called servo1.
  - Although now it may seem a strange way of doing things, Object-Oriented Programming (OOP for short), is one of the most powerful methods of current Computer Science and all programming languages that pride themselves, incorporate OOP.
  - Explain OOP in depth goes far beyond the scope of this humble tutorial on Arduino, so for now we will just show you how to use objects..
  Once created the object instance (and we can create as many as we need to handle multiple servos at once), we have to include a line in the setup() function to instruct C ++ to connect the abstract object, which is still servo1, to a physical Arduino pin to control it. In our case we will use the pin 9:
  servo1.attach(9);
  Having completed the procedure to govern the servo position we turn to:
  servo1.write(angle);
  Where the angle variable represents the value in degrees of the position we want the servo to move to. Easy, right?
  Let’s write a program that will sweep an angle in degrees and move the servo to that position.
  #include <Servo.h> // Includes Servo library Servo servo1; // Creates a Servo object called servo1 int angle= 0 ; void setup() { servo1.attach(9); // Attaches servo1 to pin 9 } void loop() { for(angle = 0; angle <= 180; angle += 1) // Increment angle by 1 { servo1.write(angle); delay(25); } for(angle = 180; angle >=0; angle -=1 ) // Decrement angle by 1 { servo1.write(angle); delay(25); } }
  You will see how the servo moves first forward and then backwards, and then starts again.
  Let ‘s now use the potentiometer to represent the servo position, so that the servo move according the potentiometer’s value. We must understand that the value read in the pin A0 ranges between 0 and 1024, but we must convert them to a angle between 0 and 180 degrees. So to calculate the angle just do:
  As experience has shown me, when we use formulae as the one above, errors tend to infinity. So I will take the opportunity to present the map() function, which does exactly that in a comfortable way, getting rid of the float data type (though in order to learn, you should use the formula first and later use the map() function).
  angle = map(analogRead(A0), 0, 1024, 0, 180);
  The formula means: convert the value read from the pin A0, which is a number between 0 and 1024, to a proportional value in the range between 0 and 180, then assign this value to the variable angle.
  Sketch 16.2Sketch 16.2
  #include <Servo.h> // Includes Servo library Servo servo1; // Creates a Servo object called servo1 int angle = 0 ; void setup() { servo1.attach(9); // Attaches servo1 to pin 9 } void loop() { angle = map(analogRead(A0), 0, 1024, 0, 180); servo1.write(angle); delay(250); }
  Summary
  - We have introduced servos and we have seen some easy sketches to use them.
  - We have introduced some OOP basic concepts so you can use the libraries.
  - We have defined libraries as sets of functions and objects that can be used without worrying about how do they work internally.
  - Libraries are an exceptional system to use electronic components without going into more or less complicated technical details. This opens up a huge potential of components at our disposal.
  - We introduced another function to our arsenal, the map() function.
  Give a Reply