problem with pid output range

Hi every one
I am having a little problem with a PID code which I tried to implement.
I am trying to control a motor with encoder when my goal is to rotate the motor for 4000 points of the encoder, I want that the movement of the motor during is work will be under pid control, by controlling the value of the pid (kip, ki, kd) I will be able to control the overall time of the Sycle.
My problem is that when I run the code it works ok but the output of the PID to the motor is either 0 or 255 which mean on /off control.
I would be very happy if someone could explain me how I change the output so it will be a range between 0 to 255.
Thank you in advance
eyal rabin

If we could see your code it would help us to tell you where it needs altering.

I also note that you have started two topics on the same theme, this is frowned upon on this forum.

Are you by any chance using "digitalWrite()" where you should be using "analogWrite() ?

Hi,
From some reason when I tried to use the analog write instead of digital write my encoder is getting crazy
I am attaching my code for help in understand how to make it works
Thank you in advance

#include <PID_v1.h>

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Define the aggressive and conservative Tuning Parameters
double Kp=533, Ki=0, Kd=480;

//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

#include <Encoder.h>
Encoder myEnc(2, 3);
const int pinMotorA = 5;
const int pinMotorB = 6;
int pb = 12;
int valorPot;
int zero;

void setup()
{

//turn the PID on
myPID.SetMode(AUTOMATIC);
pinMode(pinMotorA, OUTPUT);
pinMode(pinMotorB, OUTPUT);
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
pinMode(pb, INPUT);
digitalWrite(pinMotorA, LOW);
digitalWrite(pinMotorB, LOW);
}
long oldPosition = -999;

void loop(){
zero=4000;
Setpoint = 4000;

up:
int pbstate = digitalRead (pb);

while (pbstate == LOW ){
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
if (newPosition<zero)
{
Input = newPosition;

double gap = abs(Setpoint-Input); //distance away from setpoint

//we're far from setpoint, use aggressive tuning parameters
myPID.SetTunings(Kp, Ki, Kd);
myPID.Compute();
digitalWrite(5,Output);
Serial.println(Output);
digitalWrite(pinMotorB, LOW);
}
else {
digitalWrite(pinMotorA, LOW);
digitalWrite(pinMotorB, LOW);
digitalWrite(pb, HIGH);
zero=(zero+4000);
Setpoint =(Setpoint+4000);

goto up;
}
}
}

Hi,

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What model arduino are you using?

Thanks.. Tom.... :slight_smile:

Hi
my Connections goes this way

from the arduino ono:
pin 12-pb
pins 5,6 - input 1 and 2 of the driver
pins 2,3 - from output of the encoder

driver:

pins (1,8)enable and vs goes to +
pins (2,7) from arduino uno
pins (3,6) outputs to the motor
pins(4,5) to gnd

motor with encoder (pololu 50:1 Metal Gearmotor 37Dx54L)
red=driver
black=driver
green= gnd
blue=vcc
yellow= output to arduino
white= output to arduino

please help me find the problem in the code/

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Do you have pull up or pull down resistor in the input connected to your press button, when the button is OFF, you cannot leave the input pin of the controller open, it must have a 10K resistor pulling it high or low, or the pullup resistor parameter in the controller turned ON.
What model is the Arduino and what IDE version are you using?
What are the specs on your encoder and does it require pull up resistors, a link to spec sheet will help.
Also a picture of your project will help us see how your motor and encoder are connected.

Thanks.. Tom.... :slight_smile: