# How to convert a DC motor into a servo motor using a rotary encoder and a microcontroller?

Operating/ Rated Voltage: 24V

Max efficiency: 1.4 Kg-cm/300 rpm/ 14.2W/ 0.87A

Max power: 4.5 Kg-cm/180 rpm/28.2W/1.4A

Stall Current: 2.9 A (max)

Stall/ max Torque: 8 Kg-cm

Gear Ratio: 1:34

1496 pulses per revolution of output shaft and 44 pulses per revolution of motor shaft

coil resistance : 30 ohms.

How can I use these data to make a model of the dc motor? How can I use that model to make this motor into a servo motor using microcontroller like the arduino.

What are the topics I need to study.

Things I have done so far -

Read about transfer function of electro mechanical systems and tried to formulate a transfer function. But there are some things I couldn't figure out- like the inductance of the armature and the exact number of gear teeth in the gear, the inertia of the armature and some other stuff as well.

Things I plan to do -

I found out about MATLAB's system identification toolbox and black box modeling, Which doesn't require complete model of the system.

So I plan to use that toolbox to estimate transfer function of the dc motor. In order to collect data I will be using an arduino with pyserial to log voltage and current input to the motor, motor shaft position and time.

Later I intend to use Z transform to convert the inverse of the transfer function into discrete time domain from continuous time domain.

For the next step I am planning to make a controller which will combine a feedforward (Using the inverse transfer function) and a Feedback (PID based) controller together.

Is there any guide/example/tutorials available on this topic?

Am I heading in the right direction? or am I missing something.

TIA.

• Hi, welcome to engineering. Probably this question can be better answered in electrical Engineering Stack Exchange. Dec 30, 2020 at 12:30
• It might be helpful to share what you have done so far. The response provide below is general. With additional detail it is possible to receive a focused response. Dec 30, 2020 at 13:30
• @Samm Flynn, looking good. Are you vaguely familiar with the dynamics of motors? voltage, impedance, current, magnetization, back-EMF, etc... that can catch some people off guard, as their system has higher order than they expected. The fact that you plan to use an encoder is excellent. Adding the intermediate step of measuring and controlling current and not just voltage may be helpful, but it depends on the details. "Am I heading in the right direction?" yes! Jan 11, 2021 at 23:55
• Also, if all you need is a PID then modeling the system may not be all that productive, especially if you already have or can easily build the system. Jan 12, 2021 at 16:24
• Does this answer your question? youtu.be/3pYWLF8qw-g Jan 16, 2021 at 10:54

At the very high level you will need

• 24V Power supply or a method to generate 24V
• 24V Motor controller
• Microcontroller - Arduino is a good place to start

There are also prebuild motor controllers that can be programed via computer. These tend to be expensive. I would suggest following web sites similar to the ones listed below. They tend to have blogs, videos and white paper that can provide guidance.

You can use a LCR meter to measure the inductance of the coil (armature). These value can be found in datasheets too.

Using positions sensors is an alternative to using rotary encoders. The application will determine the appropriate choice. Since a micro-controller is used consider PWM (Pulse width Modulation) to drive the actuator. This can help keep track of distance travel.

References

• @SammFlynn, thank you for updating the the questions. As others have suggested you are on the right track. Feel free to stop by with additional questions. :-) Jan 13, 2021 at 15:23

You are at the right track. As the DC motor is rather fast for a potential slow microcontroller, using a discrete controller will improve the reliability and stability of the closed-loop system. Even though a DC motor is rather easy to model (speaking of the basic dynamics upto the 3rd order), using system identification can improve parameter estimation compared to the numbers given by the manufacturer (they might not be that accurate for DIY low cost DC motors).

However, you are forgetting one crucial part: the load. the dynamics of your DC motor can change critically depending on the load attached to it. Increasing the inertia alone will increase the current, and might reduce overall acceleration. If the mass is offset, the system needs more power at certain angles. Depending on the load's dynamics, this can yield unwanted (anti-)resonance frequencies which can destabilize the system.

This means you can go in 2 directions: you either make the controller super robust, such that it can handle any load within a certain range of dynamics. This will reduce performance in the means of controller aggressiveness (aka it becomes slower). Alternatively, you could also include the designated load into the model and create a controller using that. With this, you develop the most suitable controller, but its limited to only minor deviations to the designated load.

Adding to the other answers. I just so happen to have done exactly this. I used a windshield wiper motor and a potentiometer but the principal is the same.

Here's my arduino source code: https://pastebin.com/0ezsmi4y

And a short video I took of it in action. This is an alternate version that takes RC PWM input instead of serial.

I think all the talk about fancy feedback loops is putting the cart before the horse. It's been years since I wrote this, but apparently I just used bang bang control with hysteresis and I remember it worked fine. That's often true with high gear reduction mechanisms. Once you cut the power they basically just stop, so you don't need much integral or derivative action.

Start with that, or basic proportional control, and then when you've got the servo moving around you can worry about getting the loop dialed in.

Here's the hardware I used for reference:

Maker Motor PN01007-38

Cytron MD10C Motor Controller from Robot Shop

Arduino Uno

Some miscellaneous 10k pot I had lying around