Code has no errors but isnt working. Ultrasonic sensor and stepper motor

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);
 }

what Arduino are you using?
upload a schematic of the circuit?
upload a copy of the serial monitor output?
does it ever print

Serial.print("steps:");

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..

Welcome to the forum.

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.

Link to your sketch in Wokwi:

how can i make it spin faster?

Remove, or reduce drastically, the two delay calls, they control your speed.

Do you really have a stepping motor that will take 2048 steps per revolution?

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

There is no way that this motor will require 2048 steps per revolution.

In fact this web site components101.com says

this means that the motor will have to make 64 steps to complete one rotation and for every step it will cover a 5.625°

Which is more realistic.

Those motors have a gear train so can actually have 2048(ish) pulses per revolution of the output shaft. See the Gear Reduction Ratio paragraph of >> In-Depth: Control 28BYJ-48 Stepper Motor with ULN2003 Driver & Arduino

1 Like

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