1 2 Previous Next 22 Replies Latest reply on Feb 5, 2016 3:03 AM by JoMe_264151

    simple PID like algorithm



      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.


      any advice guys?



        • 1. Re: simple PID like algorithm

          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.





          • 2. Re: simple PID like algorithm

            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).

            1 of 1 people found this helpful
            • 3. Re: simple PID like algorithm

              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

              • 4. Re: simple PID like algorithm

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





                • 5. Re: simple PID like algorithm

                  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



                  • 6. Re: simple PID like algorithm

                    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.





                    • 7. Re: simple PID like algorithm

                      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.

                      • 8. Re: simple PID like algorithm

                        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%





                        • 9. Re: simple PID like algorithm



                          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;




                          thanks very much for your help

                          • 10. Re: simple PID like algorithm

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


                            For a PID look here





                            • 11. Re: simple PID like algorithm

                              Ok thanks. my PWM config is 8 bit resolution, two PWM outputs, with deadbands for 2-4 clock cycles. a 100kHz clock giving a period of 2.56mSec. its also a less than type count. 


                              My duty cycle is between 0 and 255, Its just how will the output function error*Kp increment my duty cycle?


                              ive set KP initially to 1, and the error will presumably be set-point-actual_temp.


                              given a set point of 30c and a measured temp of 21C. the error will be 9, and the output will be equal to 9? wont that mean that the pwm duty cycle will be 9/255?   PWM_WriteCompare1(Output or 9/255)


                              Will this value only increase with the use of the full formula i.e the integral and derivative?


                              Sorry its the output function controlling the duty cycle im having issues with.


                              thanks for your assistance

                              • 12. Re: simple PID like algorithm

                                A pure P-controller always has got a (small) error.


                                An I-component will make sure that there is no long-term error adding up.


                                The D-component will allow for a short reaction time (Not usable for slow reaction-times as heating or cooling.


                                Your example from above is right, duty cycle will be 900/255 = ~4%. When the temperature climbs too slow, you can increase KP.


                                Assume KP = 50, So in your above example you get 9*50 = 450. Limited to 0..255 you get 255, system is heating up. When the difference is only 1°C you get 1*50 that is 5000/255 = ~20% duty cycle.. When that is about what radiation cools your system, you have a stable situation.





                                • 13. Re: simple PID like algorithm

                                  Hi Bob,


                                  thanks for the reply. I see now how it controls the duty cycle. simple yet clever as you and hli have been putting it ot me.


                                  Ive read that the derivative might not be applicable to a heating cooling situation especially given the slow time constant of heating/cooling.


                                  I was looking at the integral component.


                                  This is defined as: ki * errSum


                                  where the error sum is equal to error * timechange.


                                     unsigned long now = millis();
                                     double timeChange = (double)(now - lastTime);
                                  it gives a variable 'now' and its equal to millis(), im just trying to figure out how this is calculated or is it a random milliseconds number.


                                  i.e millis(100);  is it the same a cydelay in a manner of speaking,



                                  1 of 1 people found this helpful
                                  • 14. Re: simple PID like algorithm

                                    The I-part of the PID ensures that the error may become zero within a longer time. A side effect will be that the summed error will be corrected later producing some unwanted variance at that time. The I-part is good when you mix two chemical parts with two controlled pumps. At the end all errors are extinguished and in the resulting reservoir the concentration is exactly as wanted. What are you designing? A DNA multiplier?





                                    1 of 1 people found this helpful
                                    1 2 Previous Next