Pages: [1]   Go Down
Author Topic: Having Trouble with Rotary Encoder  (Read 614 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

I am trying to setup a PID of a DC Motor, with a rotary encoder. The encoder I am using is this one: http://www.robotshop.com/productinfo.aspx?pc=RB-Cyt-39&lang=en-US

For the life of me I can't get the Arduino to read the code and then use it to update the PID, I am starting out with a very low speed since the encoder has a low resolution.

With using the Basic PID Example(http://arduino.cc/playground/Code/PIDLibaryBasicExample) , is anyone able to setup the code so I have a starting point. I can control the DC Motor just fine, but adding the encoder and PID I am lost.

Many Thanks in advance.


Would it be easier to control the speed of a servo, that I have tapped a feedback wire into?

Logged

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

Here is what I have so far.  Also something I noticed is the encoder has a LED on it that blinks, when the light is broken, and when I have it connected the light constantly stays light even when the light is broken.

Code:
#include <PID_v1.h>
#define DIRA 12 // Direction                 
#define PWMA 3  //Motor Connected to Pin 3
#define FB 0 // Encoder Feedback to Pin 0

double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);

void setup()
{
  pinMode(DIRA, OUTPUT);
  pinMode(PWMA, OUTPUT);
  pinMode(FB, INPUT);
  //initialize the variables we're linked to
  Input = analogRead(0);
  Setpoint = 200;

  //turn the PID on
  myPID.SetMode(AUTOMATIC);
}

void loop()
{
  Input = analogRead(0);
  myPID.Compute();
  analogWrite(PWMA, Output);
  digitalWrite(DIRA, LOW);
}
Logged

0
Offline Offline
Shannon Member
****
Karma: 215
Posts: 12443
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well the user manual says the LED should go out if the beam is broken - beware of ambient light though, check it in a darker environment perhaps?  If the LED doesn't blink it indicates the unit is defective.

[edit: one quick thought - check you're not accidentally shorting the output signal...]
Logged

[ I won't respond to messages, use the forum please ]

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

I did a check with one of the basic encoder sample codes and it worked fine, it would blink each time and it would count each time the light was broken.  SO I know the encoder is not defective.
Logged

Pages: [1]   Go Up
Jump to: