How to rotate the servo more smoothly
Makes

How to rotate the servo more smoothly

Kerry Kidd
Kerry Kidd

Bearing in mind that the rotation movement of a servo motor (180 °) is a “sudden” movement, I will show in this tutorial, how to control the rotation of the servo, giving it a smoother movement.

Conrado Sena de Alencar

Conrado is 43 years old, he has a degree in Mathematics and is passionate about programming logic. he's taken several programming courses and has worked for 15 years in a large private school in the city of Vitória, Espírito Santo, Brazil and he is the Coordinator of Educational Informatics.

When we program the micro: bit and experience one of its infinite possibilities, even if they are simple like a winking heart, a dice or a rock-paper-scissors game, we feel fulfilled, ecstatic, we look like children with toys, this is the purest truth. So with peripherals, we feel like high-level scientists, it's wonderful to test some applicability and see it working.


microbit.org brings several of these applications in the form of articles and tutorials, some using the servo motor, with 180 ° rotation, such as Inchworm, Milk Cartoon Robot, Milk Monster and Railway Crossing, among others.


At the end of last year, I found a tutorial to assemble a cardboard reindeer, however, the tutorial used a DC motor with reducer, which I didn't have (I don't have it yet). But the reindeer was “so cute” (words of my daughter Angela, seven years old) that I decided to use the servo motor.

Fig 2 - reindeer sample with servo motor
Fig 2 - reindeer sample with servo motor

The problem was the rotation speed of the servo, if the reindeer were real, he would have terrible neck pains. Not to mention that, because it is made of cardboard, and consequently has a light structure, it rocked a lot and ended up falling.

I decided to seek help from social groups about the micro: bit. I wanted a solution to my problem: to decrease the rotation speed of the servo, to give a smoother movement to the reindeer head. But I couldn't find it, not even an extension inside the MakeCode Editor.

Then I started to study and debug all the code that I could find and that involved a servo motor, and, based on what I saw, I finally managed to develop simple code,  that met my demand.

The purpose of this tutorial is to show you how I managed to control the speed of rotation of the servo, giving it a smoother movement, because, as we know, its movement is sudden, which makes some applications “strange”, as in the case of my reindeer with neck pain.

Creating the code

Before creating the code, keep in mind that the variable names and values ​​used were defined according to my application, feel free to make the necessary changes.

Variables

In order to be able to control the rotation speed of the servo, a variable is required. I called it angle and assign 60 to its initial value.

Thus, whenever the application starts, angle will have a value of 60.

setting angle to 60 onstart
Setting angle to 60 onstart

Servo Motor

Using pin 0 as the connection for the servo, and the initial angle of 90 ° is assigned to it. So, it will always go to that angle
Using pin 0 as the connection for the servo, and the initial angle of 90 ° is assigned to it. So, it will always go to that angle

Rotation of the servo

For the application, I chose the maximum and minimum angles to be 120 and 60 degrees, respectively. Therefore, the servo can only rotate until it reaches these values. Starting at 120, and, keeping in mind that repetition is necessary, we have:

IMAGE 03

This way, this block will only execute its commands while the variable is smaller than the angle defined by me.

Now I increase the angle by a certain value (0.5) and I define that this will be the new angle of the servo, so, if it was in the 90 ° angle, it will now be in the 90.5 ° angle and will be increased with each new cycle. And yet, for the effect to be effective, it is necessary to pause (I chose 10 ms) the commands, otherwise, the movement of the servo will continue to be sudden.

IMAGE 04

Finally, after reaching the limit angle, I take another break (2000 ms) before starting to decrease the angle value.

IMAGE 05

Now, just repeat the while block making the necessary changes:

IMAGE 06

Finally, the final schedule will look like this:

IMAGE 07
Completed Code

Keep Programming!