Stepper Motor not running

Hi, I've been trying to make a stepper motor to run at half the speed of another motor, however the motor refuses to spin. The motor works but not whith this code. The RPM of the controlling motor is measured using a hall sensor switch. I am using the Adruino Uno R3 and the stepper motor is wired according to the schematic given on Arduinos website with a 9-volt battery as secondary power source:

The picture has a potentiometer to control the speed of the motor but I instead connected a hall sensor to PWM2.
This text will be hidden

The code itself is essentially in two parts. The first part is just the tachometer to get a RPM, and then a simple motor control to set the speed of the stepper motor to half
of that measured. Code below:

#include <Stepper.h>

const int hall_pin = 2;
const float hall_thresh = 50; //number of times the hall sensor is tripped before rpm is calculated

const int stepsPerRevolution = 200;


void setup() {

  Serial.begin(115200);
  pinMode(hall_pin, INPUT);
}

Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void loop() {
//Tachometer
  bool on_state = true;
  float hall_count = 1.0;
  float start = micros();
  while (true) {
    if (digitalRead(hall_pin) == 0) {
      if (on_state == false) {
        on_state = true;
        hall_count++;
      }
    } else {
      on_state = false;

    }

    if (hall_count >= hall_thresh) {
      break;
    }
  }
  float end_time = micros();
  float time_passed = ((end_time - start) / 1000000.0);
  Serial.print("Time Passed: ");
  Serial.print(time_passed);
  Serial.println("s");
  float rpm_val = (hall_count / time_passed) * 60.0;
  Serial.print(rpm_val);
  Serial.println(" RPM");


//Motorcontrol
  int motorspeed = rpm_val / 2;
  if (motorspeed > 0) {
    myStepper.setSpeed(motorspeed);
    Serial.print(motorspeed);
    Serial.println(" motorspeed");


  }

}

The issue I am experiencing is that I can't get the motor to run at all. I've tried the different parts of the code separately and they both work on their own, but when I try to combine them only the rpm meter works, with no rotation on of the steppermotor. I've also tried to set the motorspeed manually but without any success.

Thank you in advance for any help

What is supplying the power for the motor?

The steppermotor is powered by both the arduinos 5v output and a 9v battery, the motor where the rpm is measured is a simple dc motor powered by its own 9v battery

You set the speed, but you never tell the motor to actually step.

oh yeah that makes sense, I realized now that I removed parts from the code, I'll try fixing it and get back to you with the results

Hi,
in general, 9V batteries cannot supply currents to drive stepper motors.
They are not built to supply a lot of current.

The L293 is an ancient and very inefficient DC motor driver. It is, by today's standards, a crappy motor driver and even worse stepper driver. The output stage soaks up 2V to over 4V and wastes that power as heat which makes them a poor choice for battery power. Modern MOSFET output drivers are much more efficient.

Post a data sheet for the stepper and we can help you find a proper driver and power supply.

Is your 9V battery like this?

If so, save it for your smoke detector.

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