Now it’s finally time to act more decisively in the physical world, I mean, to move objects. Earlier we’ve used only the LED and piezo buzzer as executing devices. Today we’ll introduce a servomotor that will help us to construct different manipulators, “spiders” and so on. Our example will be a radar, we will build it to scan the space around it, which means we’ll have a distance sensor installed on a servomotor. Let’s take a closer look at this servomotor. We use a small hobby servo like this. As most servos do, it allows the angular position of the shaft to be changed if needed or to be stable if not. Of course, there are some limitations. Most servos are limited in the rotation range. We have 180 degrees, and it’s quite cool, because it could be 60 or 120, and certainly it can hold the position in the working range only. This servo won’t hold not only my body weight but also if I just push it with my hand. But it’s enough to move a distance sensor as well as for many other purposes. Let’s look inside the servomotor. First of all, we see here the motor itself, that rotates a small metal gear wheel. On above there’s a plastic reduction gearbox that shifts rotational speed and power. By the way it limits our servo even more, because it normally wears out sooner than the metal one. Next, there’s a potentiometer, the pot we’ve met earlier inside the servo. It helps the servo to understand the current shaft angle. Also there’s an electronic control board that allows us simply to send a command from the Arduino board, like “hold the angle”, and later the servo control board will do the rest. Let’s connect it. Concerning the servo’s connection, there’s a triple cable almost familiar to us. It has slightly different wire colouring: the brown one is for ground, the red, as always, is for power, and the signal wire, through which the servo receives commands, is the orange one. Note that the signal wire transmits data only one way, I mean, motor can only receive commands but not to notify the controller of the actual state. The motor is self-controlled and doesn’t report to Arduino. The triple cable like this looks likely to be connected via the Troyka shield, and we’ll do that now. But I’d recommend not to connect more than one servomotor to the Arduino, because the latter would not be able to deliver sufficient power. Ideally, you should give the motor a separate power supply. Experience, however, have shown that in most cases Arduino can supply one small servo. A servomotor like this, much more powerful one, I can’t recommend to connect it here. It needs a separate power supply, otherwise even if you don’t damage the Arduino, the moment the motor starts rotating under heavy load, it would consume too high current, the Arduino power supply would drop, and the controller would restart. Thus instead of working properly, your program would restart again and again trying to rotate the servomotor. So I’ll turn the power off and reattach the small servo, for instance, to the 9th pin. [PAUSE] The brown wire is for ground, the red one is for power, the orange is for signal. How are we going to control the servo? The small board we’ve seen inside the servo receives pulses. It uses pulse duration modulation, in other words, pulses can have different width but same frequency. The servo controller board expects one pulse each 20 milliseconds, and depending on the pulse width, the controller will change the angle. Sure we could write all the code by ourselves, turn the voltage on, wait for some milliseconds, turn it off, wait the time left up to 20 ms with the pin off and so on. But we have an instrument ready to use, and it’s much easier. Let’s switch back to the Examples where we should find the Sweep in the Servo submenu. I’ve just opened it, let’s look inside. Here, let me introduce the new stuff - Dynamic Libraries, that’s very common and useful. The example begins with include <Servo.h>. What is <Servo.h>? It is so called header file of the Servo library. All the functions the library creator designed to work with devices are described there. For example, this particular library is designed to control servos. Apart from the header file, we normally have the implementation file with CPP extension here, but it’s out of the topic now. We want to add the library to our sketch. We can do it with the #include statement, and so the compiler will know that we can use functions declared in the library even if we haven’t declared them in the sketch. What’s going on further? We introduce the new line: Servo myservo; here we create the new object that is myservo. We won’t examine objects precisely, for now we will treat them like special data type. Actually, it’s a combination of variables, data structures and functions we can implement to them. Now we have there a servo type object. It’s been declared in the library. In most cases we don’t even need to look inside the library, because they are often well defined and so due to the descriptions we can easily understand, what we are going to have at functions inputs or outputs and what data they use. In the Reference section on the Arduino website you can always find the Servo library description. It’s standard library, it’s always included in distribution. Here you can study in detail the methods, that is the functions we can use with Servo-type objects. But now we are going to see how we implement them. First of all, in the Setup, we see the line myservo.attach(9);. Attach method is used to attach a servo device. As you see, there is no pinMode(9) line, because it’s been already included in the attach method, and the pin 9 is the pin we have attached the servo named myservo to. If we need more than just one servo, we should create a new object, for example, myservo1, and we will have to write myservo1.attach and to specify another pin. What’s next? In the main cycle, in the Loop section, we see two count-controlled for loops. One counter increments from 0 to 180 by 1, another one in reverse goes from 180 to 0. Inside the loops we call one more Servo library method - myservo.write(pos);. What is that write? It helps us to tell the servo the shaft position. The position is stored in the pos counter variable. So at each iteration we tell the servo to go to the position 0, next as the counter value incremented by 1, to go to the position 1 and so on up to 180 degrees. Each shaft movement is followed by 15 milliseconds waiting. It’s not useless at all, because one thing is to send a command to servo, another one is to physically rotate the shaft especially if it’s loaded with something. It’s not instant. That’s why if you are going to pull the servo very often, especially in different directions, especially loaded, you will most probably get nothing but the gearbox worn out. Keep it in mind! The same way works the second loop, but the counter variable pos goes in opposite direction, from 180 to 0. Let’s load the sketch and see what’s going to be with the servo. I’ve already put on the servo this white part - so-called mount, they can look different, they are used to physically connect the servo shaft with different devices we want to move. We will move a distance sensor, we will mount it a little bit later, though it could be any other device or, for example, a manipulator joint. So you see the sketch has loaded and the servo has started to rotate either opposite directions in the 180 degrees range. Actually, I see it’s a little bit less, but you can always check what actual range your servo has. Great! We can now move the radar and scan the space around. Note that when you include the Servo library in the sketch, and start using it, PWM on pin 9 and 10 terminates. You already know what is a pulse-width modulation, so never forget that the tone turns it off on 3 and 11, and servo on 9 and 10.