# Cypress Developer CommunityTM

## simple PID like algorithm

Summary: 22 Replies, Latest post by Bob Marlowe on 05 Feb 2016 03:03 AM PST
User
33 posts

Hi,

Ive been trying to use pwm to control a heater using feedback from a thermistor. Ive asked a similar topic before about implementing a PID controller but was wondering if anyone knows of a simpler way to decently regulate the output. currently all the code i have does is kind of use reduced PWM duty as the set point and measured gets nearer to each other. this either causes an overshoot or doesn't quite reach the set point.

thanks

User
9268 posts

Depending on the time the system uses to react your closed loop will either overshoot or converge asymptotic. Only by fine-tuning the PID parameters and by stabilizing (thermal Isolation) of the heater and / or forcibly stirring the heated liquid will help.

Bob

User
2551 posts

I can only recommend to follow the "improving the beginners PID" tutorial series. Its quite comprehensive - I learned PID from there, and its not exactly complicated (read the value you want to control, make some calculations, write the value to the peripheral that controls your setup).

The tricky part is in finding out the correct P,I, and D values. For tuning look at the Ziegler-Nichols-method (tl;dr: set I and D to 0, start increasing P until your control loop oscillates. With this frequency you then calculate P, I, and D).

User
33 posts

thanks for your quick replies folks. say i only use the the P term for initial testing and understanding then Output = kp * error

Another thing is how do i relate the output to a PWM duty cycle? using an 8 bit PWM with a range of 0-255

thank you guys

User
9268 posts

The function PWM_WriteCompare() will set the duty-cycle . Use a value between 1 and the programmed period value.

Bob

User
33 posts

Hi bob, The PWM_WriteCompare() function is being used to drive a h bridge, At the moment the value of between 0 and 255 was randomly chosen, How does the Proportional output alter the PWM duty cycle? would i not need to relate the output function with the proportional function to the PWM's duty cycle somehow as it should take into account he overshoot in temperature and compensate for it?

ive got a setpoint which is a temperature reading in degrees C and the feedback is from a temp sensor also in degree C.

Sorry if my question seems a bit redundant

thanks

User
9268 posts

Duty Cycle is Period / Compare *100, Quite easy.

The PID controller parameters (KP, KI, KD) are taking care of overshoots. Proceed as hli suggested, this is the only way.

Bob

User
33 posts

Hi Bob, thanks for the reply. Ive read the link hli sent to me, Its just i still have an issue with what you're saying. it makes a lot of sense. I know that 255 will equal 100% duty cycle and 128 will be 50%. Its the PID output function that i have an issue with. I know it can get the error term by reading the difference between set_temp and actual temp. but wont  the PID function I called output need to be multiplied or somehow control the duty cycle in the PWM_writeCompare() function?

I have the PID output which is currently just the error*Kp, but doesnt the output function need to be in the WPM_writecompare() function? the value i set in the write compare function directly controls the duty cycle and therefore the heater.

ive been doing something like this PWM_WriteCompare(192), roughly 75% Duty cycle. I have two PWM_WriteCompare functions because the two PWM outputs are connected to IN1 and IN2 of the Hbridge im using. I can reverse the direction by giving a duty cycle value to one write compare function and have the other PWM write compare function tied to ground.

The two PWM_writeCompre functions are my only outputs.

If have simple if statement that i know causes an overshoot.

its simply

if(set_point>actual_temp)  //cooling direction

{

PWM_writeCompare1(192); //etc

PWM_writeComopare(0); //it tied to ground

}

then to heat the heater/peltier i do the opposite with the write compare.

How does the PID output function interact with the PWM_writeCompare function?

Sorry for posting a silly question, thanks for all your help so far i greatly appreciate it.

User
9268 posts

The PWM has two outputs which can be programmed with a dead-band suitable for h-bridge. Because the characteristics of the peltier are different for heating and cooling you may need two different PIDs connected to two PWMs. You may select which one is active using a MUX.

wont  the PID function I called output need to be multiplied or somehow control the duty cycle in the PWM_writeCompare() function?  This the KP performs. You might use a limiting function for duty cycle >= 0 and <= 100%

Bob

User
33 posts

Ok,

So would it be something like this?  For the moment i only have the Proportional and i will put limits on the PWM as well as implement the Intergral and derivative functionalites.

Output = kp * error;
PWM_WriteCompare1(00000);
PWM_WriteCompare2(Output);

thanks very much for your help

User
9268 posts

Something like that. Depends mostly on the PWM's configuration.

For a PID look here

Bob