# Servo

The servo motor has three wires: power, ground, and signal. The power cord is usually red and should be connected to the 5V pin on the Arduino board. The ground wire is usually black or brown and should be connected to the ground pin on the Arduino board. The signal pins are usually yellow, orange or white and should be connected to the digital pins on the Arduino board. Please note that the servo system consumes considerable power, so if you need to drive more than one or two, you may need to use a separate power supply (ie the +5V pin on the Arduino) to power them. Be sure to connect the Arduino to the ground of the external power supply.

![](https://451694785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MPda5vaSVRKTQ6AoAmb%2Fuploads%2FVWV3AFsErW9kHLpUQdml%2F7.jpg?alt=media\&token=8e1c148b-e8fe-4b28-81cc-eea0c7298a8e)

### **Component Required:**

1 x Uno R3

1 x Servo (SG90)

3 x M-M wires (Male to Male jumper wires)

### **Connection Diagram:**

![](https://451694785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MPda5vaSVRKTQ6AoAmb%2Fuploads%2FNM4E26F67Ia2us43Bdib%2Fservo.png?alt=media\&token=85a11b85-0b58-414b-ac38-3fad5de46788)

### **Wiring schematic:**

![](https://451694785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MPda5vaSVRKTQ6AoAmb%2Fuploads%2F34myPCQ7HgOvgGtL2B1q%2Fservo.png?alt=media\&token=efbb68bc-a55e-434b-953c-a632defc81e6)

### **Physical wiring diagram:**

![](https://451694785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MPda5vaSVRKTQ6AoAmb%2Fuploads%2FQ9ta0Nr0Tv8oKnwvBlnV%2Fservo-wired.png?alt=media\&token=c51fd66a-5a09-44b0-9745-360d5e4d5611)

### **Code**

{% hint style="warning" %}
After wiring add the **Servo.zip** library. Download it below
{% endhint %}

{% file src="<https://451694785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MPda5vaSVRKTQ6AoAmb%2Fuploads%2Fgit-blob-484e364a2e9f7b0d064ecfd3b8add3dce7001cdd%2FServo.zip?alt=media>" %}

```
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
//  a maximum of eight servo objects can be created

int pos = 0;// variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop(){

  for (pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
  { // in steps of 1 degree
    myservo.write(pos); // tell servo to go to position in variable 'pos'
    delay(15);  // waits 15ms for the servo to reach the position
  }

  for (pos = 180; pos >= 1; pos -= 1) // goes from 180 degrees to 0 degrees
  {
    myservo.write(pos); // tell servo to go to position in variable 'pos'
    delay(15);  // waits 15ms for the servo to reach the position
  }
}
```

{% file src="<https://451694785-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MPda5vaSVRKTQ6AoAmb%2Fuploads%2Fgit-blob-e051f6b5523d94b718830acc9dfeaed53b7df656%2Fservo.ino?alt=media>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.arduinosa.co.za/arduino-kits/basic-starter-kit/servo.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
