DC Motor to Servo Conversion Beta Code

By Eduardo Contreras – Computer, Power, and Powertrain

The following code is a beta version of converting a dc motor into a servo:

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

Adafruit_DCMotor *myMotor = AFMS.getMotor(1);

const int deg_error = 15;

int cur_pot = 0;
int new_pot = 160;

int inputPin = A0;

void setup()
{
  // initialize serial communication with computer:
  Serial.begin(9600);       

  AFMS.begin();
  myMotor->setSpeed(50);         

    cur_pot = analogRead(inputPin);

    while(cur_pot != 160) {
     if(cur_pot > 160) {
    myMotor->run(BACKWARD);
     }
     else if(cur_pot < 160) {
       myMotor->run(FORWARD);
     }
   cur_pot = analogRead(inputPin);  
    }
    myMotor->run(RELEASE);
    Serial.println("Calibration Done!");
    delay(1000);
}

void loop() {
  check_input();

  Serial.println(cur_pot);   

  while(cur_pot >= new_pot + deg_error || cur_pot <= new_pot - deg_error) {
    if( cur_pot > new_pot) {
      myMotor->run(BACKWARD);
    }
    else if( cur_pot < new_pot) {
      myMotor->run(FORWARD);
    }
    cur_pot = analogRead(inputPin);
  }
  myMotor->run(RELEASE);
  cur_pot = analogRead(inputPin) 
}

void check_input() {
  if ( Serial.available()) {
    char ch = Serial.read();

    switch(ch) {       
      case 'r':
        Serial.println("Move Right");
        new_pot = cur_pot + 115;
        break;
      case 'l':
        Serial.println("Move Left");
        new_pot = cur_pot - 115;
        break;
    }
  }
}

For connections, see Figure 1 below:

Figure 1: Connections for the Arduino Motor Shield, with the motor and analog potentiometer connected. The potentiometer will be used to detect the position of the motor.

Figure 1: Connections for the Arduino Motor Shield, with the motor and analog potentiometer connected. The potentiometer will be used to detect the position of the motor.

Note: The idea for this is for both shafts from the motor and potentiometer to be connected together so that the potentiometer will turn whenever the motor moves in any direction and will keep that position saved in a variable, here called cur_pot.

Before letting the user command the motor to move, it will need to reset its position to a predetermined position. In this case, it will move until the Arduino reads the potentiometer at a certain value, here being 160. After it is in position, the user will be able to tell the Arduino to move the motor.

When the user wants to turn the motor, the user will either type ‘r’, which will turn right, or ‘l’, which will turn left. This is done by incrementing, to turn right, or decrementing, to turn left, a second variable, new_pot, and have the Arduino compare this value to the current reading from the potentiometer. Depending on the value, the motor will either run forward or backwards, trying to reach the second variable number. After it reaches the value, the motor will stop. If the position changes from an outside force, the code will try to go back to the previous value. Afterwards, the user can type another instruction to turn either left or right.

The following video demonstrates how the code works: