I found a forum where someone asked about doing exactly what I’m trying to do, use a RAMPS board to drive stepper motors not for a 3D printer. They were directed to use this code example to get started. What great fortune, I don’t have to figure this out on my own!

Unfortunately, it wasn’t that straight forward. Fortunately, that forced me to learn some stuff.

To start, within the Marlin firmware there is a library called pins_RAMPS.h which contains the pin numbers for most things on the RAMPs board.

DescriptionPin Number
X_STEP_PIN54
X_DIR_PIN55
X_ENABLE_PIN38
RAMPs X Axis Pins

Subbing in the step and direction pins into that code linked above did not work, it in fact did nothing. That makes sense when you learn how the enable pin works. When it is high, the motors are disabled, when it is low, the motor is enabled. By default the pin is high. So I updated the code to drive this pin low, and… it still doesn’t work. BUT! Now the motor at least twitches, which it didn’t do when the motor was disabled. Progress!

When I was writing the first blog on this, I read that for the A4988, you should only back the right most jumper installed. When I did this, the motor only twitched. Once I put all 3 jumpers on, it worked properly. I’m thinking this might be similar. Maybe the pins that drive the A4988 resolution pins high are controlled by an output of other pins… seems far fetched, but maybe?

The RAMPS.h library doesn’t list any pins that sounds like this, so I just plugged the board in and measured them. Sure enough, they’re already high. So much for that.

On the other hand, maybe the resolution pins being held high IS the problem. I unplugged everything, pulled out the driver, and removed the 3 jumpers. Then I plugged everything back in and it worked! Slowly the shaft of the stepper motor started whirling back and forth and then stopped just like the code example was written to do. Awesome!

So now I need to figure out how to get the resolution pins working… or not. I can probably make due to 200 steps making up a full circle.

One thing to note, I added a line to put the enable pin low, but I never put it back to high. That means that the motor is locked in the last position it was sent to. This could be really handy if I ever get to the point where I have retractable lids for the food bowls, that way they stay locked shut and the kitties prying with the claws shouldn’t be able to get past it. On the other hand, that means that the motors are always drawing power to stay locked. For the motor that drives the auger, a lock really isn’t necessary and in fact probably shouldn’t be used since I have the current pushing a little higher than is likely save for the motor.

Now that I have Arduino code directly controlling the motor, I should try it with the auger and see how many steps it takes to move one increment and how fast seems reasonable.

The number of steps is pretty easy, there are 6 cavities on the auger, so 200/6 ~ 33 steps. I have a mini load cell, so I might use that to weight the hopper and wait for weight to suddenly decrease, that way the 0.33 steps that I’m rounding off won’t catch up with me someday and one cat doesn’t get fed. I could also adjust it so every 6th rotation spins 34 steps… but we’ll see how this evolves.

As you can see in the video above, the screws holding the motor to the mount are a little loose which is hopefully why it skips and stutters. They’re not actually the proper screws for the threads on the stepper; they held tight at first but slowly loosened over time. If that doesn’t fix it, I may have to increase the reference voltage more, especially once there’s kitty food in here increasing the resistance.

I was getting annoyed playing with the length of time between steps as a means of controlling the speed, so I found a library on this website that works with steppers and drivers; accelStepper. It’s available for download via “Manage Libraries” under the Tools drop down.

I added a line to enable the motors and played with the speeds to see what seemed reasonable.

/*Example sketch to control a stepper motor with A4988 stepper motor driver, AccelStepper library and Arduino: continuous rotation. More info: https://www.makerguides.com */
// Include the AccelStepper library:
#include <AccelStepper.h>
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 55
#define stepPin 54
#define motorInterfaceType 1
#define enPin 38
  
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
  // Enable Motor
  pinMode(enPin, OUTPUT);

  // Set the maximum speed in steps per second:
  stepper.setMaxSpeed(1000);
}
void loop() {
  digitalWrite(enPin, LOW);
  // Set the speed in steps per second:
  stepper.setSpeed(20);
  // Step the motor with a constant speed as set by setSpeed():
  stepper.runSpeed();
}

I settled on a speed of 20 being reasonable. Once I get proper screws installed between the mount and the motor, I’ll revisit this with food in the hopper and see what works.

This spinning constantly is pretty annoying, so I wired a button up to the X axis min end stop (Pin 3 as per pins_RAMPS.h file in Marlin) and programmed it to spin 33 steps when pressed soon.

// Include the AccelStepper library:
#include <AccelStepper.h>
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 55
#define stepPin 54
#define motorInterfaceType 1
#define enPin 38
#define xEndStopPin 3
  
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
  Serial.begin(9600);
  pinMode(enPin, OUTPUT);

  // Set the maximum speed in steps per second:
  stepper.setMaxSpeed(1000);

}
void loop() {
  int xEndStopVal = digitalRead(xEndStopPin);
  Serial.println(xEndStopVal);
  if (xEndStopVal == LOW) {
    digitalWrite(enPin, HIGH);
  } 
  else {
    digitalWrite(enPin, LOW);
    delay(1000);

    while(stepper.currentPosition() != 33) {
      stepper.setSpeed(20);
      stepper.runSpeed();
    }
  }
  delay(1000);
  stepper.setCurrentPosition(0);
}

Now it’ll be easier to test going forward and this code should come in hand as I start a proper firmware file.

Next steps are going to be designing something to hold this contraption and either move it on a gantry or move a pipe underneath it to be able to put food in multiple bowls from the single hopper.

1 Comment

Leave a Reply to Javier Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.