Control direction of a linear actuator

Hello, this is my second post, getting harder :slight_smile:
I have a linear actuator Actuonix L16-12V option P, which changes direction by changing the polarity

I just need to manually activate it and decide to move it forward or backward through a button switch and the H-bridge motor driver L293D of the Arduino Uno kit.

I cannot make it moving.
The actuator has 5 cables and I am just using the red and black (motor V+ and V-). Maybe this is a wrong idea.

The code comes from the Ex 10 of the official Arduino Project book, which uses the HBridge, 2 buttons, and a potentiometer to control a rotating DC motor. I "just" reduced to comments the rows of code related to the potentiometer, because I am not interested in the speed. Maybe this is wrong.

I do not know where the error could be and such any help would be appreciated!

const int Hbridge7 = 2;
const int Hbridge2 = 3;
const int HbridgeEnable=9;
const int directionButton =4;
const int onOffButton =5;
const int pot=A0;

int onOffButtonState=0;
int previousOnOffButtonState=0;
int directionButtonState=0;
int previousDirectionButtonState=0;

int motorEnabled=0;
//int motorSpeed=0;
int motorDirection =1;

void setup() {
// put your setup code here, to run once:
pinMode(directionButton, INPUT);
pinMode(onOffButton, INPUT);
pinMode(Hbridge7, OUTPUT);
pinMode(Hbridge2, OUTPUT);
pinMode(HbridgeEnable, OUTPUT);
digitalWrite(HbridgeEnable, LOW);
}

void loop() {
// put your main code here, to run repeatedly:
onOffButtonState=digitalRead(onOffButton);
delay(1);
directionButtonState=digitalRead(directionButton);
//motorSpeed=analogRead(pot)/4;

if(onOffButtonState != previousOnOffButtonState){
if(onOffButtonState ==HIGH){
motorEnabled=!motorEnabled;}
}
if(directionButtonState != previousDirectionButtonState){
if(directionButtonState ==HIGH){
motorDirection=!motorDirection;}
}
if(motorDirection==1){
digitalWrite(Hbridge7, HIGH);
digitalWrite(Hbridge2, LOW);}

else{
digitalWrite(Hbridge7, LOW);
digitalWrite(Hbridge2, HIGH);}

/* if(motorEnabled==1){
analogWrite(HbridgeEnable, motorSpeed);}
else{
analogWrite(HbridgeEnable,0);}
previousDirectionButtonState=directionButtonState;
previousOnOffButtonState=onOffButtonState;*/
}

actuator 3.jpg

actuator 3.jpg

Your motorSpeed is initialized to 0 and never changed. Change the line

//motorSpeed=analogRead(pot)/4;

to

motorSpeed = 200;

Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code. You can go back and edit your original post to include the code in code tags.

Thank you @groundFungus,
Now it moves, but it does not read the buttons: as soon as I upload the sketch, it moves, and once reached the max (min) extension, it still tries to move in the same direction. I suppose that it may -need- the unused wires to read its position, but I do not understand why it does not receive the buttons input to stop and reverse

It’s a surprise it moves, the battery and the L293 driver are not up to the task.

You cannot run a 12 volt motor with 650 ma of stall current with a 9v transistor radio battery, they cannot deliver sufficient current. After you obtain a decent 12 volt supply, perhaps an amp or more, you’ll need to eliminate the breadboard, they’re designed for logic signals, not motor wiring.

The L293D is an ancient, inefficient device with high voltage drop. You’ll need to put in 14-15 volts to get 12 out. A modern mosfet based H bridge would not have that issue. Polou makes some nice products. In a pinch, a DPDT relay could be used for low duty cycle applications.

How are your button switches wired. The best way to wire them is to have one side of the switch go to ground and the other side of the switch goes to an input pin set to pinMode(pin, INPUT_PULLUP). That will turn on the internal pullup resistor so the pin is not floating when the switch is open. The input will read LOW when the button is pressed and HIGH when not pressed.

And I agree with WattsThat. The 9V smoke alarm battery will not last long at all and the L293 motor driver wastes a lot of power.

Thank you @WattsThat, I promise that I will find a better battery and motor driver for the future :wink: but I would very much like to make this work because it should! I need to move no load, it is just for learning, so I do not really care about the low voltage
I changed the two inputs to
pinMode(directionButton, INPUT_PULLUP);
pinMode(onOffButton, INPUT_PULLUP);
with no result

Did you change your code to reflect the logic of the switches connected to ground (LOW when pressed).

If you do make changes in your code, please post the latest version so that we can keep up. Use the IDE autoformat tool before posting. Post code in code tags.

const int Hbridge7 = 2;
const int Hbridge2 = 3;
const int HbridgeEnable = 9;
const int directionButton = 4;
const int onOffButton = 5;
//const int pot = A0;

int onOffButtonState = 0;
int previousOnOffButtonState = 0;
int directionButtonState = 0;
int previousDirectionButtonState = 0;

int motorEnabled = 0;
int motorSpeed = 0;
int motorDirection = 1;

void setup() {
  pinMode(directionButton, INPUT_PULLUP);
  pinMode(onOffButton, INPUT_PULLUP);
  pinMode(Hbridge7, OUTPUT);
  pinMode(Hbridge2, OUTPUT);
  pinMode(HbridgeEnable, OUTPUT);
  digitalWrite(HbridgeEnable, LOW);
}

void loop() {
  onOffButtonState = digitalRead(onOffButton);
  delay(1);
  directionButtonState = digitalRead(directionButton);
  motorSpeed = 200;

  if (onOffButtonState != previousOnOffButtonState) {
    if (onOffButtonState == LOW) {
      motorEnabled = !motorEnabled;
    }
  }
  if (directionButtonState != previousDirectionButtonState) {
    if (directionButtonState == LOW) {
      motorDirection = !motorDirection;
    }
  }
  if (motorDirection == 1) {
    digitalWrite(Hbridge7, HIGH);
    digitalWrite(Hbridge2, LOW);
  }

  else {
    digitalWrite(Hbridge7, LOW);
    digitalWrite(Hbridge2, HIGH);
  }

  if (motorEnabled == 1) {
    analogWrite(HbridgeEnable, motorSpeed);
  }
  else {
    analogWrite(HbridgeEnable, 0);
  }
  previousDirectionButtonState = directionButtonState;
  previousOnOffButtonState = onOffButtonState;
}

I wired up a test circuit with an Uno, a 754410 (L293 equivalent) and 2 switches to duplicate as closely as I can.
The motor is stopped when the program starts and then will respond to the switches. A schematic of your project and photos of your wiring could be helpful. I have to suspect a wiring problem because the code seems to be working for me.

@hgroundFungus, you are right. I badly connected the resistors to the switches. well, thank you!!!

The other three wires are probably a linear potentiometer that you have to read and use calculate position.