Week 9 – Lab Report

Program a servo using an Arduino Microcontroller and the knob servo code  from the Arduino website


Assemble a servo environment using an Arduino Microcontroller, a servo, control it using a potentiometer, and open source code available through the Arduino website.


Arduino microcontroller UNO

Hitec HS-422 Servo

5 volt Potentiometer

Ethernet Cable

Example code “knob” from Arduino.cc


// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

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

void loop()
val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 1023, 0, 179, 0);     // scale it to use it with the servo (value between 0 and 180)
myservo.write(val);                  // sets the servo position according to the scaled value
delay(1);                           // waits for the servo to get there



1.- Connect cables as per diagram, the yellow cable from servo to pin 9, the black to ground, and the red to power.

2.- Connect the potentiometer to analog pin 0, black to ground, and red can share the power pin.

3.- Open the Arduino environment, go to examples, scroll to servo tab, and choose the knob example.

4.-Go to tools scroll to board and choose the Duemilanove board in this case, again go to tools, scrool to serial port, and choose the serial port.

5.- Now  you’re ready to test your hardware, start by uploading your code into the Arduino, and verify it, by clicking the check button, if that’s ok, click the upload button right of the verify. If that goes well the servo should start spinning.


The servo now start spinning to one side and the to the other, as you turn the knob on the potentiometer. The code has a val=  which sets the degrees of travel of the servo. You can tamper with the delay increasing or decreasing its response time, that didn’t speed the movement of the servo gear. You can also exchange the values for the serial read and change the direction sinc of the knob and the servo. It worked!!!




This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply