Monitoring the current of a linear actuator

This example code uses a MegaMoto Plus and an Arduino Uno to monitor the current of a linear actuator, however, similar products can be used as substitutions.

/*  Code to monitor the current amp draw of the actuator, and to cut power if it
  rises above a certain amount.

  Written by Progressive Automations
  August 19th, 2015

  Hardware:
  - RobotPower MegaMoto control boards
  - Arduino Uno
  - 2 pushbuttons
 */

const int EnablePin = 8;
const int PWMPinA = 11;
const int PWMPinB = 3; // pins for Megamoto

const int buttonLeft = 4;
const int buttonRight = 5;//buttons to move the motor

const int CPin1 = A5;  // motor feedback

int leftlatch = LOW;
int rightlatch = LOW;//motor latches (used for code logic)

int hitLimits = 0;//start at 0
int hitLimitsmax = 10;//values to know if travel limits were reached

long lastfeedbacktime = 0; // must be long, else it overflows
int firstfeedbacktimedelay = 750; //first delay to ignore current spike
int feedbacktimedelay = 50; //delay between feedback cycles, how often you want the motor to be checked
long currentTimefeedback = 0; // must be long, else it overflows

int debounceTime = 300; //amount to debounce buttons, lower values makes the buttons more sensitive
long lastButtonpress = 0; // timer for debouncing
long currentTimedebounce = 0;

int CRaw = 0;      // input value for current readings
int maxAmps = 0; // trip limit 

bool dontExtend = false;
bool firstRun = true;
bool fullyRetracted = false;//program logic

void setup()
{
  Serial.begin(9600);
  pinMode(EnablePin, OUTPUT);
  pinMode(PWMPinA, OUTPUT);
  pinMode(PWMPinB, OUTPUT);//Set motor outputs
  pinMode(buttonLeft, INPUT);
  pinMode(buttonRight, INPUT);//buttons
  
  digitalWrite(buttonLeft, HIGH);
  digitalWrite(buttonRight, HIGH);//enable internal pullups
  pinMode(CPin1, INPUT);//set feedback input
  
  currentTimedebounce = millis();
  currentTimefeedback = 0;//Set initial times

  maxAmps = 15;// SET MAX CURRENT HERE

}//end setup

void loop()
{
  latchButtons();//check buttons, see if we need to move

  moveMotor();//check latches, move motor in or out

}//end main loop

void latchButtons()
{
  if (digitalRead(buttonLeft)==LOW)//left is forwards
  {
    currentTimedebounce = millis() - lastButtonpress;// check time since last press
    if (currentTimedebounce > debounceTime && dontExtend == false)//once you've tripped dontExtend, ignore all forwards presses
    {
      leftlatch = !leftlatch;// if motor is moving, stop, if stopped, start moving
      firstRun = true;// set firstRun flag to ignore current spike
      fullyRetracted = false; // once you move forwards, you are not fully retracted
      lastButtonpress = millis();//store time of last button press
      return;
    }//end if
  }//end btnLEFT

  if (digitalRead(buttonRight)==LOW)//right is backwards
  {
    currentTimedebounce = millis() - lastButtonpress;// check time since last press

    if (currentTimedebounce > debounceTime)
    {
      rightlatch = !rightlatch;// if motor is moving, stop, if stopped, start moving
      firstRun = true;// set firstRun flag to ignore current spike
      lastButtonpress = millis();//store time of last button press
      return;    }//end if
  }//end btnRIGHT
}//end latchButtons

void moveMotor()
{
  if (leftlatch == HIGH) motorForward(255); //speed = 0-255
  if (leftlatch == LOW) motorStop();
  if (rightlatch == HIGH) motorBack(255); //speed = 0-255
  if (rightlatch == LOW) motorStop();

}//end moveMotor

void motorForward(int speeed)
{
  while (dontExtend == false && leftlatch == HIGH)
  {
    digitalWrite(EnablePin, HIGH);
    analogWrite(PWMPinA, speeed);
    analogWrite(PWMPinB, 0);//move motor
    if (firstRun == true) delay(firstfeedbacktimedelay); // bigger delay to ignore current spike
    else delay(feedbacktimedelay); //small delay to get to speed

    getFeedback();
    firstRun = false;
    
    latchButtons();//check buttons again
  }//end while

}//end motorForward

void motorBack (int speeed)
{
  while (rightlatch == HIGH)
  {
    digitalWrite(EnablePin, HIGH);
    analogWrite(PWMPinA, 0);
    analogWrite(PWMPinB, speeed);//move motor
    if (firstRun == true) delay(firstfeedbacktimedelay);// bigger delay to ignore current spike
    else delay(feedbacktimedelay); //small delay to get to speed
    getFeedback();

    firstRun = false;
    
    latchButtons();//check buttons again

  }//end while

  dontExtend = false;//allow motor to extend again, after it has been retracted

}//end motorBack

void motorStop()
{
  analogWrite(PWMPinA, 0);
  analogWrite(PWMPinB, 0);

  digitalWrite(EnablePin, LOW);
  firstRun = true;//once the motor has stopped, reenable firstRun to account for startup current spikes

}//end stopMotor

void getFeedback()
{
  CRaw = analogRead(CPin1); // Read current

  if (CRaw == 0 && hitLimits < hitLimitsmax) hitLimits = hitLimits + 1;
  else hitLimits = 0; // check to see if the motor is at the limits and the current has stopped 

  if (hitLimits == hitLimitsmax && rightlatch == HIGH)
  {
    rightlatch = LOW; // stop motor
    fullyRetracted = true;
  }//end if

  else if (hitLimits == hitLimitsmax && leftlatch == HIGH)
  {
    leftlatch = LOW;//stop motor
    hitLimits = 0;
  }//end if

  if (CRaw > maxAmps)
  {
    dontExtend = true;
    leftlatch = LOW; //stop if feedback is over maximum
  }//end if

  lastfeedbacktime = millis();//store previous time for receiving feedback
}//end getFeedback
Controlling multiple actuators with the multimoto Arduino shield
Wasp motor controller for linear actuator control
Using relays to control linear actuators
Controlling the timing of a single linear actuators motion
Continuously extend & retract a linear actuator with brushless DC motor
PA-12 micro servo actuator sample codes
Where can I find additional information on your products?

We have data sheets, user manuals, 3D models, wiring diagrams and more in our Resources section.

How can I determine which progressive automations linear actuator is best suited for my application?
What is duty cycle and how is it calculated?
Can I use your actuators to replace the one that I already have?
What does stroke mean? How am I supposed to know which size to choose?
How do I know which force rating is right for my application?
Can I use my own power supply as a source for my actuators?
How can I control actuators to travel at the same time?
Why is my linear actuator making so much noise?
Can I customize a linear actuator to my specifications?
Can I synchronize my linear actuators?
Are linear actuator kits available?
Will temperature affect my linear actuator?
Can I implement one of your actuators into a third-party mechanism?
What is the pin out for my linear actuator?
Can I get 3D CAD models for my linear actuator?
What are the control box options for my actuator?
Can I use your control boxes with a third-party product?
Can I use my own control box?
Can I use my own power supply?
Do you have 220 VAC power supplies available?
Does my linear actuator come with mounting brackets?
Where can I find a step-by-step guide for my product?
Can the FLTCON control boxes work with my actuators?
What is backdriving? -- What does dynamic and static load ratings mean? -- What is lateral loading?
How can I place an order?

Orders can be placed by one of the following ways:

Online: Use our online order process with options to pay by Credit Card or PayPal.

Phone: (03) 8797 3901

Email: [email protected]