Dc motor position control confusion


I'm a novice at writing code, so please be gentle with me.

I have a project that I need to do, and before I start spending hours going round in circles, I thought I'd ask for help.

I've been looking all over and can only seem to find examples for dc motor speed control, which all look too complicated for what I require.

So far, I can control the 12v DC motor in both directions using a h bridge. The bit that is confusing me, the motor has an encoder that generates a 0v to 5v squarewave.

What my aim is, is to first move the motor to its endstop then move it to it's other endstop and counter the number of pulses from the encoder then move the motor to it's mid position and store the positions.

After this has been done my aim is to control the motor position via a potentiometer.


If you think DC motor speed control is too complicated then you're not ready for this project. DC motor position control really requires DC motor speed control (or at least keeping the motor at a pretty low speed whether electronically or through gearing) in order to work. It sounds simple, but in all but really trivial cases, it really isn't.

It may be easier to do what you require with a stepper motor.

Even if you count the square waves from the encoder how will you know that you have reached the end stop when you don't know where you started ? The encoder is only any use if you know where you are when you start counting and how many pulses there are per unit of length

I have to use the dc motor I have as it is part of a small gearbox which is what i want to control.

My thought was that if the arduino is looking for the pulses, then if it didn't see any pulses within a set period of time it stops the motor.

That sounds like a good idea but it is not a good idea to deliberately stall the motor as then it will take much more current than when running

A better idea would be to use limit switches to stop the motor at each end stop

UKHelibob putting limit switches in is not an option here.

I just have to ask, how strong are the gears? Will they strip if an end stop is hit? Will the end stop be able to stop the motor given the gear reduction? Can you time the encoder so it will be between counts when it hits the end stop? The reason for that question is to eliminate the possibility of having one more count going one way that going the other way.

The gears are strong enough. Normally the motor is controlled via an ecu which has decided to release a lot of smoke.

There is a possibility that when the motor stalls that it could be in-between pulses

Ok. In a nutshell, the difficulty of doing this really depends on how quickly the motor (with load attached) will stop when you tell it to. A motor/load combination that is heavily geared down and has plenty of torque to control the inertia will be easy to work with. It also depends on how much overshoot you are happy with.

Here's a quick test. Connect the motor in the system you have to a power supply and a switch. Let it run at normal speed then flip the switch off. If the load stops where you want it to stop within your allowable position error, then you will probably be able to get this working with an arduino quickly.

If it doesn't then you need to start doing some research into feedback motor controllers. here's a basic article, or buy one off the shelf.

This is the motor and gearbox. The motor to gearbox ratio is 1125:1

When I power the motor, then turn off the motor stops straight away

OK. Try using the Arduino Encoder library to read position. I haven't used it but the examples look pretty simple.

Looking at the encoder examples it looks for 2 pulses from the encoder on A and B.

My motor only pickup. There is a IR led which is powered all the time and the IR sensor which sends out the 0v to 5v pulse.

How would the encoder library work for a single signal?

It won't. What you have is not an encoder: it's a revolution sensor!

You can probably do what you want with what you have. Set up the arduino with an interrupt service routine and in that routine count up if you're going one direction and down if you're going the other way. That will tell you where the motor shaft is. That will probably work well enough for what you're trying to do.

I've got the control of the motor sorted using a l298n board. But I'm stuck now.

I've never used interrupts before.