How to Control a Servo’s Angle of Rotation in Relation to a Coordinate

This tutorial explains how to control a Servo’s angle of rotation in relation to a coordinate that can be found in a typical grid of points.  You will need a(n):

  1. Arduino Uno
  2. Breadboard
  3. Servo (that can rotate 360 degrees)
  4. 6 Wires

Set Up 0

Setting It Up

Before we begin to put the parts together, make sure your Arduino board is not connected to your computer!

Set Up 2

First and foremost, the Servo needs to be connected to a proper source of power for it to work.  Make sure to plug in a red wire from the 5V pin to the positive end of the breadboard and to ground it by plugging a black wire from the GND pin to the negative end of the breadboard.

Then, connect your Servo to your breadboard with 3 jumper wires: orange wire (signal) to F8 (breadboard), red wire (+5V) to positive end of breadboard, and brown wire (ground) to negative end of breadboard.

On the same row as the Servo’s orange wire, connect a yellow wire from J8 (breadboard) to pin 9 (Arduino board).

In the end, it should look like this:

Set Up 1

Now, let’s make that Servo rotate!

Arduino Code

You will need to open up the Arduino interface before we can start writing some code.  Remember, your Arduino board is not connected to the computer, so uploading anything is completely impossible.  For your Arduino board’s safety, let’s keep it unplugged for now.

Firstly, you must include two different sets of libraries: (1) Servo.h and (2) math.h.  These two libraries provide you with the ability to use your Servo and to use the inverse tangent function.  Unfortunately, “pi” is not defined in either libraries, including “math.h”, so you must define it yourself.

We will now to declare a function that’s argument relies on the use of coordinates rather than separate x and y variables (see Diagram 1).

C:UsersTessaDocumentsArduinoprojecttutorialDiagram Model
Diagram 1: Coordinate System

Within the loop function, we will state the position of the motor in relation to the grid (see Diagram 1).  Now, we want the motor to angle towards the points located around it (which are diagrammed as circles).  In the code below, we will focus on 4 points (see Diagram 2).

C:UsersTessaDocumentsArduinoprojecttutorialDiagram Model
Diagram 2: Angle of Rotation in relation to Person on Grid

Now plug in your Arduino, upload the code and watch your Servo’s arm rotate!  You can also check out the angle of the arm in relation to the coordinate in your serial monitor!

If you like, you can expand on this tutorial by including sensors into the equation!  Instead of manually giving a set of coordinates, you can collect coordinates through a series of sensors within a built grid.  The motor’s arm will rotate to the individual’s position within that grid, thanks to the sensor!

One thought on “How to Control a Servo’s Angle of Rotation in Relation to a Coordinate”

  1. #include

    Servo servo;

    const float pi = 3.14159265359;

    float PersonX, PersonY;
    float MotorX, MotorY;
    float deltaX, deltaY;
    float angleRad, angleDeg;

    void setup() {

    It keep saying Error compiling, do you know why?

Leave a Reply

Your email address will not be published. Required fields are marked *