Pages: [1]   Go Down
Author Topic: Problems with optical encoder and motor shield.  (Read 777 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everybody, i have a problem with optical encoder and the motor shield. When the two motors are stopped, the encoders function properly: making them rotate by hand, they count 20 steps per spin but when the motors are on, some interferences make much many steps be counted instead of their real number.
I paste the code but not the whole one because it’s too long. I don’t think there’s something wrong, but there could be something to better.
Code:
#define LEFT 0
#define RIGHT 1

//i pin usati per i due motori
int SX = 6;
int M1 = 7;
int DX = 5;
int M2 = 4;
//le due "velocità" dei motori
int velocita_sx = 100;                                       
int velocita_dx = 100;

//array per l'encoder
long coder[2] = {0,0};

void setup(){
  attachInterrupt(LEFT, LwheelSpeed, CHANGE);    //inizializzo il pin 2 per l'interrupt
  attachInterrupt(RIGHT, RwheelSpeed, CHANGE);   //inizializzo il pin 3 per l'interrupt
  pinMode(M1, OUTPUT);                           //pin per il controllo dei due motori
  pinMode(M2, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  Serial.print("Coder value: ");               //stampo a monitor il valore dell'array
  Serial.print(coder[LEFT]);
  Serial.print("[Left Wheel] ");
  Serial.print(coder[RIGHT]);
  Serial.println("[Right Wheel]");
  digitalWrite(M1,LOW);                     //per definire il senso di rotazione
  digitalWrite(M2, LOW);
  if(coder[LEFT] < 50 && coder[RIGHT] < 50){   //dovrebbe fare 50 passi dell'encoder e poi fermarsi
  analogWrite(SX, velocita_sx);     
  analogWrite(DX, velocita_dx);
  }
 
 
  if(coder[LEFT] >= 50 && coder[RIGHT] >= 50){
    analogWrite(DX, 0);
    analogWrite(SX, 0);
  }
}


//le due funzioni chiamate dall'interrupt per aumentare il contatore dei passi
void LwheelSpeed()
{
  coder[LEFT] ++; 
}
 
 
void RwheelSpeed()
{
  coder[RIGHT] ++;
}


Thanks for your help.
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6613
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It sounds like the PWM motor current is interfering with the signal from the encoders. What are you using to drive the motors, and how do you have the encoders connected?

You may need to use screened (shielded) cable to connect the encoders to the Arduino, and you should definitely ensure that the power and ground lines to the encoders are completely separate from the power and ground lines to the motors or motor drivers.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using this motor shield and there are two power lines, the first 9V for arduino which feeds a servomotor, a ultrasonic sensor and the two encoders, the second for motor shield and motors.

How can i screen the connection from encoders to Arduino?
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6613
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I suggest you use shielded cable to connect the encoders. Use the shield as the ground connection, connecting it to the ground side of the encoders (and nothing else) at the encoder end, and to one of the Arduino ground pins. Use a different Arduino ground pin to connect power and the servo to the Arduino.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Pages: [1]   Go Up
Jump to: