Hi!
Any idea why this stepper (28BYJ-48) is not rotating?
I used a debouncing method (still improving this method) to make a push button act like a ON/OFF button, but when it's ON the motor doesn't react at all. Also I want to control the speed of this motor and display this on a LCD. It is just a start project.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <Stepper.h>
const int stepsPerRevolution = 200; // 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);
int stepCount = 0; // number of steps the motor has taken
unsigned long previousMillis = 0;
int M1 = 3; // On/Off
int flag = 0;
void setup() {
pinMode(M1, INPUT);
Serial.begin(9600);
lcd.init();
lcd.clear();
lcd.backlight();
} // setup *****************************************************************
void loop() {
uint32_t currentMillis = millis(); // Millis times uses to debounce the button
const uint32_t BOUNCETIMEOUT = 20; // Debounce time in milliseconds
#define buttonPressed LOW // When the button is pressed the input will be low, this is to remove the confusion this migth cause.
bool currentButtonState = digitalRead(M1); // Reads the current state of the button and saves the result in a bool
static bool lastButtonState;
static uint32_t lastMillis; // Start of the debounce timeout
if ((lastButtonState != currentButtonState)) { // Checks to see if the button has been pressed or released, at this point the button has not been debounced
if (currentMillis - lastMillis >= BOUNCETIMEOUT) { // Checks to see if the state of the button has been stable for at least bounceTimeout duration
lastButtonState = currentButtonState;
// check if you press the SET button and increase the menu index
if (currentButtonState == buttonPressed)
{
if (flag == 0) { // and the status flag is LOW
flag = 1;
while (flag == 0) {
int sensorReading = analogRead(A1); // here is a potentiometer
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
// set the motor speed:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// step 1/100 of a revolution:
myStepper.step(stepsPerRevolution / 100);
lcd.setCursor(0, 1);
lcd.print(motorSpeed);
}
}
Serial.println("ON");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ON");
}
else {
flag = 0;
Serial.println("OFF");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("OFF");
}
}
}
} // if
else {
lastMillis = currentMillis; // Saves the current value of millis in last millis so the debounce timer starts from current millis
}
} // loop ******************************************************
Hi,
Consider writing some code that JUST controls the stepper before adding LCD and other stuff.
Have you tried the stepper library examples to get your hardware sorted and running?
Do you have a DMM?
Can you please post a circuit diagram, not a Fritzy, not a cut and paste from another site, a hand drawn image will be fine of YOUR project, that is reverse engineer your hardware?
Please include power supplies, component names and pin labels.
I know that I have a lot of programming clumsiness.
Somehow I got the desired result, setting a flag as you suggest, but for another case, using hardware interrupts on a button that is for On/Off. In the On state, I wanted to be able to adjust the rotation speed and display something on an LCD.
In the case of this stepper, I have to rethink things. I'll be back,
Well, I am following the examples from Arduino: https://docs.arduino.cc/learn/electronics/stepper-motors
And the circuit for the stepper would be the one below, the difference is that I am using a Nano board.
IN4->D11
IN3->D10
IN2->D9
IN1->D8
In your diagram, you do not. It is not fair to us, to post incorrect or incomplete diagrams. Please update and post an accurate diagram.
I strongly urge you to post a "real" schematic as requested in post #4. It will remove the temptation to take short cuts like sketching in a power line that's not correct. Also leaving out a pile of devices...
We shouldn't have to comb through your posts and mentally annotate your diagram to arrive at a correct view.
Then please add it. This is becoming tiresome. Otherwise, the schematic is a welcome improvement.
I don't believe you can normally run I2C from D4,D5. Those are not I2C capable. Also, Vin should never be connected to a 5V power source. The minimum for that is absolute 7V, nominal 9V.
Power supplies are missing from the diagram. Requested earlier on.
Referring to the power supply, I use a single 5V power supply for everything. So I don't use the regulator on the Arduino as a power supply. Okay, so I should use a 9V supply for the Vin pin.
As for the circuit, in the reply above I posted it and the stepper is connected as I mentioned (ULN2003 is the driver) and as examples I follow these: https://docs.arduino.cc/learn/electronics/stepper-motors .
In these examples I do not see references to I2C.
Hi,
Have you been able to get the stepper moving at all?
Have you a basic bit of code that JUST rotates the stepper, NOTHING else in the code?
Forget the LCD for the moment.
Use serial prints to help debug.
What I am getting at is have you developed your code in stages and got each stage working before going onto the next.
Thanks.. Tom...
Have you looked at the stepper libraries available and ther example codes that they provide.
I found another source of information that describes the 28BYJ-48 stepper in a good way.
I can get to an application that allows me to control or set a desired angle. I will put this stepper into operation again when I have more free time. Until then, I'm still thinking about what I would like this stepper to do.