Stepper motor rotate 90 and back to 0

I'm tempted to make an animal feeder with a stepper motor, can anyone help me with the code?

just use that as a search term and you'll find plenty of them.

What gear have You got? Controller, driver, power supply....?
Schematics would be nice to see before spending time on any code.

A gear motor will probably give you more success than a stepping motor.

Stepper motor: 28 byj-48
arduino: Arduino Uno

Here is a demo code using the MobaTools stepper library that will move a stepper, of the type shown, 90 degrees, pause for a settable time and return 90 degrees to the starting position initiated with the push of a button. There is no facility to home the motor, that may be needed. The MobaTools library is available for installation via the IDE library manager.

This is an example of non-blocking code using a state machine.

This code has been successfully tested with real hardware.

#include <MobaTools.h>

const byte pin1 = 4;
const byte pin2 = 5;
const byte pin3 = 6;
const byte pin4 = 7;
const byte buttonPin = 12; // button connected to ground

const unsigned int motorStepsPerRev = 2048;

MoToStepper stepper( motorStepsPerRev, HALFSTEP);

byte mode = 0; //machine state

unsigned long timer = 0;
unsigned long pauseTime = 3000;

void setup()
{
   Serial.begin(115200);
   Serial.println("Stepper conrol with the MobaTools stepper library");
   Serial.println("Press button to begin cycle");
   
   stepper.attach(pin1, pin2, pin3, pin4 );
   stepper.setSpeed(80);  // rpm /10
   stepper.setRampLen(10);
   stepper.setZero();
   
   pinMode(buttonPin, INPUT_PULLUP);
}

void loop()
{
   if (mode == 0)
   {
      if (digitalRead(buttonPin) == 0)
      {         
         mode = 1;
      }
   }
   else if (mode == 1)
   {
      stepper.write(90);
      mode = 2;
   }
   else if (mode == 2)
   {
      if (stepper.stepsToDo() == 0)
      {
         timer = millis();
         mode = 3;
      }
   }
   else if (mode == 3)
   {
      if (millis() - timer > pauseTime)
      {
         mode = 4;
      }
   }
   else if (mode == 4)
   {
      stepper.write(-90);
      mode = 5;
   }
   else if (mode == 5)
   {
      if (stepper.stepsToDo() == 0)
      {
         mode = 0;
      }
   }
}

The advantages of the MobaTools stepper library over the built in Stepper library are that the MobaTools library uses non blocking motion functions and uses acceleration so higher speeds are possible.

1 Like

The Arduino is not a power supply !


If you continue to power your stepper with the the Arduino, you will destroy the Arduino.

1 Like

I agree with @LarryD, powering the stepper from the Arduino is not a good idea.

image

Make sure that the external 5V supply ground connects to the Arduino ground.

@ruip31 - How is your pet feeder with a servo?

No, um using stepper motor

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.