I am trying to use a button (a step pad from Spirit Halloween) to move a step motor (28BYJ-48) back and forth 3 times then stop until the button is pushed again. So far I have managed to make the step pad work (using the pin13 LED as an indicator) and have managed to make the motor move but can't seem to get the code to work together.
I have tried reading and troubleshooting by myself to no avail. In order to properly see what state the Nano believes its in I added an "else" to turn on the pin13 LED when the pad is not pressed. When running the program I simply get the LED constantly on whether the button is pressed or not.
This is my current code:
#include <Stepper.h>
const int stepsPerRevolution = 2038;
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
(buttonState = digitalRead(buttonPin));
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(500);
delay(750);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(500);
delay(750);
Serial.println("clockwise");
myStepper.step(500);
delay(750);
Serial.println("counterclockwise");
myStepper.step(500);
delay(750);
Serial.println("clockwise");
myStepper.step(500);
delay(750);
Serial.println("counterclockwise");
myStepper.step(500);
delay(750);
}
else {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
}
If its not something obvious in the code itself I will happily post a schematic of how I have everything wired. And yes, I am aware I have not added anything to stop the motor once the sequence is complete, I'll get there one day.
My guess is that your button pin is floating when not pressed, and as @alto777 mentioned you never turn the LED off so once on it stays that way.
Regarding the button pin... you need to ensure that it is always connected to VCC or GND. This is done with pull-up/pull-down resistors. The easiest way is to use the Arduino's internal pull-up resistors.
If you define the pin as
pinMode(buttonPin, INPUT_PULLUP);
Then connect the other side of the button to GND... and to check it's pressed you look for...
Killzone_kid, I'm currently working on drawing something up.
a7, I feel real dumb. I added digitalWrite(ledPin, LOW); as the first line of the "if" statement. It does seem the button is working but I'm still not getting motor movement. I can feel a slight vibration of the motor trying to do anything but am confused why it wouldn't be able to move when the switch triggers it.
Yeah, thats kind of what I thought. Not sure why the voltage would drop off though. I'm going to run it off of a different DC power source and drive it through ULN2003AN but I took that out of line in order to simplify when troubleshooting.
Oof. That’s an attempt. I KNOW I KNOW I should not be powering the step motor off of the usb port on the nano. I’m quite sure going through the motor driver board with external power source will fix this but that’s a problem for the morning.
Or change it so the switch is wired to ground, and the pin is INPUT_PULLUP or use the resistor (10K) to make the pin read HIGH when the button is not pressed.
Very more common. Invert the switch logic at some point.
Okay, sorry ya’ll. You were all very right and my drawing was wrong. I have been using breadboards so staring at them long enough has led me to make some mistakes.
The switch/LED seem to be working just fine. Now when I add the motor driver in it’s like the arduino doesn’t want to talk to it. Here’s an updated drawing with the motor driver inline.
It’s not super legible but on the driver pins IN1-IN4 are connected to the arduino’s pins D8-D11 in that order. Also the 5 wires and connector on the motor are connected. There is only one way to insert them.
I’ve metered and I seem to be 5v at the source and going into motor driver than a fluctuating voltage around 2.5 v going between the ground and each of the 4 legs. I also checked to make sure that the reading was in fact the same between the legs and the ground on the arduino and everything was consistent. The motor itself simply got hot so I disconnected things in order to not fry the motor. I’ve tried a different motor and different driver but same issue. I also plugged the original motor directly into the arduino and it still works just fine.
Also for troubleshooting purposes I’m running the basic stepper_one revolution code and as it works plugged directly into the arduino I now don’t think the coding is the problem (I know, wrong forum).
Any thoughts? Sorry again to be a pain but thanks for your patience with me.
ANYWAY I'm still at a dead end with my motor but I will take that question elsewhere so I'm not continually bugging you folks. Thanks for helping me learn!
Okay! So the code I wrote originally at the beginning of the post seems to be working in that I can feel the motor try to move 6 times before stopping when the button is pressed, but only if it’s attached directly to D7-D11 (though only D8-D11 are at work in the code.
Also I can see that the LED in pin 13 stays on for the duration of the motor move then turns off.
As said before the thought was that there wasn’t enough power so I am trying to drive it through a motor driver (ULN2003AN) with a 28BYJ-48 step motor. I have it powered through a 5v dc power supply. The motor drivers LEDs turn on and stay constant and the motor itself just gets hot (I have been pulling it out before it’s supernova but I do have a spare motor and spare motor driver that are exactly the same thing).
The behavior is consistent across the primary and the spare board and the primary and the spare motor leading me to believe it’s not the physical hardware.
As I said I'm using the same code (with the added LED off) but I'm posting it here just so its all in one place.
#include <Stepper.h>
const int stepsPerRevolution = 2038; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(500);
delay(750);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(500);
delay(750);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(500);
delay(750);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(500);
delay(750);
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(500);
delay(750);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(500);
delay(750);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
I swear I'm trying to troubleshoot on my own but I've just hit a wall.