Linear Actuator Position Control Using Arduino

Good morning
I have arduino, H bridge and Linear Actuator with feedback potentiometer
I want to make actuator move from position for example 18 mm to position 2 25 mm
What is the code for this control ?

int RPWM = 10;   
int LPWM = 11;
int sensorPin = A0;

int sensorVal;
int Speed=255;
float strokeLength = 6.0;                           //customize to your specific stroke length
float extensionLength;

int maxAnalogReading;
int minAnalogReading;

void setup() {
  pinMode(RPWM, OUTPUT);
  pinMode(LPWM, OUTPUT);
  pinMode(sensorPin, INPUT);
  maxAnalogReading = moveToLimit(1);
  minAnalogReading = moveToLimit(-1);

void loop(){
  sensorVal = analogRead(sensorPin);
  while(sensorVal < maxAnalogReading){
    driveActuator(1, Speed);
  driveActuator(0, Speed);
  sensorVal = analogRead(sensorPin);
  while(sensorVal > minAnalogReading){
    driveActuator(-1, Speed);
  driveActuator(0, Speed);

int moveToLimit(int Direction){
  int prevReading=0;
  int currReading=0;
    prevReading = currReading;
    driveActuator(Direction, Speed);
    timeElapsed = 0;
    while(timeElapsed < 200){ delay(1);}           //keep moving until analog reading remains the same for 200ms
    currReading = analogRead(sensorPin);
  }while(prevReading != currReading);
  return currReading;

float mapfloat(float x, float inputMin, float inputMax, float outputMin, float outputMax){
 return (x-inputMin)*(outputMax - outputMin)/(inputMax - inputMin)+outputMin;

void displayOutput(){
  sensorVal = analogRead(sensorPin);
    extensionLength = mapfloat(sensorVal, float(minAnalogReading), float(maxAnalogReading), 0.0, strokeLength);
    Serial.print("Analog Reading: ");
    Serial.print("\tActuator extension length: ");
    Serial.println(" inches");  

void driveActuator(int Direction, int Speed){
    case 1:       //extension
      analogWrite(RPWM, Speed);
      analogWrite(LPWM, 0);
    case 0:       //stopping
      analogWrite(RPWM, 0);
      analogWrite(LPWM, 0);

    case -1:      //retraction
      analogWrite(RPWM, 0);
      analogWrite(LPWM, Speed);
I found this code on internet but this one is for maximum and minimum values and as i mentioned in the question my actuator starts from 18 mm and i want to move it to 25 mm

What is your experience with the Arduino and what have you tried so far ?
What sort of motor is in the actuator ? A link to it would help

i have made an update on my question using an example on internet but in the example had intended to move it from minimum to maximum

Hi, @mozeid10
Welcome to the forum.

Have you written this code in stages?
First write some code to move the actuators.

Can you please post your circuit diagram?
What model Arduino are you using?
What H-Bridge are you using? Can you post links to data/specs?
What actuator, link to specs/data?

What are you powering your controller and actuator with?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:


I use arduino uno ,L298n motor driver and 121F02-11302428 ACTUATOR LA12 LINAK

Thanks for the information.
English version;
linear-actuator-la12-data-sheet-eng.pdf (3.7 MB)
Which model do you have?

Can you please post a hand drawn circuit diagram with component labels and pin names?
The Fritzy you have posted does not show anything very clearly.

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

Things you might need to determine by experiment:

What is the analog reading at zero extension?
What is the analog reading at full extension?
What is the length difference between zero extension and full extension?

With those three numbers, you can determine the analog reading for any point in the range of extension. To get to a specified position you need to turn on the motor until you reach the desired analog input.

For more accuracy you can use a PID control library to slow down as you approach the desired position.

unfortunately I did not build up the circuit and model because we are waiting to receive the actuator because we have ordered it last week but i have posted it in order to know what is the code when we start impentation

Have you got a part number for the actuator that has been ordered?

Have you got a UNO and a potentiometer?
Have you got the motor controller?

You are better off developing your own code, rather than trying to adapt someone elses.

Have you written any Arduino code before?

Thanks.. Tom.... :grinning: :+1: :coffee: :australia:

When you use the line:
int position = analogRead(A0);
Your variable named 'position' will have a value from 0 to 1023. Each value represents a position of your linear actuator. You can convert those numbers to mm with a little math. Then, knowing the current 'position' and the desired position you can calculate which direction to move the actuator.