Newbie with unsuccessful code for stepper

I’m trying to put together my first code for a fully analog antique motorcycle speedometer using a hall sensor and two steppers: one for speed and the other for rotating odometer and trip meter via some gears. I’ve completed the speedometer build and now I’m trying to put together the code for the speed dial stepper. The steppers are tiny 4 wire steppers with Easydrivers. Like I said, only using the speed gauge stepper for now. As I’m using two steppers at the same time, I’m utilizing AccelStepper library.

Attached is my latest attempt on the code. So far, everything I’ve tried works the led but not the motor. I thought I would post it and see if some kind soul would review and let me know what stupid mistake I’m making.

15 mm 4 wire steppers, A1120 hall sensor, arduino uno board, EasyDriver board, 6 volt battery connected to V in.
Code is as follows: Many Thanks

#define LED_BUILTIN 13
float diameter_of_wheel = 2.192; // measure the diameter of wheel and enter here in feet (now for 26.3in od)
volatile byte rotation; //variable for interrupt must be volatile
float timetaken, rpm, detect_time;
int v;
int speeddial;
//int count; to be used for odometer

#include <AccelStepper.h>
AccelStepper stepperS(AccelStepper::DRIVER, 6, 7); // pins for SpeedoS stepper driver
// AccelStepper stepperO(AccelStepper::DRIVER,8,9);  will include odometer later

void setup()
  //  stepperO.setMaxSpeed(300.0);
  //  stepperO.setAcceleration(300.0);
  attachInterrupt(digitalPinToInterrupt(2), magnet_detect, RISING); // interrupt called when sensors sends digital pin 2 high (every wheel rotation)
  rotation = rpm = detect_time = v = timetaken = speeddial = 0; // initialize all variables to zero

void loop()
  if (rotation != 1)
    digitalWrite(LED_BUILTIN, HIGH);
    digitalWrite(LED_BUILTIN, LOW); // led on every other rotation to ensure hall effect is functioning

  if (rotation == 1)
    timetaken = millis() - detect_time; //time in milliseconds ffom last magnet detect  or one rotation
    rpm = (1000 / timetaken) * 60; //to calculate rpm
    v = diameter_of_wheel * 3.14 * rpm * 60 / 5280; //velocity in mphpevtime = millis();
    speeddial = (v * 2.31 / 2.25);
    //Speedo max speed is 120 mph and 10-120 on face is 254 degrees which is 2.31 degrees per mph
    // steppers are 18 degrees per step. Using easydriver 1/8 steps, each step is 2.25 degrees
    // therefore speedo position (speeddial) is integer of velocity (v) x 2.31/2.25
    rotation = 0;  //reset rotation to zero
void magnet_detect()  //called whenever a magnet is detected
  detect_time = millis(); // reset timer for next revolution
  // count++; to be used for odometer

Steppers pull a lot of current, even little ones. Your Arduino probably can’t handle it - the driver needs separate power.

Unless the circumference of your wheel changes every revolution, Pre-compute a variable value for ALL except RPM and simplify your math.


The Easy driver board has an independent 6 volt supply.
Thanks for the advice to simplify math.

I expect that your problem is to do with your time calculation. In your interrupt routine, you set detect_time to millis. Later on you use millis-detect_time in your RPM calc. Given how fast the Arduino is though, there's a good chance that that difference is zero.

You might use micros instead, but even then the same risk applies. I'd suggest that you calculate only after a larger number of interrupts and store millis or micros when you do the calc, not in the interrupt routine.

Have you seen these steppers, specifically designed for automobile gauges.

They do not need a sophisticated driver and Adafruit have a good set of data on them nor a lot of current.

Googling x27 stepper arduino

Will give you many examples of how to use them.

Thanks.. Tom... :slight_smile:

Wildbill, I will try to make modifications as you suggested.
TomGeorge, I did look at these steppers but they are bigger and can't fit it in if the trip meter and odometer scrolls are in place and driven with a second motor. Still have not let any smoke out so should be working, right?

Thanks for the replies.