Servo

Overview:

In this lesson, you will learn how to control a servo motor using an Arduino.

You will add a "pot" to control the position of the servo.

Materials Needed:

  • (1) Servo Motor
  • (1) 10 kΩ variable resistor (pot)
  • (1) Half-size breadboard
  • (1) Arduino Uno R3
  • Jumper wires

Code:

#include <Servo.h> 

 

int servoPin = 9;

 

Servo servo;  

 

int angle = 0;   // servo position in degrees 

 

void setup() 

  servo.attach(servoPin); 

 

 

void loop() 

  // scan from 0 to 180 degrees

  for(angle = 0; angle < 180; angle++)  

  {                                  

    servo.write(angle);               

    delay(15);                   

  } 

  // now scan back from 180 to 0 degrees

  for(angle = 180; angle > 0; angle--)    

  {                                

    servo.write(angle);           

    delay(15);       

  } 

Layout: