X27 stepper motor

Hello, I want to make a km/h speed circuit made with an x27 stepper motor. Can you help me with this for my vehicle? There are codes in mph in the resources on the internet, I could not find an example in km/h.

Hi, @conk83
Welcome to the forum.

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

You should be able to add some conversion code lines.

Can you please tell us your electronics, programming, arduino, hardware experience?

Have you tried the code you have, if not, then please do to make sure you can control the x27 stepper.
Starting with a breadboard experiment would be the best place to start.

Please post the code if it works on your x27.

Tom... :grinning: :+1: :coffee: :australia:

1 Like

Start with any example that seems suitable and get a prototype working. The calibration to km/hr can be done later.

1 Like

No, not from this post. Read reply #2 seriously and use it.

1 Like
[code]

 #include "SwitecX25.h"

 
 #include "FreqMeasure.h"
 
const int UpdateInterval = 100; 
const double StepsPerDegree = 3.0;  
 const unsigned int MaxMotorRotation = 315; 
 const unsigned int MaxMotorSteps = MaxMotorRotation * StepsPerDegree;
 const double PulsesPerMile = 5764.0; 
 const double SecondsPerHour = 3600.0;
 
 
 const double SpeedoDegreesPerMPH = 180.0 / 87.0;  
 
 unsigned long PreviousMillis = 0;   
 double MinMotorStep;  
 
 SwitecX25 Motor(MaxMotorSteps, 4,5,6,7);
 void setup(void) 
 {
 
  pinMode(8, INPUT_PULLUP);
 
  Motor.zero(); 
  Motor.setPosition(744);


  
  MinMotorStep = PulseToStep(2 * (UpdateInterval / 1000.0) * F_CPU);
 
  FreqMeasure.begin(); // Start freqmeasure library
 }
 
 double sum=0;
 int count=0;
 double avgPulseLength=0;
 unsigned int motorStep = 0;
 int noInputCount = 0;
 
 void loop() {
  unsigned long currentMillis = millis();
 
  // Update the motor position every UpdateInterval milliseconds
  if (currentMillis - PreviousMillis >= UpdateInterval) {
   PreviousMillis = currentMillis;   
    count = 0;
    sum = 0;
 
    // Read all the pulses available so we can average them
    while (FreqMeasure.available()) {
      sum += FreqMeasure.read();
      count++;
    }
     if (count) {
     
    avgPulseLength = sum / count;
      motorStep = PulseToStep(avgPulseLength);
      noInputCount = 0;
    } 
   else if (++noInputCount == 2)  // force speed to zero after two missed intervals
     motorStep = 0;

    // Ignore speeds below the the two missed intervals speed so the motor doesn't jump
    if (motorStep <= MinMotorStep) 
      motorStep = 0;
 
    Motor.setPosition(motorStep); 
  }
 
  
  Motor.update();
 }
 

 
 unsigned int PulseToStep(double pulseLength)
 {
  return (unsigned int)((F_CPU * SecondsPerHour * SpeedoDegreesPerMPH * StepsPerDegree) / (PulsesPerMile * pulseLength));
 }
[/code]


I have a code like this, I partially changed it to km/h by changing it from mph, but I encountered 2 problems. Firstly, it reaches the position in the form of a pause when turning the clock, it advances to the desired position without tasting what I want. Secondly, when the power is suddenly cut off, even if I set the final position to 0 degrees, it occurs in a sudden power cut. When I re-feed or reset, it continues from a position other than the current one, in short, it starts outside the current route. What changes can I make about these two problems? Thank you.

By the way, I'm sorry for the translation, I hope my problem is understandable.

#include "SwitecX25.h"
#include "FreqMeasure.h"




const int UpdateInterval = 1000;  // 100 milliseconds speedo update rate
const int UpdateInterval2 = 50;
const double StepsPerDegree = 3.0;  // Motor step is 1/3 of a degree of rotation
const unsigned int MaxMotorRotation = 315; // 315 max degrees of movement
const unsigned int MaxMotorSteps = MaxMotorRotation * StepsPerDegree;
const double PulsesPerMile = 100.0; // Number of input pulses per mile
const double SecondsPerHour = 560.0;
const double SpeedoDegreesPerMPH = 180.0 / 120.0; // Speed on face of dial at 180 degrees is 108mph.
unsigned long PreviousMillis = 0;   // last time we updated the speedo
unsigned long PreviousMillis2 = 0;
double MinMotorStep;  // lowest step that will be used - calculated from update interval
double sum=0;
int count=0;
double avgPulseLength=0;
unsigned int motorStep = 0;
int noInputCount = 0;
float mph=0;

SwitecX25 Motor(MaxMotorSteps, 4,5,6,7); // Create the motor object with the maximum steps allowed





void setup(void) 
{

  
 



  Motor.zero(); //Initialize stepper at 0 location
  Motor.setPosition(744);
  Motor.updateBlocking();
 Motor.setPosition(0);  //0MPH
 Motor.updateBlocking();



  MinMotorStep = PulseToStep(2 * (UpdateInterval / 1000.0) * F_CPU); //Force to zero when two intervals have passed with input

  FreqMeasure.begin(); 

}

void loop() { 
 
  unsigned long currentMillis = millis();

  
  if (currentMillis - PreviousMillis >= UpdateInterval) {
    PreviousMillis = currentMillis;   
    count = 0;
    sum = 0;

    
    while (FreqMeasure.available()) {
      sum += FreqMeasure.read();
      count++;
    }

    if (count) {
     
      avgPulseLength = sum / count;
      motorStep = PulseToStep(avgPulseLength);
      noInputCount = 0;
    } 
    else if (++noInputCount == 2)  
      motorStep = 0;

  
    if (motorStep <= MinMotorStep) 
      motorStep = 0;

    Motor.setPosition(motorStep);
    
  }

  Motor.update();

  unsigned long currentMillis2 = millis();


  if (currentMillis2 - PreviousMillis2 >= UpdateInterval2) {
    PreviousMillis2 = currentMillis2; 

   mph = ((motorStep / 3)/SpeedoDegreesPerMPH); // Might be a better way of calculating mph based on input frequency
   



  }




}

unsigned int PulseToStep(double pulseLength)
{
  return (unsigned int)((F_CPU * SecondsPerHour * SpeedoDegreesPerMPH * StepsPerDegree) / (PulsesPerMile * pulseLength));
}

This is my first time working with x27, so I want to get help from knowledgeable people like you.

Hi,
As you are not using a digital readout, use one of the MPH codes that works for you, then just use a KPH scale in the meter face.

If your meter code is 60MPH max meter deflection, then put 96KPH as max speed on your scale behind the needle.

Tom.. :grinning: :+1: :coffee: :australia:
PS, I should have thought of that ages ago.

I am reading in frequency, I will read the data with the hall sensor, but the problem here is that the x27's reactions are late and not smooth.

Try using a Tacho type code, it should be quicker.

Google;

arduino tacho speedo x27

It will show you the basics need for your code.

The frequency library would be optimised for accuracy not conversion speed.

Tom... :grinning: :+1: :coffee: :australia:

1 Like

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