Im basically trying to make a stepper motor move with an ultrasonic sensor. All of the stuff is from the elegoo arduino uno kit. (stepper 28BYJ-48 and sensor hc sr04) My coding is not great so it may look ugly but there arent any errors and it uploads fine but the stepper motor doesn't move. Anything will be appreciated
#include <SR04.h>
#include <Stepper.h>
//www.elegoo.com
//2018.10.25
/*
Stepper Motor Control - one revolution
This program drives a unipolar or bipolar stepper motor.
The motor is attached to digital pins 8 - 11 of the Arduino.
The motor should revolve one revolution in one direction, then
one revolution in the other direction.
*/
#define TRIG_PIN 12
#define ECHO_PIN 13
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
const int rolePerMinute = 15; // Adjustable range of 28BYJ-48 stepper is 0~17 rpm
int stepCount = 0;
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
void setup() {
myStepper.setSpeed(rolePerMinute);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
a=sr04.Distance();
Serial.print(a);
Serial.println("cm");
delay(1000);
// Roatate Stepper if distance is more than 10cm
if(a<=20)
{// Rotate CW 1/2 turn slowly
myStepper.step(1);
Serial.print("steps:");
Serial.println(stepCount);
stepCount++;
delay(500);
}
//myStepper.setSpeed(rolePerMinute);
//myStepper.step(stepsPerRevolution);
// Serial.println("clockwise");
// myStepper.step(stepsPerRevolution);
}
arduino uno from elegoo. yes it prints the steps when the sensor detects <20. i currently do not have a picture of my arduino. The motor worked before I tested with the example code so it isn't a motor issue.
All that means is the syntax is understood by the compiler, nothing more.
It doesn't mean it will do what you want it to.
No one asked you for a picture, we need to see a schematic, hand drawn is fine Fritzing is not.
However there is nowhere in your code where you instruct the stepping motor to move more than one step every one and a half secons, and with 2408 steps per revolution it is no wonder that it doesn't appear to move..
It does what you wrote in your sketch, it even does move a little.
I put your project the Wokwi simulator. Start the simulation, click on the HC-04 ultrasonic module to change the distance below 20cm, then see the stepper slowly move.
Wokwi uses the standard stepper motor of 200 steps per revolution.
If you really have a stepper motor of 2048 steps (a stepper with gears ?), then you might not notice the minimal rotation.
this is the 5v step motor from the elegoo kit. its the 28byj-48. The sample code with the kit uses 2048 so that's what I believe to be the steps per revolution