BYJ48 Stepper motor control

I am using the byj48 Stepper motor with the Arduino CustomStepper Library.

Everything is going good but I would like to control when the Stepper performs each move.

I tried using an if statement like you would with a normal led on button.

Here’s my code :slight_smile:

What I am trying to achieve is the stepper motor to perform the action inside the if statement IF the button is pressed.

Thanks

#include <CustomStepper.h>


CustomStepper stepper(2, 3, 4, 5, (byte[]){8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001}, 4075.7728395, 12, CW);
boolean rotate1 = false;
boolean rotatedeg = false;
boolean crotate = false;

const int buttonPin = 2;
int buttonState = 0; 

void setup()
{
  
  stepper.setRPM(12);
  stepper.setSPR(4075.7728395);
  pinMode(buttonPin, INPUT);
}

void loop()
{
  
  buttonState = digitalRead(buttonPin);
  
  if (stepper.isDone() &&  rotate1 == false && buttonState == HIGH)
  {
    
    stepper.setDirection(CCW);
    
    stepper.rotate(2);
    rotate1 = true;
  }
  
  stepper.run();
}

How have you wired the button?

I have wired it with power going to either side of PTM, but the negative goes through a 10K resistor. Then I have the digital pin 2 from arduino on the negative leg.

Exactly as shown here :

http://arduino.cc/en/tutorial/button

What happens when you press the button? Try the "button" example in the Arduino to test it, before using it in a larger sketch.

Note that there are two ways to plug in the switch and only one will work. It may need to be rotated 90 degrees in order to function properly.

Tried that, and I'm positive the switch is in correctly. If I turned it ninety degrees it would not work as I am using a breadboard. Plus I have run the button sketch with no problems..?

Thanks but I think it's more on the code side, and I have much less experience with coding :D

b8el018: Tried that, and I'm positive the switch is in correctly. If I turned it ninety degrees it would not work as I am using a breadboard. Plus I have run the button sketch with no problems..?

This sounds like you are positive because of deductive reasoning rather than an actual test. Guess which is more reliable.

...R

Sorry. Sometimes what i say only makes sense in my head. So Ill re-phrase it : I am certain the button is wires up correctly due to the fact it works with identical wiring with the button sketch to light an LED.

Anyway do you know what my problem is and can you help?

From Louis

Anyway do you know what my problem is and can you help?

You haven't said exactly what the problem is.

It is always a good idea to put print statements in your program to test whether parts of it are working. For example, just before the IF statement in question, print out the button status. Does that status change if you press the button?

Won't a BYJ-48 sink more than 40mA, thus burning the Arduino pin? If you limit that current with a 10kohm resistor, there won't be enough current to power the motors.

Even although the BYJ-48 are tiny motors, they are still enough to damage the arduino.

I suggest using a L293D, UNL2003 or similar to interface with the motors. If you don't have those, at least place some FETs (or even BJTs) between the motors and the Arduino. This might save your Arduino.

Yeah... Erm I may have forgotten to mention I actually do have one of those. I can't check which but It came with it. Ill update the question, thanks.

jremington: You haven't said exactly what the problem is.

It is always a good idea to put print statements in your program to test whether parts of it are working. For example, just before the IF statement in question, print out the button status. Does that status change if you press the button?

I see how that works. Ill try it later thank you :)

I still can't get this to work, can someone please tell me another way I can use a button to control my Motor.

I have an arduino uno with a ZC A0591 board and then a 28BYJ-48 Stepper motor.

I have successfully used and run the Custom Stepper Library but I just can't get this if statement to work :)

I am not familiar with that motor or that stepper library.

However if your only problem is getting things to happen in response to a button press maybe this pseudo code will give you some ideas

void loop() {
   readButton();  
   runMotor();
}

void readButton() {
    btnState = digitalRead(btnPin);
    Serial.println(btnState);
    if (btnState == LOW && motorDone == true) {
        motorDone = false;
   }
}

void runMotor() {
  if (motorDone == false) {
     // code to make motor do stuff
     motorDone = true;
  }
}

To start with you could put some simple code in the runMotor() function just to make an LED flash and when that works you could substitute the motor code.

...R

Thank you, the custom stepper library has been editted to work with any Gear ratio stepper motors as it allows you to set your own, I think :)

Robin2: To start with you could put some simple code in the runMotor() function just to make an LED flash and when that works you could substitute the motor code.

...R

Okay, so I started with the normal LED Button sketch and made the Led come on when the button is pressed. Then I added a serial.prinln command which also worked. As soon as I add the motor commands everything stops working including the LED and the serial.println commands.

I was thinking maybe I have to use boolean but I'm not sure how to use it.

Thanks

b8el018: As soon as I add the motor commands everything stops working including the LED and the serial.println commands.

You have not posted that code so I cannot make any suggestions.

It would be useful to post the working version with the LED and print commands and separately post the version with the motor commands added.

...R

Ok, sorry I’m new to this,

Here’s the working code with serial.print and LEDWrite

#include <CustomStepper.h>

CustomStepper stepper(2, 3, 4, 5, (byte[]){8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001}, 4075.7728395, 12, CW);

const int LEDPin = 13;
const int buttonPin = 6;

int buttonState = 0;


void setup()
{
  Serial.begin(9600);
  pinMode(LEDPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  stepper.setRPM(12);
  stepper.setSPR(4075.7728395);
}

void loop() {
 
 buttonState = digitalRead(buttonPin);

 if(buttonState == HIGH) {
   Serial.println("Button Pressed!");
   digitalWrite(LEDPin, HIGH);
   }
 
 else {
   
   digitalWrite(LEDPin, LOW);

   }
 }

And what I would think to do next is to add the stepper commands into the if statement.
This is where I get stuck, I’m thinking it’s something to do with the boolean maybe?

#include <CustomStepper.h>

CustomStepper stepper(2, 3, 4, 5, (byte[]){8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001}, 4075.7728395, 12, CW);

const int LEDPin = 13;
const int buttonPin = 6;

int buttonState = 0;



void setup()
{
  Serial.begin(9600);
  pinMode(LEDPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  stepper.setRPM(12);
  stepper.setSPR(4075.7728395);
}

void loop() {
 
 buttonState = digitalRead(buttonPin);

 if(buttonState == HIGH) {
   Serial.println("Button Pressed!");
   digitalWrite(LEDPin, HIGH);
   stepper.setDirection(CCW);
   stepper.rotate(2);
   }
 
 else {
   
   digitalWrite(LEDPin, LOW);

   }
   stepper.run();
 }

Sorry for being such a newb, lol.