DC motor spped control (Fan) PWM PID

Hello,

I'm working on a DC motor speed project using ATmega168 on (Freeduino SB) and Arduino software.

Simply the user can spacify the required temperature + sensor feedback of the current temperature (analog inputs ) to Arduino, to run the fan in a suitable speed.

I have ordered the hardware, though I am eorking on the code but it looks like a big mess :-?.

The thing is I have been browsing the forum in two days and I got confused of the different solution to manipulate the DC motor speed.

The Big Question is , is this a right code? remember I don't have the hardware yet.

I just can't believe it! is it that simple! ::slight_smile:

// DC motor

#include <PID_Beta6.h>

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

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

void setup()
{
  //initialize the variables we're linked to
  Input = analogRead(0); //read from sensor (LM35D)
  Setpoint = analogRead(1); //knob 0-5 V

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

void loop()
{
  Input = analogRead(0);
  Setpoint = analogRead(1);
  
  myPID.Compute();
  analogWrite(3,Output); //DC motor (fan) 0-5V
}

Thanks in advance

Why is this a poll? There are no appropriate choices on the poll.

Well, no, but I am a new user here and I just wanted to post my question, and I couldn't without doing that poll things!, sorry about that !
So, what do you think tiger, is it that simple?

When you want to create a new thread, these are your choices:

Start new topic | Create Poll | Notify of replies | Mark Topics as Read

That first one was the one you wanted to use.

The question that you need to ask yourself is whether the speed of the fan is supposed to affect the sensor. That is, are you trying to maintain the temperature that the sensor reads, or not.

If the sensor is reading the temperature in the room, and the fan is supposed to come on when the temperature gets above a set-point, and go off when the temperature drops below that set point, then PID control is overkill.

If you have both a heater and a fan, and are trying to maintain a temperature in a very narrow and, then PID control is necessary.

You haven't given us enough information to tell you whether what you have written is required or adequate.

Some more comments would certainly be useful. For example, how is the PID library to take the input you have given it (2, 5, 1 and two values in the range of 0 to 1023) and map that to a value in the range of 0 to 255 to control the fan speed?

In particular, what do the 2, 5, and 1 values mean?

You might also want to document (in the comments) why you read the sensor values in setup(), when you don't use the values that you read.

One final thing to think about. The loop function is called in an endless loop. As soon as it ends, it is called again. This can happen many times a second. Depending on what happens in the function, the function can be called several million times per second. Is it necessary for the PID library to be trying to control the process that often? Or should you have some sort of delay, so that the temperature is only read like once a minute, and the fan speed adjusted accordingly?

Please don't get me wrong. I'm not trying to discourage you. You'd know that if were in the same room having this conversation, but it's difficult to have the same kind of conversation online.

Hi Pauls,
Thanks for the quick reply.

Basically, I have just copied the code from the PID example, and I thought these values (2,51,) something standard in the pid beta.

However, it might be an overkill as you said by using the pid in such a simple project, but I have to use it in my project.

The project as you described :

the sensor is reading the temperature in the room, and the fan is supposed to come on when the temperature gets above a set-point, and go off when the temperature drops below that set point

.

I have just jumped in the Arduinos, and I need some help , thanks for the tips that you have given me, am trying to figure out how to manage.

many thanks

Btw, the setpoint is an analog input that I have to specify via a POT or simply a voltage 0-5V.

However, it might be an overkill as you said by using the pid in such a simple project, but I have to use it in my project.

Why? Is this some sort of school assignment?

It's fine if it is. But, if it is, it means that the project is more about learning about PID than it is to create a working project. In which case, blindly plugging values into a function call is not going to prove useful. Look at the function call, and understand what each of the parameters controls, and how it is used.

Actually it is a a school assignment, when it comes to the PID I have full understanding how it works (maths & graphic).

The mystery of the parameters inside PID myPID(&Input, &Output, &Setpoint,2,5,1); is not clear for the moment.

I've tried to find out what these parameters represent , but I found nothing.

However, I wrote a simple code to illustrate what I've understood from the Arduino usage.

A GOD father like you could laugh so much about it, but it's ok :-X . I just need a hand with the code.

So, please give me your rate how much would you laugh in a scale 1-10 ?

/* DC motor control 

*/

int sensor = 0; //sensor value
int sensorPin = 0; //sensor pin
int setPoint = 0; // required temperature value
int setPointPin = 1; //required temperature pin
int fanPWM_Pin = 3;
int error = 0;
int prev_error = 0;
int out = 0;
double P_Gain = 1, I_Gain = 0.3, D_Gain = 0.5; // PID Coefficients example

void setup()
{
  //initialize the variables we're linked to
  sensor = analogRead(sensorPin); //read from sensor (LM35D)
  setPoint = analogRead(setPointPin); //knob 0-5 V

 
}


//------------------------------------ Functions prototypes----------------------------
int pid(int error_ctrl, int prev_error_ctrl);

//----------------------------------- "Main" function ----------------------------------
void loop ()
{
 
 setPoint = analogRead(setPointPin); //read the required temp
 
 sensor = analogRead(sensorPin); //read the current temperature
 delay(200);
 
 error =  sensor - setPoint; // the difference of temperature
  
  if (error >= 1)  
             {
                          
                out = pid(error, prev_error);    // call pid function  
               delay(200);   
             }  
  analogWrite(fanPWM_Pin, out); // write to pin 3, to set the fan speed
                       
 prev_error = error; //save the old error
 error = 0; //reset error var.
 
}//End Main

//------------------------------------PID Function ----------------------
int pid(int error_ctrl, int prev_error_ctrl)
{
  double p = 0, i = 0, d = 0;
  int sum = 0;
  
  p = P_Gain * error;//Proportional part
  
  i= I_Gain * (error_ctrl + prev_error_ctrl); //integral part
  
  d = D_Gain * (error_ctrl - prev_error_ctrl); //derivative part
  
  sum = p + i +d; // PID

return sum;   
}

//Program End

Cheers

The mystery of the parameters inside PID myPID(&Input, &Output, &Setpoint,2,5,1); is not clear for the moment.

I've tried to find out what these parameters represent , but I found nothing.

You downloaded the PID library? A library on the Arduino is not an object file, like it would be on a PC. It consists of one or more header files and one or more source files. If the documentation is not adequate (and it rarely is), check out the header files and source code.

So, please give me your rate how much would you laugh in a scale 1-10 ?

I, and most others here, never laugh at beginner code. We were all beginners once. Some make an effort to learn. Those people don't get laughed at.

Some make no effort, and insist that the problem is in the documentation (that they haven't read), in the library they are using (incorrectly), or in the schematic (that they didn't understand or follow). Those people we laugh at.

Thanks for encouriging me :wink:

Infact I'm not really a begginer in programming, but I'm not so much in it, using the new built in fonctions is a bit confusing, as I said , am totaly new to the Arduino (2 days).

At this point I don't want to use the library anymore, I took your advice by implementing the PID myself.

My question was, what do you think about the new code, that I have posted recentl? Is it far from the right code?

I can't test it, the hardware are comming on Monday, and I have to submit the project in 2 weeks ::slight_smile:

Cheers

My question was, what do you think about the new code, that I have posted recentl? Is it far from the right code?

I have no idea. I've never created a device that relied on PID for accuracy, so I've never researched the topic, or tried to implement by own code.

This is the declaration of the function so those numbers where the P, I and D parameters:

PID(double *Input, double *Output, double *SetPoint, double P_Param, double I_Param, double D_Param)

Thanks, that was very helpfull.

Cheers mate