Introduction: Controlling the Position of an Actuator With an Analog Sensor

About: Progressive Automations is your primary source for electric linear actuators, motion control systems and automation accessories. For over a decade, we have supplied various industries with top quality automati…

In this Instructable, we will learn how to use an analog sensor to control the position of an actuator. In this example, we'll be using a potentiometer, but you can use any analog sensor. You could use a light sensor to move an actuator based on the position of the sun.

We'll be using the following parts:

- 1x Linear Actuator with Pot Feedback

- 1x Arduino Uno

- 1x MegaMoto Motor Controller

- 1x Breadboard

- 1x Potentiometer (10k is suggested, or you can use a different analog sensor)

First we'll go over the wiring, then see the code it takes to make this happen, and finally offer some project ideas.

Lets get started!

Step 1: Wiring

For wiring, we need to attach the shield to the Arduino, then do some simple wiring connections to get the actuator and potentiometer connected.

1) Connect the MegaMoto Shield to the Arduino. See here for more detailed instructions on a MegaMoto

- Set the Enable jumper to D8

- Set PWM A to D11

- Set PWM B to D3

- Ensure that Sensor is NOT on A0 or A1. We are not using it for this project, and A0 and A1 are in use.

2) If you are using a 12V or less power source, connect the BAT+ to Vin. If you are using greater then 12V, you need a separate power supply for the Arduino.

3) Connect the Actuator to the MOTA and MOTB terminals

***Get a small breadboard to simplify the next connections***

4) Connect the 3 other actuator wires as follows:

- Yellow to the 5V

- White to GND

- Blue to A1

5) Connect your other potentiometer as follows:

- One side to the 5V pin

- Other side to GND

- Middle (slider) pin to A0

6) Connect your power supply to the BAT+ and BAT- terminals on the MegaMoto

Now that wiring is complete, we can move to programming!

Step 2: Programming

The idea behind the program is to take a reading of the potentiometer (or other analog sensor), take a reading of the actuators position, and compare them. If they are different, extend/retract the actuator to match them. We will be using the map() function, to simplify the comparison.

The map() function takes a range of values, and converts them to a different range.

For example: Say your sensor gives an input of 0-1023. But, your actuator gives feedback from 400-600. You would use the map() function on the potentiometer feedback, to drop the range from 0-1023 to 400-600. This allows it to have an easier comparison to your sensor. The other option is to boost the range of the actuator, from 400-600 up to 0-1023. The function would look like: map(variable, 400,600,0,1023);

If you drop a signal, you lose precision. If the sensor gives values from 0-1000, and you drop it to 400-600, you are changing from a 1000 point range to a 200 point range. That will make the precision worse by a factor of 5. The sensor will have to change 5 times, from 200, 201, 202, 203, 204, 205 before it changes from 440 to 441. If you have a precise application, this may cause a problem, but for most cases, it should be OK.

Here is the code with comments explaining each part, the code is also attached:

/*  Sample code to control the position of an actuator with potentiometer feedback using a MegaMoto.

The main loop of this program checks the potentiometer, and moves the actuator accordingly.


 Written by Progressive Automations

  This example code is in the public domain.
*/

const int feedback = A0; //potentiometer from actuator
const int pot = A1; //pot from throttle

const int enable = 8;
const int PWMA = 11;
const int PWMB = 3;

int actMax = 760;
int actMin = 250;//positions of actuator

int potMin = 0;
int potMax = 1023;

int precision = 2;//how close to final value to get
int checkingInterval = 50;//how often position is checked (milliseconds)

int rawcurrentPosition = 0;
int currentPosition = 0;
int rawdestination = 0;
int destination = 0;
int difference = 0;//values for knowing location

void setup()
{
  pinMode(feedback, INPUT);//feedback from actuator
  pinMode(pot, INPUT);//feedback from potentiometer
  pinMode(enable, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(PWMB, OUTPUT);//three pins for MegaMoto
  digitalWrite(enable,HIGH);
  Serial.begin(9600);
}

void loop()
{   
    destination = getDestination(); 
    currentPosition = analogRead(feedback);//check where you are
    Serial.print("Position    ");
    Serial.println(analogRead(feedback));
    difference = destination - currentPosition;//find out how far you are from the destination
    if (currentPosition > destination) pullActuatorUntilStop(destination);// choose what action to take
    else if (currentPosition < destination) pushActuatorUntilStop(destination);
    else if (difference < precision && difference > -precision) stopActuator();
}//end void loop

int getDestination()
{
    rawdestination = analogRead(pot);//read the potentiometer to get the destination
    destination = map(rawdestination, potMin,potMax,actMin,actMax);//convert the potentiometer feedback to match the actuator
    return(destination);
}//end getDestination

void pushActuatorUntilStop(int destination)
{
  destination = getDestination();
  int temp = analogRead(feedback); 
  difference = destination - temp;//check difference to see if continue moving, or stop

  while (difference > precision || difference < -precision)
  {
    destination = getDestination();
    temp = analogRead(feedback); //continue checking difference
    difference = destination - temp;
    pushActuator();
  }//end while
  
  delay(25);
  stopActuator();
}//end pushActuatorUntilStop

void pullActuatorUntilStop(int destination)
{
  destination = getDestination();
  int temp = analogRead(feedback); //check difference to see if continue moving, or stop
  difference = destination - temp;

  while (difference > precision || difference < -precision)
  {
    destination = getDestination();
    temp = analogRead(feedback); //continue checking difference
    difference = destination - temp;
    pullActuator();
  }//end while
  
  delay(25);
  stopActuator();
}//end pullActuatorUntilStop

void stopActuator()
{
  analogWrite(PWMA,0);
  analogWrite(PWMB,0);
}//end stopActuator

void pushActuator()
{ 
  analogWrite(PWMB,255);
  analogWrite(PWMA,0);
}//end pushActuator

void pullActuator()
{
  analogWrite(PWMB,0);
  analogWrite(PWMA,255);
}//end pullActuator

Step 3: Conclusion

In this Instructable, we learned how to use the feedback from an analog sensor to control the position of an actuator. You can use the principles we learned here to add any analog sensor to your actuator project.

By reading the analog input from a sensor and adjusting the position of the actuator, you can make a multitude of projects. You could make a solar tracker, that moves to different positions based on the position of the sun, a throttle that moves based on the position of a joystick, or a force tester, that will move the actuator based on the amount of force being applied.

If you'd like to take a look at our selection of linear actuators, motions control systems and microcontrollers then please visit us at www.progressiveautomations.com for all your actuator needs! We can even build a custom actuator or control system for you based on your own custom specifications with the help of our highly trained staff of engineers. You can learn more about the custom order process right here!

Follow, Like and Subscribe!

Twitter -twitter.com/ProgAutoInc

Facebook -www.facebook.com/ProgressiveAutomations

Youtube -www.youtube.com/user/MrActuators