Direction Control W/ Optical Encoder

Hello,

I want to preface this question with the fact that I’ve used Arduino before but never for the development of motor control, so apologies if this is an easy solution. Hoping this post will point me in the right direction.

With this project I am working on making a motor change direction using an optical encoder. The motor is going to move the same speed, I just want to change direction. So far, I’ve hooked the encoder’s 5VDC and Common to the board as well as it’s positive and negative feedback to the Arduino. My understanding is that I need to feed the encoder’s feedback into the analog inputs. I am getting lost mostly on the sketch side of it. How to I read the encoder feedback and translate that to motor movement in the direction I’d like?

Further down the road once this part of the project is developed, I am going to incorporate lower and upper limit switches.

Thanks,

Patrick

Where did You pick up that strategy? It sounds wrong.
Posting a link to the encoder datasheet would help a lot.

Hi Railroader,

I can't seem to find the forum thread where that was mentioned, I could have misinterpreted it. Before I saw that I was feeding it in through the digital inputs.

The encoder data sheet is here.

Thanks,

Patrick

Thanks!
It's a 4 quadrant encoder. Usually 2 interupt capable pins, usually digital, are used, as I remember.
There's a library, Encoder.h that's usually used.

I find that this is one of the libraries that does it correctly with a state machine.

Rotary Library
from Encoder Library, for Measuring Quadarature Encoded Position or Rotation Signals

2 Likes

Please post a link to the motor driver board you are using, and a diagram showing how it is powered and wired. Hand drawn is preferred.

The motor should stop completely before it can change direction. Otherwise, you may burn out the motor driver.

Hi Railroader,

Thank you very much I downloaded the library and included it in the code!

Patrick

Hi Jremington,

I will draw out how I have it powered and wired up. I am using the Motor Sheild 3 and supplying power to the Motor Sheild via an external DC power supply. I've had the motor running with a sample code I've found online.

Thanks,

Patrick

Edit: Is there another motor driver that I should try as an alternative to the Motor Sheild that you recommend?

Here is the rough schamatic of how I have the encoder wired in, I included the motor power.

Thanks for posting the wiring diagram. It looks fine.

You can use the Arduino Encoder library to read the shaft speed and shaft position relative to startup.

To run the motor at a certain speed, regardless of load, most people use PID motor control.

The Arduino Motor Shield Rev3 uses the ancient L298 motor driver IC, which can handle only 1 Ampere per channel and is very inefficient. If your motor draws more current than that, a modern motor driver, like those from Pololu, would be a better choice.

Hi, thanks for checking that out for me! Upon further tinkering it turns out the encoder I was trying to use wasn't any good. Swapped it out for the same kind and it's working. I was able to read the feedback from it in the serial monitor. I also imported the Encoder library.

Got it on the PID motor controls, I was thinking about using the line "analogWrite(pwmPin, 120". That seemed to work not sure if it will work for my application with the next level of coding needed.

The motor right now is running at the speed I'd like and it's drawing well below 1 Ampere, so that should be ok for now. I also only need 1 motor for this project. But I will certainty investigate the Pololu drives if I run into any issues.

Next step is to integrate the motor output with the encoder for control of the movement direction. I was thinking if then statements to reflect the status of both encoder input channels. Thoughts?

Thanks again for all the guidance,

Patrick

Next step is to integrate the motor output with the encoder for control of the movement direction. I was thinking if then statements to reflect the status of both encoder input channels

Sorry, I have no idea what you mean by that. The motor driver controls the direction of rotation, depending on how your code sets the motor driver inputs.

What information are you expecting to get from the encoder, and how do you plan to use that?

My apologies, the end result of the project is to be able to use the encoder pot to adjust the direction the motor is moving, to move an arm up and down at a fixed speed. So, from my understanding the code is responsible for change the polarity to allow the motor to run in the opposite direction?

What is that?

The motor driver determines the direction of motor rotation, unless you force the motor shaft to turn backwards by some other means, which will destroy the motor and possibly the driver.

We seem to have trouble communicating. Given a motor driver, a motor and motor power supply, the first step is to learn the Arduino code to control the motor speed and direction of rotation. Forget the encoder for the time being.

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