Servo Control via Potentiometer (using Arduino)

This tutorial explains how to directly interface with a servo via a potentiometer, using an Arduino. It ensures that the input and output are calibrated correctly, so that the servo and potentiometer remain in sync, and allows you to view readings through a serial viewer. When writing this code, I used extracts from the calibrate example in the Arduino library, and code from the Michal Rinott tutorial on the arduino.cc site.

In case you didn’t know, a potentiometer is a variable resistor, which can be used to adjust a power output according to how far open / closed it is. With the Arduino you can read this change as an analog input, allowing for high control over inputs. I used a potentiometer from spark fun. It is actually called a Trimpot, but this is basically just a really small potentiometer. You could use a bigger one if you prefer. On the potentiometer you should have three pins, to wire it up, connect the left hand pin to the 3.3v power supply on the Arduino. Then connect the centre pin to analog pin 1 (A1) on your Arduino, and the right hand pin to the ground pin on the Arduino (place a 330 ohm resistor in-between the right hand pin and the ground pin).

photo2-1024x768.jpg

Then using this code, you should be able to read the analog output of the potentiometer in a serial viewer.

int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value
int sensor = 1;  // analog pin used to connect the potentiometer

void setup() {
      Serial.begin(9600); //start serial output
}

void loop() {
    sensorValue = analogRead(sensor);     // reads the value of the      potentiometer (value between 0 and 1023
    Serial.print(sensorValue);  //print the servo value in degrees in the serial monitor
    Serial.println();
}

Now its time to wire up the servo. This is more complicated, as it needs an external power supply to ensure you don’t burn out your Arduino. You will need a switch, a 6v power pack (a set of 4 aa batteries in a holder will do), and a smallish servo. Plug the battery back into the switch, so that you can use it to turn the battery pack on and off. Alternatively you could just unplug the battery pack each time you wan’t to turn off the Arduino. You will find 3 coloured wires coming out of the servo. Connect the red one to the power packs positive wire (probably red), and connect the black wire to the ground strip on your breadboard. To the same ground strip connect a ground wire to the Arduino, and another to the power pack. Whilst you are doing this, make sure the power pack is turned off. Finally, connect the white or yellow wire on the servo to pin 9 on the Arduino. This is the control wire, which we will use to control the rotation of the servo.

photo-1024x768.jpg

I also connected an additional momentary push button to my Arduino, which allows for the device to be re-calibrated whilst in use. You’d don’t need to do this, it is just personal preference. Connect the left hand pin to the 3.3v power supply,and the right hand pin of the button to pin 7 on the Arduino and to the Arduino’s ground pin (via a 330 ohm resistor).

photo1-1024x768.jpg

If it is wired up correctly, plug in the Arduino, turn on the power pack, and then upload the code. The servo should move in sync with the potentiometer.

To use this code, you will need to download and install the Servo library.

// Controlling a servo position using a potentiometer (variable resistor)
// Added callibration function to ensure accurate control of servo

#include <Servo.h>

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

int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value

int sensor = 1;  // analog pin used to connect the potentiometer
int button = 7;

int buttonVal = 0; // sets the value
int state = 0; //sets the state
int old_val = 0; //stores the previous value

void setup() {
    myservo.attach(9);  // attaches the servo on pin 9 to the servo object
    pinMode(button, INPUT); //sets the button as an input

    myservo.write(180);

    // calibrate during the first five seconds
   while (millis() < 5000) { //for the first five seconds
        sensorValue = analogRead(sensor);

        // record the maximum sensor value
        if (sensorValue > sensorMax) {
             sensorMax = sensorValue;
        }

        // record the minimum sensor value
        if (sensorValue < sensorMin) {
             sensorMin = sensorValue;
        }
   }

     // signal the end of the calibration period
  myservo.write(0);

  Serial.begin(9600); //start serial output
}

void loop()
{
  buttonVal = digitalRead(button);
  sensorValue = analogRead(sensor);            // reads the value of the    potentiometer (value between 0 and 1023)
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(sensorValue);                  // sets the servo position according to the scaled value
  sensorValue = constrain(sensorValue, 0, 179); //in case the sensor value is outside the range seen in callibration
  delay(15);                           // waits for the servo to get there

  Serial.print("servo = " );
  Serial.print(sensorValue);  //print the servo value in degrees in the serial  monitor
  Serial.println();

  if((buttonVal==HIGH) && (old_val==LOW)){ //checks if there was a transition between old and new val
      state = 1 - state; //sets the state
      delay(10); //sets a delay of 10
   }

   old_val = buttonVal; //stores the new value as the old value

   if (state==1) {
      calibrate();
   }

 }

void calibrate(){
  myservo.write(180);
  // calibrate during the first five seconds
  sensorValue = analogRead(sensor);
  // record the maximum sensor value
  if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
  }
  // record the minimum sensor value
  if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
  }
  // signal the end of the calibration period
  myservo.write(0);

  delay (2000);
}
 
0
Kudos
 
0
Kudos

Now read this

Arduino Bit Array

Sometimes even using a byte of memory takes up too much space, especially when working with simple true / false values of 1′s / 0′s. I have created a library which allows you to split a byte into 8 individual bits, based on the Arduino... Continue →

Subscribe to Jacob Unwin

Don’t worry; we hate spam with a passion.
You can unsubscribe with one click.

3pzjKF4ncLKz54YCiFkO