Go Down

Topic: Having Trouble with Rotary Encoder (Read 695 times) previous topic - next topic

insert

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?


insert

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: [Select]
#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);
}

MarkT

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...]
[ I won't respond to messages, use the forum please ]

insert

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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy