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 ‘ON‘ pulse 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.
Let’s go through the code line by line.
The first line: #include <servo.h>
#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.
#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);
#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.
#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).
#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.
#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
#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);
}
}