Linear Actuator Help

Hello,

I am using the following setup for my linear actuator system:

500mm Linear Actuator 1605 Ball Screw Motion Guide Rail
Nema 23 Bipolar Stepper Motor
TB6560 Easy Driver
Arduino Mega Board
Power supply using 12 V and .275A

I am having trouble performing a simple back and forth motion between two fixed points.

When I use this code, the linear actuator responds by going in random directions each time and not consistently going left, stop, right, stop, and etc.

Any help would be greatly appreciated.

I have attached the code here to better explain the issue:

int Distance = 0;
void setup(){
pinMode(8 , OUTPUT); //STEP PIN

pinMode(9 , OUTPUT); // DIR PIN

digitalWrite(8,LOW);

digitalWrite(9,LOW);

}

void loop(){

digitalWrite(8,HIGH);

delayMicroseconds(100);

digitalWrite(8,LOW);

delayMicroseconds(100);

Distance = Distance + 1;

if (Distance == 8000) {
delay(500);

if (digitalRead(9)==LOW) {

digitalWrite(9,HIGH);}
else {

digitalWrite(9,LOW);

}
Distance = 0;
}
}

Why do you suddenly start reading an output pin, #9. As soon as you begin that that pin is always an input and will have noise on it, I suspect. Modify you code logic.

Paul

Would you mind elaborating on that? What would you suggest instead?

bcturne2:
Would you mind elaborating on that? What would you suggest instead?

As soon as this code is executed:

if (digitalRead(9)==LOW) {

digitalWrite(9,HIGH);}
else {

digitalWrite(9,LOW);

pin 9 is no longer an output pin it will always be an input because you never set it to output again.

Change the logic to just have a byte variable set to 0 or one and set it to match the digital write value. Then check that variable value to see which way to set pin 9.

Paul

Post a link to the stepper motor data sheet and tell us what you selected for the motor current limit. I suspect the power supply is inadequate.

Please use code tags when posting code.

Why do you suddenly start reading an output pin, #9. As soon as you begin that that pin is always an input

??
This code happily blinks an led connected to pin 9.

void setup()
{
  pinMode(9, OUTPUT);
}

void loop() 
{
  if (digitalRead(9) == LOW)
  {
    digitalWrite(9, HIGH);
  }
  else
  {
    digitalWrite(9, LOW);
  }
delay(500);
}

cattledog:
??
This code happily blinks an led connected to pin 9.

void setup()

{
  pinMode(9, OUTPUT);
}

void loop()
{
  if (digitalRead(9) == LOW)
  {
    digitalWrite(9, HIGH);
  }
  else
  {
    digitalWrite(9, LOW);
  }
delay(500);
}

Very interesting! So, except for identifying a pin as input-pullup, all others are just for documentation purposes.

Paul

No OUTPUT is totally necessary.

On very small microcontrollers, the output pins are memory locations that can be used to store binary data. On things like the ATTiny series, the packages are so small that there are many outputs on the silicon that aren't connected to pins on the package. When you are really short on memory you can use the 'pins' for storage.

So reading from an output will return what you last wrote to it and doesn't change it to INPUT mode.

analogRead() is different. That changes the pin mode.

MorganS:
No OUTPUT is totally necessary.

On very small microcontrollers, the output pins are memory locations that can be used to store binary data. On things like the ATTiny series, the packages are so small that there are many outputs on the silicon that aren't connected to pins on the package. When you are really short on memory you can use the 'pins' for storage.

So reading from an output will return what you last wrote to it and doesn't change it to INPUT mode.

analogRead() is different. That changes the pin mode.

Interesting! That is so clever. Years ago some of the USART chips in PCs I was dealing with had a byte or two of memory and I could determine which chip I was talking to.

Thanks, Paul

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks… Tom… :slight_smile:

How 'bout:

digitalWrite(9,! digitalRead(9));

You didn't give the pin a name. Doing a search-replace when you change a pin number next week will be very time-consuming.

At this point, the code is able to translate the linear actuator back and forth, however, the actuator motor moves the actuator forward two times occasionally instead of the correct motion.

I am fairly new to Arduino code and while I appreciate all of the responses, I don't exactly understand how to implement them.

Would someone mind explaining to me what the code should look like for the actuator to go strictly back and forth for a specified distance with an integrated pause in between motions with my pin configuration (Pins 8 and 9)?

Hi,
Can you post your code as per request in post #9.
That way it will be formatted to appear correctly in the post.

Also;

 if (Distance == 8000) {

make

 if (Distance >= 8000) {

Thanks.. Tom... :slight_smile:

My mistake, here is my code.

int Distance = 0;
void setup(){
  pinMode(8 , OUTPUT); //STEP PIN
  
  pinMode(9 , OUTPUT); // DIR PIN
  
 digitalWrite(8,LOW);
  
 digitalWrite(9,LOW);
  
}

void loop(){
  
  digitalWrite(8,HIGH);
  
  delayMicroseconds(100);
  
  digitalWrite(8,LOW);
  
  delayMicroseconds(100);
  
  
  Distance = Distance + 1;
  
  
  
  if (Distance == 8000) {
    delay(500);
    
  if (digitalRead(9)==LOW) {
  
  digitalWrite(9,HIGH);}
  else {
    
  digitalWrite(9,LOW);
  
  }
  Distance = 0;
  } 
}
  pinMode(8 , OUTPUT); //STEP PIN

If you give the pin a name (like stepPin) then the comment is unnecessary.

You have a very high step speed. Almost 5000 steps per second. With no acceleration, the motor can't get to that speed in a 5000th of a second.

Maybe try the AccelStepper library?