Stepper vibrates, no move

Hi there!
I know, I know - there are plenty of cases of steppers not rotating. But I think this is a special case:

the background:
originally I wanted to build a simple ultrasonic radar. But I had nothing to mount the ultrasonic on the stepper thus I decided to let the distance decide about the rotation direction.

So I built everything together and tested with the same configuration first the ultrasonic (HC-SR04) and then the stepper (28BYJ-48, 5V DC). And both worked, separately. Please note: I didn't change any of the electrical setup, I just uploaded different sketches: SR04_Example and stepper_onerevolution.

And now come the tricky part:
I combined the two in one sketch. From what I understand that should work too...
No, it didn't work.

Though it didn't make real sense I added an additional power supply on the breadboard. But as expected that didn't anything.
btw, the sensor works.

Do have any idea where the problem is?

As a minor side question if it is ok:
the program for the stepper alone should deliver one rotation in each direction. But it does not. It is a little bit more.
Any idea how to fix this? Or is it a problem of my stepper?

SR04_Example.ino (333 Bytes)

UltraschallcontrollsStepper.ino (803 Bytes)

stepper_oneRevolution.ino (947 Bytes)

magus66:
No, it didn't work.

That does not provide any useful info from which to help you. Tell us in detail what it actually does. Your combined code does not print the value from the sensor - are you sure you are getting a useful value?

You have not told us exactly what you were hoping would happen when you combined the programs. For example if you are hoping that the motor would change direction when it gets too close then your program is not appropriate because

myStepper.step(rotation);

will complete all the steps before it tests the sensor again. You should probably be moving one of a few steps between tests.

I suspect it is a mistake that you also have

myStepper.step(rotation);

when you want to reverse

For the future include short programs in your Post. Use the Code button </>. People reading on tablets can't open attachments.

...R

Thanks.

Tell us in detail what it actually does.

Well it vibrates and the diodes are going on/off.

You have not told us exactly what you were hoping would happen when you combined the programs.

My main objective is experimenting. In this case I wanted the stepper to turn clockwise a certain amount and if the next distance measurement shows a value below DistLimit then the next time it should turn counterclockwise.

Your combined code does not print the value from the sensor - are you sure you are getting a useful value?

I know it because at a certain distance I can read 'counterclokwise' on the serial.

I suspect it is a mistake that you also have

Absolutely YES :frowning:
But that does not change anything about the basic problem. I wouldn't mind if it run all the same direction if it runs at all :wink:

I am just wondering how can the same setup work for both components standalone but not in combination, at least as the stepper ist concerned.

For the future include short programs in your Post. Use the Code button </>. People reading on tablets can't open attachments.

Thanks for the info. I am still learning...

Here are the three programs used on the same unchanged setup:

Stepper

//www.elegoo.com
//2016.12.12

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

 */

#include <Stepper.h>

const int stepsPerRevolution = 1800;  // change this to fit the number of steps per revolution

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

void setup() {
  // set the speed at 20 rpm:
  myStepper.setSpeed(20);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

Ultrasonics:

//www.elegoo.com
//2016.12.08
#include "SR04.h"
#define TRIG_PIN 5//ursprüngl. 12
#define ECHO_PIN 6//ursprüngl.  11
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;

void setup() {
   Serial.begin(9600);
   delay(1000);
}

void loop() {
   a=sr04.DistanceAvg(30, 3);
   Serial.println(a);
   //Serial.println(" cm");
   delay(100);
}

my combination of the two (mistake in rotation removed):

/*
 * ursprüngliche Idee: ultrasonic radar
 * 
 * now: change the direction of the stepper when the measures distnce is below a certain thteshhold
 */


#include "SR04.h"
#include <Stepper.h>

const int TRIG_PIN = 5;
const int ECHO_PIN = 6;
const int stepsPerRevolution = 1500;


Stepper myStepper(stepsPerRevolution, 8,9,10,11);
SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);

void setup() {
  // put your setup code here, to run once:
  myStepper.setSpeed(30);
  Serial.begin(9600);

  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
  long a = sr04.DistanceAvg(30,3);

  const int rotation = 1000;
  const int DistLimit = 50;

  if (a<DistLimit) {
    Serial.println("clockwise");
    myStepper.step(rotation);
  } else {
    Serial.println("counterclockwise");
    myStepper.step(-rotation);
  }

  delay(500);
}

magus66:
I know it because at a certain distance I can read 'counterclokwise' on the serial.

That leads me to wonder if the SR04 library and the stepper library do not like each other. I am not familiar with the internal details of either. Unfortunately, libraries clashing is not unusual on an Arduino.

From the depths of my ignorance all I can suggest is trying the use different I/O pins for the motor. The Atmega Hardware Timers are linked to specific pins and it is probably that the SR04 library is using one of the Timers. On an Uno Timer0 is linked to pins 5 and 6, Timer1 to pins 9 and 10 and Timer2 to pins 3 and 11. My first try would be to change from using pins 9 and 10 for the motor. Note that you can use the analog pins for digital I/O

...R