DC Motor With Encoder

Hi

Urgent Help Needed. I am using DC Motor with Encoder. The Encoder gives 64counts /rev. I am Using Arduino Mega, SN754410 Driver IC and the Pololu Motor.

I am new to Arduino. I need to program the Arduino In a such a way that it control's the position of the Motor. Say for Example to move the motor for 120 counts.

What Shall I do. I don't get the programming part.

However I have done the encoder.h one where I get the values displayed on the serial monitor.

Thanks

Regards
Kunal

We need to know more about the encoder.

Please post a link that tells us how it is wired.

Weedpharma

Kunal04:
However I have done the encoder.h one where I get the values displayed on the serial monitor.

If you can get the values already, then you can now use them to control your motor(s). Start the motors, check for the desired position, when the position is reached, stop the motor.

Or more sophisticated, compute the error (difference between desired and measured position), and use that
(via a PID loop perhaps) to set the motor direction and PWM level. This is closed loop position control.

Hi
Below is the link with data from Pololu.

Pololu - 19:1 Metal Gearmotor 37Dx52L mm 12V with 64 CPR Encoder (No End Cap)
Thanks

Do you have any programming experience? If not, you need to learn the basics then add in extras like reading the sensor outputs.

Weedpharma

Okay thanks.

I shall get something going up running in some days time. I will ask questions then by posting up my program. However One question. Can I overwrite the arduino library in my code.?

The library resides on the hard drive and is read (not written) by the compiler. So you cannot overwrite by writing a program.

Weedpharma

Thank you Very Much.

So I tried running the motor by including the encoder.h in the program, How ever I was not able to stop my motor at 1200 counts. Help??

#include <Encoder.h>

#define InA1 11
#define InB1 12
#define PWM1 13

Encoder myEnc(5, 6);

void setup() {
Serial.begin(9600);
Serial.println("Welcome to EE399 Project AS/RS System:");
pinMode(InA1, OUTPUT);
pinMode(InB1, OUTPUT);
pinMode(PWM1, OUTPUT);

analogWrite(PWM1,125);
digitalWrite(11,HIGH);
digitalWrite(12,LOW);
}
long oldPosition =0 ;

void loop() {
long newPosition = myEnc.read();
if (newPosition = 1200) {

analogWrite(PWM1,0);

}

}

Please put your code in its own window as seen in other posts. This can be done by placing     [code]  and [/code]  around the code or use the </> icon. This makes it easier for others to read.

How to use this forum

Where are you actually counting the pulses?

Why use InA1 as a name for an output? Surely out1 would be more meaningful?

Weedpharma

Where is your wiring diagram?

Without correct wiring it will not work. We need to see the wiring to see if it correct.

Weedpharma

Okay Here is My code. I want to on the motor for certain number of counts

#include <Encoder.h>

#define InA1            11                     
#define InB1            12                     
#define PWM1          13                       

EncodermyEnc(5, 6);
 
void setup() { 
    Serial.begin(9600);
    Serial.println("Welcome to EE399 Project AS/RS System:");
     pinMode(InA1, OUTPUT);
     pinMode(InB1, OUTPUT);
     pinMode(PWM1, OUTPUT);
     
     analogWrite(PWM1,125);
     digitalWrite(11,HIGH);
     digitalWrite(12,LOW);
}
long oldPosition  =0 ;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition = 1200) {
   
    analogWrite(PWM1,0);
   
  }

}

I have the Pololu Motor that I am using,It has encoder on it's back.
My circuit is connected somehow like this.

The two motor pins are connected to the driver IC.

PWM1 is connected to pin 13.
Motor Pins are connected to 11,12.
Encoder A and B are connected to 5 and 6.

I cannot see any wiring diagram in your post.

Weedpharma

void loop() {
  long newPosition = myEnc.read();
  if (newPosition = 1200) {
    
    analogWrite(PWM1,0);
    
  } 

}

Firstly you use asignment '=' instead of equality '==', mistake one.

Secondly you assume the position can't overshoot - dodgy assumption, use a greater-or-equal
test instead:

  if (newPosition >= 1200) {
    ...

okay,thanks.

please find attached sketch of my circuit.

"EncodermyEnc(5, 6);" in your supplied code

Maybe try separating into Encoder myEnc(5,6);

If this is correct in your code, why supply code that is not being used?

In your diagram, please use D5 and D6 rather than pin 5 and 6, for clarity.

As you have not told us, can we assume the encoder supply and ground are connected and the grounds are all connected?

Weedpharma

Hi yes they are connected properly.

Okay I have now got the coding for the encoder Read, Now my next task is to run the motor in one direction and lets say count to 1200 counts. I am using Quadrature encoder.

Here is code, Help Needed.

int encoder0PinA = 2;
int encoder0PinB = 3;
volatile int encoder0Pos = 0;
volatile int encoder0PinALast = LOW;
volatile int n = LOW;
int valNew = 0;
int valOld = 0;
volatile int m = LOW;

void setup()
{
  pinMode (encoder0PinA,INPUT); 
  pinMode (encoder0PinB,INPUT);
  Serial.begin (9600);
  attachInterrupt(1, CountA, CHANGE);
  attachInterrupt(0, StateB, FALLING);
}

void loop()
{
  encoder0PinALast = n;
  valNew = encoder0Pos;
  if (valNew != valOld) {
    Serial.print (encoder0Pos, DEC); 
    //Serial.print (m);
    Serial.print ("/");
    valOld = valNew;
  }

}

void CountA()
{
  n = digitalRead(encoder0PinA); 
  if ((encoder0PinALast == LOW) && (n == HIGH)) { 
    if (m == LOW) { 
      encoder0Pos--; 
    } 
    else { 
      encoder0Pos++; 
    } 
  }
}
void StateB()
{
  m = digitalRead(encoder0PinB);
}