CODING: SERVO

The way we control a servo (like the MG90S) is by sending it quick little bursts of electricity through the signal wire — these bursts are called PULSES.

The servo doesn’t care about the power wires (red and black/brown) for positioning; those just give it energy to move. The magic happens on the orange/yellow/white signal wire. The Arduino (or whatever is controlling it) sends a pulse every ~20 milliseconds (that’s like 50 times a second — super fast!). Each pulse is ‘ON‘ (high, like 5V electricity flowing) for a very short time, then ‘OFF‘ (low, 0V) the rest of the time. The key thing the servo listens to is how long that ‘ONpulse lasts — that’s the pulse width:

  • A short pulse (about 1 millisecond long) tells the servo: ‘Go to 0 degrees!’ (one end of its swing).
  • A medium pulse (1.5 milliseconds) tells it: ‘Go to the middle, 90 degrees!’ (straight ahead, neutral).
  • A long pulse (2 milliseconds) tells it: ‘Go to 180 degrees!’ (the other end).

The longer the electricity is ‘ON’ during each quick burst, the farther the servo arm turns and holds position. It’s like Morse code, but instead of dots and dashes for letters, the length of the ‘dot’ (the on-time) tells the servo exactly where to point — and it checks this 50 times a second to stay locked there!”

Let’s code the servo to move TURL-X’s head.

The code below moves the servo to 90 degrees.
If you secured the head on properly, with TURL-X looking straight ahead, running this code should not move the head as it should already be at 90 degrees.
 

Let’s go through the code line by line.

The first line:   #include <servo.h>

 
We are including a library called servo that lets us move the servo to a specific angle by just writing in the angle.
 
A library in Arduino (or any programming) is basically code that someone else already wrote, and they kindly put it in a box for everyone to use. Instead of you having to figure out all the hard, boring, or complicated stuff yourself (like exactly how to send those perfect tiny pulses to make a servo
move to 90°), you just borrow their ready-made tools (which are called functions, like attach & write) and use them in your own code.
Without the library, you’d have to write a bunch of tricky code yourself to create those exact short bursts of electricity (called pulses) on the signal wire. But the library hides all that complicated pulse-sending stuff. You just tell it the angle you want (like 0, 90, or 180 degrees) with a super easy command.
➕➕
filename.cpp
#include <Servo.h>     // "Hey Arduino, give me the Servo toolkit!"

Servo myServo;

void setup() {
  myServo.attach(3);
  delay(20);
  myServo.write(90);  
}

void loop() {
  
}

Look at the next line:  Servo myServo;

 
This is like creating your own personal pet and giving it a name.
Servo is the type (or breed) of pet — it means ‘this is going to be a servo motor pet, the kind that can point exactly where I tell it.
myServo is the name you give your pet so you can talk to it later.
It’s exactly like when you get a new puppy or a virtual pet in a game: First you decide what kind it is: ‘I want a dog’ (that’s the Servo part). Then you name it: ‘His name is Spot'(that’s the myServo part).
Now that you’ve created and named your pet servo, you can give it commands later. Without naming it first with Servo myServo;, the Arduino wouldn’t know which servo you’re talking to.
➕➕
filename.cpp
#include <Servo.h>    

Servo myServo;          // "I'm naming my pet servo"

void setup() {
  myServo.attach(3);
  delay(20);
  myServo.write(90);  
}

void loop() {
  
}

This line  myServo.attach(3);

 
You are saying “Plug myServo into pin 3 so he can hear my commands and start obeying!” 
We connected TURL-X’s servo signal wire to pin 3. 
We put this in setup() (the morning routine) because you only need to ‘attach’ him once when the Arduino wakes up — after that he stays connected forever (until you turn power off).
➕➕
filename.cpp
#include <Servo.h>    

Servo myServo;          

void setup() {
  myServo.attach(3);      // listen for commands on pin 3
  delay(20);
  myServo.write(90);  
}

void loop() {
  
}

The delay(20);

Waiting here is not really necessary but good practice when using cheap servos that may twitch when first attached.
 
➕➕
filename.cpp
#include <Servo.h>    

Servo myServo;          

void setup() {
  myServo.attach(3);      
  delay(20);                //settle a bit before moving on
  myServo.write(90);  
}

void loop() {
  
}

myServo.write(90);

is you actually giving the command:
‘Hey myServo — turn your head exactly to the 90-degree position right now!’
The number inside the brackets (like 90) is the angle you want him to point to.
0 = all the way to one side (like looking left).
90 = straight ahead / middle / neutral (the ‘rest’ or ‘center’ position most servos snap to when they first wake up).
180 = all the way to the other side (looking right).
➕➕
filename.cpp
#include <Servo.h>    

Servo myServo;          

void setup() {
  myServo.attach(3);      
  delay(20);                
  myServo.write(90);       // "myServo, go to and sit at 90 degrees!"

void loop() {
  
}
And that is all we need to move our servo. Take note that we put our code in the void setup(function) and left the void loop() function empty. We did this because we want it to move only once, if we put it in the loop section it would keep doing it over and over.
Now copy the code and past it into your Arduino IDE. Run it (by clicking on the upload button as you did in the LED example). It should not move the head, if the head moves remove the head and position it so it is looking straight ahead, the servo should be at 90 degrees. 
Play around with different angles. Change the 90 to 20 and get it to look left. 

After you upload the code, switch TURL-X’s power on (battery).

The servo draws power from the main power source. 

➕➕
filename.cpp
#include <Servo.h>    

Servo myServo;          

void setup() {
  myServo.attach(3);      
  delay(20);                
  myServo.write(90);       // "myServo, go to and sit at 90 degrees!"

void loop() {
  
}

To copy the code click the copy icon in the top right corner.

If you a feeling bold try uploading the code below. We will study it closely when we use servos in more complex projects in the future. In the mean time just watch it go and see what you recognise in the code?

A brief breakdown: 

  • Arduino starts → servo wakes up on pin 3
  • Servo slowly turns from 20° to 120° (takes about 5 seconds)
  • Servo slowly turns back from 120° to 20° (another ~5 seconds)
  • Repeats forever — nice smooth back-and-forth motion
➕➕
filename.cpp
#include <Servo.h>          // We need this helper library to easily control the servo

Servo myServo;              // Create a servo object (like giving your servo a name)

int servoPin = 3;           // The servo signal wire is connected to pin 3

void setup() {
  myServo.attach(servoPin); // Tell the servo: "Listen for commands on pin 3"
}

void loop() {
  // Move slowly from 20° to 120°
  for (int angle = 20; angle <= 120; angle = angle + 1) {
    myServo.write(angle);   // Move servo to this angle
    delay(50);              // Wait 50 milliseconds → controls the speed
  }
  
  // Move slowly back from 120° to 20°
  for (int angle = 120; angle >= 20; angle = angle - 1) {
    myServo.write(angle);
    delay(50);
  }
}

To copy the code click the copy icon in the top right corner.