Introduction: Control a Large Linear Actuator With Arduino

This Arduino Linear Actuator Tutorial shows how to do basic manual control of a large linear actuator using an Arduino and two buttons. In the first set of code, the first button extends the actuator and the second button retracts the actuator. In the second set of code, the two buttons move the linear actuator to preset positions.

Large linear actuators traditionally have five wires. Two wires are for powering the motor and three wires go the internal potentiometer for reading position. The two relays are used to toggle the positive and negative power to the motor in order to determine the direction of piston travel. The first bit of code doesn't use this, and the second uses this to reach a goal position. Let's get started.

Step 1: Project Parts List

It is suggested that you use the parts listed here to follow along in this tutorial. If you already have a large linear actuator, you can likely follow this tutorial to make it work, but you may have difficulty wiring for the potentiometer.

Step 2: Wiring

Device Sensor Shield Port
RobotGeek Pushbutton 1 Digital Pin 2
RobotGeek Pushbutton 2 Digital Pin 4
RobotGeek Relay 1 Digital Pin 7
RobotGeek Relay 2 Digital Pin 8
Linear Actuator Potentiometer Analog 0

Step 3: Code 1 - Manual Control

This code shows how to do basic manual control of a large linear actuator using an Arduino and two buttons. The first button extends the actuator and the second retracts the actuator.

You can download the code sketch here: https://github.com/robotgeek/largeLinearManualButton/archive/master.zip

/*

Manual Linear Actuator Control using an Arduino and two pushbuttons
 
This demo shows how to do basic manual control of a large linear
actuator using an Arduino and two buttons. The first button extends
the actuator and the second retracts the actuator.
 
 
 The circuit:
 * RobotGeek Pushbutton - Digital Pin 2
 * RobotGeek Pushbutton - Digital Pin 4
 * RobotGeek Relay - Digital Pin 7 
 * RobotGeek Relay - Digital Pin 8 
 
Products Used in this demo:
 - http://www.robotgeek.com/linear-actuators
 - http://www.robotgeek.com/robotgeek-geekduino-sensor-kit
 - http://www.robotgeek.com/robotGeek-pushbutton
 - http://www.robotgeek.com/robotgeek-relay


 */

// constants won't change. They're used here to set pin numbers:
const int button1Pin = 2;     // the number of the pushbutton1 pin
const int button2Pin = 4;     // the number of the pushbutton2 pin
const int relay1Pin =  7;      // the number of the Realy1 pin
const int relay2Pin =  8;      // the number of the Relay2 pin

// variables will change:
int button1State = 0;         // variable for reading the pushbutton status
int button2State = 0;         // variable for reading the pushbutton status

const int sensorPin = 0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor


void setup() { 
  
  
  //start serial connection
  Serial.begin(9600);  
  
  // initialize the pushbutton pin as an input:
  pinMode(button1Pin, INPUT);     
  pinMode(button2Pin, INPUT);    
  // initialize the relay pin as an output:
  pinMode(relay1Pin, OUTPUT);    
  pinMode(relay2Pin, OUTPUT);    
}

void loop(){
  
  
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin); 
  //print out the value of the pushbutton
  Serial.println(sensorValue);    
  
  // read the state of the pushbutton values:
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);

  // check if the pushbutton1 is pressed.
  // if it is, the buttonState is HIGH:
  // we also ensure tha the other button is not pushed to avoid conflict
  if (button1State == HIGH && button2State == LOW) {     
    // turn relay1 on:    
    digitalWrite(relay1Pin, HIGH);  
  } 
  // When we let go of the button, turn off the relay
  else if (digitalRead(relay1Pin) == HIGH) {
    // turn relay1 off:
    digitalWrite(relay1Pin, LOW); 
  }
  
  // repeat the same procedure for the second pushbutton
  if (button1State == LOW && button2State == HIGH) {     
    // turn relay2 on:    
    digitalWrite(relay2Pin, HIGH);  
  } 
  // When we let go of the button, turn off the relay
  else if (digitalRead(relay2Pin) == HIGH) {
    // turn relay2 off:
    digitalWrite(relay2Pin, LOW); 
  }  
}

Step 4: Code 2 - Preset Position Using Positional Feedback

This code shows how to do basic control of a large linear actuator using an Arduino and two buttons with each button preset to a position.

You can download the code sketch here: https://github.com/robotgeek/largeLinearPresetPositions/archive/master.zip

/*

Linear Actuator Control using preset position
 
This demo shows how to do basic control of a large linear
actuator using an Arduino and two buttons. Each button is hard
coded with a preset position. Pressing a button will move
the actuator to that position.
 
 The circuit:
 * RobotGeek Pushbutton - Digital Pin 1
 * RobotGeek Pushbutton - Digital Pin 2
 * RobotGeek Relay - Digital Pin 4 
 * RobotGeek Relay - Digital Pin 7 
 
Products Used in this demo:
 - http://www.robotgeek.com/linear-actuators
 - http://www.robotgeek.com/robotgeek-geekduino-sensor-kit
 - http://www.robotgeek.com/robotGeek-pushbutton
 - http://www.robotgeek.com/robotgeek-relay
 
 */

// constants won't change. They're used here to set pin numbers:
const int button1Pin = 2;     // the number of the pushbutton1 pin
const int button2Pin = 4;     // the number of the pushbutton2 pin
const int relay1Pin =  7;      // the number of the Realy1 pin
const int relay2Pin =  8;      // the number of the Relay2 pin
const int sensorPin = 0;    // select the input pin for the potentiometer

// variables will change:
int button1State = 0;         // variable for reading the pushbutton status
int button2State = 0;         // variable for reading the pushbutton status
int sensorValue = 0;  // variable to store the value coming from the sensor

int goalPosition = 350; 
int CurrentPosition = 0; 
boolean Extending = false;
boolean Retracting = false;

void setup() { 


  //start serial connection
  Serial.begin(9600);

  // initialize the pushbutton pin as an input:
  pinMode(button1Pin, INPUT);     
  pinMode(button2Pin, INPUT);    
  // initialize the relay pin as an output:
  pinMode(relay1Pin, OUTPUT);    
  pinMode(relay2Pin, OUTPUT);    
  
  //preset the relays to LOW
  digitalWrite(relay1Pin, LOW); 
  digitalWrite(relay2Pin, LOW); 

  
}

void loop(){
  
  // read the value from the sensor:
  CurrentPosition = analogRead(sensorPin); 

  
  // print the results to the serial monitor:
  Serial.print("Current = " );                       
  Serial.print(CurrentPosition);      
  Serial.print("\t Goal = ");      
  Serial.println(goalPosition);  
  
  // read the state of the pushbutton values:
  button1State = digitalRead(button1Pin);
  button2State = digitalRead(button2Pin);

  if (button1State == HIGH) {     
    // set new goal position
    goalPosition = 300; 
    
    if (goalPosition > CurrentPosition) {
        Retracting = false;
        Extending = true;
        digitalWrite(relay1Pin, HIGH);  
        digitalWrite(relay2Pin, LOW);  
        Serial.println("Extending");     
    }      
    else if (goalPosition < CurrentPosition) {
        Retracting = true;
        Extending = false;
        digitalWrite(relay1Pin, LOW);  
        digitalWrite(relay2Pin, HIGH); 
        Serial.println("Retracting");         
    }  
  }

  if (button2State == HIGH) {     
    // set new goal position
    goalPosition = 500; 
    
    if (goalPosition > CurrentPosition) {
        Retracting = false;
        Extending = true;
        digitalWrite(relay1Pin, HIGH);  
        digitalWrite(relay2Pin, LOW);  
        Serial.println("Extending");   
    }        
    else if (goalPosition < CurrentPosition) {
        Retracting = true;
        Extending = false;
        digitalWrite(relay1Pin, LOW);  
        digitalWrite(relay2Pin, HIGH); 
        Serial.println("Retracting");         
    }  
  }



  if (Extending = true && CurrentPosition > goalPosition) {
    //we have reached our goal, shut the relay off
    digitalWrite(relay1Pin, LOW); 
    boolean Extending = false; 
    Serial.println("IDLE");  
  }
  
  if (Retracting = true && CurrentPosition < goalPosition){
    //we have reached our goal, shut the relay off
    digitalWrite(relay2Pin, LOW); 
    boolean Retracting = false; 
    Serial.println("IDLE");  
  }


  
  
}

Step 5: You're Done!

Now that you can control a large linear actuator, what are you going to do with it? You could make a desk that transforms to allow a sitting or standing position. With a few light sensors, you could make a solar panel that tracks the sun. Dream up some ideas and let us know, we'd love to hear about it!

Comments

author
Shaes123 made it!(author)2017-02-14

My actuator never actual reaches the goal it stutters at 1 above or 1 lower then bounces

As an example if goal is 350 it gets to 351 or 349 and just clucks

Any ideas

author
Winterworks made it!(author)2017-01-17

Hi,

would it be possible to code in a way that you only have one button. Pushing once, actuator extends, pushing the again the actuator goes down?

Best

author
TheThinker made it!(author)2016-04-12

Nice ible!

author
robotgeek_official made it!(author)2016-04-12

Thanks! Hope it was helpful!

About This Instructable

5,170views

40favorites

License:

Bio: The RobotGeek team is a 6-man operation that wants to make it even easier to use Arduino to make electronics and robots. Check out our ... More »
More by robotgeek_official:Voice Control Chip-E With Google HomeRock'em Sock'em Literal Robots3D Printed Automatic Small Fish Feeder (Single Servo and Arduino)
Add instructable to: