How can I control position of a linear actuator?

Hello all,

I am very new to Arduino and would appreciate some help getting started with a project. I want to be able to move the actuator to different stroke lengths. I have a PQ 12 P linear actuator that has a potentiometer to provide position feedback and a TB6612FNG motor driver.

I want to be able to give a bunch of positions and hav the actuator move to it. could this be done using MATLAB?

How should I get started with the programming for it?

Thank you!

Did you already download the Arduino libraries for your components?

I have installed the MATLAB Support Package for Arduino. I am not sure which libraries I would need specifically. I have been reading a few threads over here and it seems like I would need to read the analog input from the internal potentiometer and then based on this value have the actuator move to the target position, using it in a while loop. Does that sound right?

You're on the right way. But I don't understand the use of MATLAB with this project.

I want to be able to give a bunch of positions for the stroke length (have a for loop running over the different values). I guess it can be done with the Arduino IDE as well but I'm more familiar with MATLAB, that's why I would rather work with it.


So I was able to code it up in MATLAB such that the actuator moves to the desired position and returns to the start position (this is what I want). However, I think there is a little bit of overshoot and I wanted to make sure that I am programming it correctly. I am fairly new to this, so please let me know if there is a better way to do this. This is the code that I am using.

clear; clc;
targetpos = [1, 3, 5]; % in mm
for i=1:length(targetpos)

function execute(targetpos)
    a = arduino('COM10', 'Uno');
    Ain1 = 'D9';
    Ain2 = 'D8';
    stby = 'D10';
    pwma = 'D3';
    spd = 5; % in volts

    configurePin(a, Ain1,'DigitalOutput');
    configurePin(a, Ain2,'DigitalOutput');
    configurePin(a, stby,'DigitalOutput');
    configurePin(a, pwma,'DigitalOutput');
    configurePin(a, Bin1,'DigitalOutput');
    configurePin(a, Bin2,'DigitalOutput');
    configurePin(a, pwmb,'DigitalOutput');

    % Function to power on actuator
    start(a, stby)
    % Function to move actuator to target position and move back
    squeeze(a, targetpos, Ain1, Ain2, pwma, spd, Bin1, Bin2, pwmb)
    % Function to power off actuator
    stop(a, stby)

function squeeze(a, targetpos, Ain1, Ain2, pwma, spd)
    sl = 10; % total stroke length in mm
    max_vol = 4.6; %voltage at max stroke length
    min_vol = 0.4; %voltage at min stroke length
    vd = max_vol-min_vol; % voltage difference between max and min stroke
    targetvol = min_vol+(vd/sl)*targetpos; % voltage at target position
    pres_vol1 = min_vol; % intial voltage
    % Move forward
    while pres_vol1 <= targetvol 
        writeDigitalPin(a, Ain1, 1);
        writeDigitalPin(a, Ain2, 0);
        writePWMVoltage(a, pwma, spd);
        pres_vol1 = readVoltage(a, 'A0'); % update voltage values
    time = toc;
    % Move backward
    writeDigitalPin(a, Ain1, 0);
    writeDigitalPin(a, Ain2, 1);
    writePWMVoltage(a, pwma, spd);
function stop(a, stby)
    writeDigitalPin(a, stby, 0);

function start(a, stby)
    writeDigitalPin(a, stby, 1);

Since you have a method of measuring the distance traveled, most people would use a PID loop to control the travel.

There are three PID constants to be estimated, in order to reach the target (setpoint) without overshoot. Various methods to choose these are given in on-line tutorials. Search for "PID tuning tutorial".

I was wondering what are the Ain1, Ain2, and stby pins in your code

Thanks :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.