# Controlling a Stepper motor with IMU

Hi, so I am fairly new to Arduino and I have a project where I need to write code by taking in inputs from an MPU9250 (IMU) and using that data to control a stepper motor. For right now I am interested in controlling motion in the x-axis.

I have done some digging around for code and anything that could help me but have come up short. My stepper motor has 1.8 step angle so 200 steps per revolution. I am not sure how to convert the data from the MPU 9250 to degrees and make it readable for the stepper motor. I am using an L293D motor driver. I would like to use both the gyroscopic data and the data from the accelerometer to have a more accurate reading so the movements of the stepper motor are more precise.

Thanks in advance.

You have not told us whether you have successfully written short programs to make the stepper motor work and separately to get data from the MPU9250. If not that is an essential first step IMHO.

A L293 is a poor choice for driving a stepper motor.

Have a look at these links

...R

lnfin:
Hi, so I am fairly new to Arduino and I have a project where I need to write code by taking in inputs from an MPU9250 (IMU) and using that data to control a stepper motor. For right now I am interested in controlling motion in the x-axis.

I have done some digging around for code and anything that could help me but have come up short. My stepper motor has 1.8 step angle so 200 steps per revolution. I am not sure how to convert the data from the MPU 9250 to degrees and make it readable for the stepper motor. I am using an L293D motor driver. I would like to use both the gyroscopic data and the data from the accelerometer to have a more accurate reading so the movements of the stepper motor are more precise.

Thanks in advance.

So, if you have 200 steps per rev and one step = 1 degree and the MPU 9250 puts out a X axis angle of 1 degree you do not have the basis to derive how many steps to drive the stepper motor in offset to come back to level on the x axis? Your going to fail.

To use both gyro and accelerometer data you will need to have some type of filter, you can enter into your favorite search engine some words like " mpu9250 filter data" and get some type of response that should allow that part of the journey to begin. I'd say that the filter you use will be dependent on the hardware you got. For instance a Uno will be hard pressed to derive Quaternions and Euler angles and stepper motor drive torques at 1000 times a sec on the other hand a complementary filter 5ish times a second might do the trick.

Good luck

Also, consider the servo motor.

A typical servo motor written to in uSeconds will move from 0 to 180 in a micro second range of 500uSec to 2500uSec. That range of 180 degrees in 200uSec give you a step of 5.54uS per degree. Actually 5.55 but 5.55 adds up to greater then 180 degrees.

To go one degree from 0 degree to 1 degree is a 5.54 micro second increment from 500uS. As you may be able to see one can get partial degrees; 501, 502, 503, 504uSec, with a servo.

If a overall angle limit is placed upon the servo at servo initialization time then a greater granularity of servo torque values may be had. For instance a servo limit of:
#define MINservo 1349 // 45 degrees
#define MAXservo 1747 // 135 degrees
Those are approximate values.

I still get 2000uSeconds of torque range but now that the min and max angle are limited this works out to: float TourqeAngle_uSec = 11.11f;. Which increases the granularity of servo torque.

If you decide to use servo's, do not use plastic geared servo which will wear out in a week of continuous operation. Use metal geared servo's instead.

Robin2:
You have not told us whether you have successfully written short programs to make the stepper motor work and separately to get data from the MPU9250. If not that is an essential first step IMHO.

I checked the wiring and have used the example code from Arduino (Stepper_speed control) I'm using a 6 wire Stepper Motor. It's small due to the specifications I have for my project. I have also written code that displays the data from the MPU9250. Should I link them both?

Should I link them both?

No. If you know that you are getting meaningful data from the IMU, and can make the stepper step 100 steps, then you can easily determine how many steps to step to move the shaft n degrees, and you know how many degrees to move, so writing your real program will be trivial.

PaulS:
No. If you know that you are getting meaningful data from the IMU, and can make the stepper step 100 steps, then you can easily determine how many steps to step to move the shaft n degrees, and you know how many degrees to move, so writing your real program will be trivial.

Why would I need to set the motor to 100 steps instead of 200? And also could you give me some pointers on how to use the data from the sensor to move the motor relative to the x-axis?

Why would I need to set the motor to 100 steps instead of 200?

I didn't way that you would. I said that if you can make the motor step at all, and you can control how many steps the motor takes, and if you know how many steps it needs to take, you know everything you need to make the motor step the required number of times.

And also could you give me some pointers on how to use the data from the sensor to move the motor relative to the x-axis?

Relative to the x axis of what? If you mean that you want the stepper to step as the IMU is rotated about its x axis, then you need to understand exactly what data you get from the IMU. Without seeing your code for getting data from the IMU, I can't tell you how to use that raw data to do what you want. Not that I even understand what relationship you expect between the stepper motor and some value(s) from the IMU.

lnfin:
Should I link them both?

You should use the knowledge and experience you have gained from the two separate programs to make another program that combines the activities in the way you want.

Write a function for the IMU that produces a number in a variable.
Write a function for the stepper motor that uses that number to move the motor an appropriate number of steps.