{"id":1314,"date":"2011-04-20T07:07:04","date_gmt":"2011-04-20T11:07:04","guid":{"rendered":"http:\/\/brettbeauregard.com\/blog\/?p=1314"},"modified":"2024-06-07T14:29:22","modified_gmt":"2024-06-07T18:29:22","slug":"how-fast-is-the-arduino-pid-library","status":"publish","type":"post","link":"http:\/\/brettbeauregard.com\/blog\/2011\/04\/how-fast-is-the-arduino-pid-library\/","title":{"rendered":"How Fast is the Arduino PID Library?"},"content":{"rendered":"<p>A couple people have asked me about speed since v1 was released.  Since I had no idea, I decided I should do a little benchmarking. (Tests were done on a Duemilanove with the ATMega168.)<\/p>\n<h3>The Test<\/h3>\n<pre class=\"brush: css; gutter: true; highlight: [12,17,18,19,20,24,25,26]; title: ; notranslate\" title=\"\">\r\n#include &lt;PID_v1.h&gt;\r\n\r\ndouble Setpoint, Input, Output;\r\nPID myPID(&amp;Input, &amp;Output, &amp;Setpoint,2,5,1, DIRECT);\r\n\r\nvoid setup()\r\n{\r\n  Input = analogRead(0);\r\n  Setpoint = 100;\r\n\r\n  myPID.SetMode(AUTOMATIC);\r\n  Serial.begin(9600);\r\n}\r\n\r\nvoid loop()\r\n{\r\n  unsigned long startTime = millis();\r\n\/*Do a PID calculation 10000 times*\/\r\n  for(int i=0;i&lt;10000;i++)\r\n  {\r\n  Input = analogRead(0);\r\n  myPID.Compute();\r\n  analogWrite(3,Output);\r\n  }\r\n\/*print the elapsed time*\/\r\n  Serial.println(millis()-startTime);\r\n}\r\n<\/pre>\n<p>The code above is a modification of the <a href=\"http:\/\/arduino.cc\/playground\/Code\/PIDLibaryBasicExample\">Basic PID example<\/a>.  Rather than do the pid stuff once though, it calls it 10000 times in a row.  Dividing the resulting elapsed time by 10000 gives us precision down into the microsecond range.<\/p>\n<p>Not pictured in this code, I also went into PID_v1.cpp and made SampleTime=0.  That ensured that every time Compute() was called, the PID equation was evaluated.<\/p>\n<p>One last note. Notice that analogRead and analogWrite are in the for loop.  I couldn&#8217;t think of a situation where you&#8217;d have a PID without also having those functions somewhere in the loop, so I included them in the test.<\/p>\n<h3>The Results<\/h3>\n<p><a href=\"http:\/\/brettbeauregard.com\/blog\/wp-content\/uploads\/2011\/04\/BasicSpeedResults.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/brettbeauregard.com\/blog\/wp-content\/uploads\/2011\/04\/BasicSpeedResults.png\" alt=\"\" title=\"BasicSpeedResults\" width=\"320\" height=\"235\" class=\"aligncenter size-full wp-image-1333\" srcset=\"http:\/\/brettbeauregard.com\/blog\/wp-content\/uploads\/2011\/04\/BasicSpeedResults.png 320w, http:\/\/brettbeauregard.com\/blog\/wp-content\/uploads\/2011\/04\/BasicSpeedResults-300x220.png 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/a><\/p>\n<p>2088 mSec \/ 10000 = <b>0.21 mSec<\/b><br \/>\nOk.  Pretty fast.  Now just for fun, I commented out the read and write code (lines 21 &#038; 23) to see how much of the 0.21 was due to the Compute function:<\/p>\n<p><a href=\"http:\/\/brettbeauregard.com\/blog\/wp-content\/uploads\/2011\/04\/TrimSpeedResults.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/brettbeauregard.com\/blog\/wp-content\/uploads\/2011\/04\/TrimSpeedResults.png\" alt=\"\" title=\"TrimSpeedResults\" width=\"320\" height=\"235\" class=\"aligncenter size-full wp-image-1334\" srcset=\"http:\/\/brettbeauregard.com\/blog\/wp-content\/uploads\/2011\/04\/TrimSpeedResults.png 320w, http:\/\/brettbeauregard.com\/blog\/wp-content\/uploads\/2011\/04\/TrimSpeedResults-300x220.png 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/a><br \/>\n826mSec \/ 10000 = <b>0.08 mSec<\/b><br \/>\nSo the PID was only responsible for about a third of the total time.  Unless I&#8217;m missing something (please tell me if I am,) it looks like the Compute function is only slightly slower than an analog read or an analog write! <\/p>\n<h3>Some Closing Thoughts<\/h3>\n<p>Looking at these results, your first inclination might be to make the sample time as small as possible (1 mSec.)  After all, that&#8217;s WAY bigger than the 0.08 mSec required. 2 things to remember:<\/p>\n<ol>\n<li>There&#8217;s other code in your program.  All together it may add up to more than 1mSec<\/li>\n<li>Even if your loop time is less than 1 mSec (say it&#8217;s 0.7 mSec,)  Compute only gets called once per loop, so the pid will actually be evaluated every 1.4 mSec.\n<\/ol>\n<p>Bottom line, if you&#8217;re looking to evaluate the pid really quickly, you need to be careful to avoid weird performance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A couple people have asked me about speed since v1 was released. Since I had no idea, I decided I should do a little benchmarking. (Tests were done on a Duemilanove with the ATMega168.) The Test #include &lt;PID_v1.h&gt; double Setpoint, &hellip; <a href=\"http:\/\/brettbeauregard.com\/blog\/2011\/04\/how-fast-is-the-arduino-pid-library\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7],"tags":[6,41],"class_list":["post-1314","post","type-post","status-publish","format-standard","hentry","category-pid","tag-arduino","tag-pid"],"_links":{"self":[{"href":"http:\/\/brettbeauregard.com\/blog\/wp-json\/wp\/v2\/posts\/1314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/brettbeauregard.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/brettbeauregard.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/brettbeauregard.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/brettbeauregard.com\/blog\/wp-json\/wp\/v2\/comments?post=1314"}],"version-history":[{"count":46,"href":"http:\/\/brettbeauregard.com\/blog\/wp-json\/wp\/v2\/posts\/1314\/revisions"}],"predecessor-version":[{"id":1399,"href":"http:\/\/brettbeauregard.com\/blog\/wp-json\/wp\/v2\/posts\/1314\/revisions\/1399"}],"wp:attachment":[{"href":"http:\/\/brettbeauregard.com\/blog\/wp-json\/wp\/v2\/media?parent=1314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/brettbeauregard.com\/blog\/wp-json\/wp\/v2\/categories?post=1314"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/brettbeauregard.com\/blog\/wp-json\/wp\/v2\/tags?post=1314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}