PID Control For CPU Temperature of Raspberry Pi

My motivation for PID Control For CPU Temperature of Raspberry Pi came for many reasons such as very hot CPU, very noisy fan's sound and fast battery consumption because the hot CPU makes the system really unstable while using Raspberry Pi for a long time. So, I have optimized the failing by using PID node on Node-RED. It's visually helpful for a trainee to understand the PID control system for an educational purpose.

CircuitDiagram

picture

UI

improvement

diagram

This will cover the basic steps that you need to follow to get started with open sources like PID node, MQTT node in the Node-RED. Also, it's really painful and hard to tune 3 gains like KP, KI, and KD as manual tuning(Trial and error) method. There are many tuning methods such as Manual tuning, Ziegler–Nichols, Tyreus Luyben, Cohen–Coon, Åström-Hägglund and Software tools such as Simulink in Matlab or Excel PID Simulator (enclosed). I've already provided my source codes in the Download List but If you use a different fan, you should tune PID gains because most physical fan's characteristics are different. You can get more details from below the linked instructable.

http://www.instructables.com/id/PID-Control-for-CPU-Temperature-of-Raspberry-Pi/

https://iotmakerblog.wordpress.com/2016/11/15/pid-control-for-cpu-temperature-of-raspberry-pi/

  • Tuning PID controller GIF

    • Manual tuning(Trial and error) ManualTuning

    • Ziegler–Nichols method Diagram

Table

Example

  • On/Off control Vs PID control OnOFFcontrolVsPIDcontrol
  • Total output of flow.json NodeRED
[{"id":"a721fd51.876a4","type":"inject","z":"2db5c9af.b6aef6","name":"","topic":"","payload":"","payloadType":"num","repeat":"5","crontab":"","once":true,"x":112.5,"y":599.7500015667506,"wires":[["93b0ad35.c1138"]]},{"id":"93b0ad35.c1138","type":"exec","z":"2db5c9af.b6aef6","command":"vcgencmd","addpay":false,"append":"measure_temp","useSpawn":"","name":"getCPUtemp","x":281.1031188964844,"y":600.2102961540222,"wires":[["d15583f.1df5b8"],[],[]]},{"id":"d15583f.1df5b8","type":"function","z":"2db5c9af.b6aef6","name":"CPU Temp Function","func":"//msg.payload = {'d':{'temp': parseFloat(msg.payload.replace(\"temp=\",\"\").replace(\"'C\\n\",\"\"))}};\nmsg.payload = parseFloat(msg.payload.replace(\"temp=\",\"\").replace(\"'C\\n\",\"\"));\nreturn msg;","outputs":1,"noerr":0,"x":488.69042205810547,"y":587.4643244743347,"wires":[["1dc8c470.f19eac"]]},{"id":"1dc8c470.f19eac","type":"mqtt out","z":"2db5c9af.b6aef6","name":"MQTT-CpuTemp","topic":"topic/cpuTemp","qos":"2","retain":"","broker":"1441bd92.da41e2","x":704.6428756713867,"y":587.3692317008972,"wires":[]},{"id":"3b892b21.0fa3f4","type":"comment","z":"2db5c9af.b6aef6","name":"Check CPU's Temperature","info":"","x":149.50003051757812,"y":553.3692555427551,"wires":[]},{"id":"2c65bde0.cb6922","type":"comment","z":"2db5c9af.b6aef6","name":"PID Control For CPU Temperature of Raspberry PI","info":"","x":225.71424102783203,"y":60.90494441986084,"wires":[]},{"id":"57809ee9.190f6","type":"PID control","z":"2db5c9af.b6aef6","name":"PID Controller","target":"40","kp":"85","ki":"1","kd":"5","x":304.7618827819824,"y":112.23830699920654,"wires":[["c6a48c32.e0696","a5b4b965.a34508","74f9653.700419c","b93afaf7.4a5928"]]},{"id":"c6a48c32.e0696","type":"debug","z":"2db5c9af.b6aef6","name":"","active":true,"console":"false","complete":"payload","x":548.2975921630859,"y":244.98829827989857,"wires":[]},{"id":"a5b4b965.a34508","type":"ui_chart","z":"2db5c9af.b6aef6","name":"Chart: PID Output Data","group":"e199b404.e60e58","order":3,"width":0,"height":0,"label":"PID Output","chartType":"line","legend":"false","xformat":"%H:%M:%S","interpolate":"linear","nodata":"PID Output","ymin":"","ymax":"","removeOlder":"1","removeOlderUnit":"3600","x":578.4880905151367,"y":166.32164478302002,"wires":[[],[]]},{"id":"74f9653.700419c","type":"ui_gauge","z":"2db5c9af.b6aef6","name":"Gauge: PID Output","group":"e199b404.e60e58","order":4,"width":0,"height":0,"gtype":"wave","title":"Gauge","label":"units","format":"{{value}}","min":0,"max":10,"colors":["#08f508","#e6e600","#ca3838"],"x":570.2975921630859,"y":205.98829827989857,"wires":[]},{"id":"24f82a86.633806","type":"debug","z":"2db5c9af.b6aef6","name":"","active":true,"console":"false","complete":"false","x":336.0475845336914,"y":494.2382901736669,"wires":[]},{"id":"8dd72124.a339b","type":"ui_chart","z":"2db5c9af.b6aef6","name":"Chart: CPU Temperature","group":"e199b404.e60e58","order":2,"width":0,"height":0,"label":"CPU Temperature [°C]","chartType":"line","legend":"false","xformat":"%H:%M:%S","interpolate":"linear","nodata":"CPU Temperature","ymin":"28","ymax":"50","removeOlder":"2","removeOlderUnit":"3600","x":377.0475845336914,"y":416.2382901736669,"wires":[[],[]]},{"id":"18b2cb2.7ef9b35","type":"ui_gauge","z":"2db5c9af.b6aef6","name":"Guage: CPU Temperature","group":"e199b404.e60e58","order":1,"width":0,"height":0,"gtype":"gage","title":"CPU Temperature [°C]","label":"°C","format":"{{value}}","min":"0","max":"100","colors":["#00b500","#e6e600","#f90404"],"x":380.0475845336914,"y":457.2382901736669,"wires":[]},{"id":"32f6d78d.42eaf8","type":"mqtt in","z":"2db5c9af.b6aef6","name":"MQTT-cpuTemp","topic":"topic/cpuTemp","qos":"2","broker":"1441bd92.da41e2","x":103,"y":414.6666383743286,"wires":[["8dd72124.a339b","18b2cb2.7ef9b35","24f82a86.633806"]]},{"id":"6f1bfb38.ef9924","type":"mqtt in","z":"2db5c9af.b6aef6","name":"MQTT-cpuTemp","topic":"topic/cpuTemp","qos":"2","broker":"1441bd92.da41e2","x":105.33333587646484,"y":113.3333215713501,"wires":[["57809ee9.190f6"]]},{"id":"f2b31368.f60fa","type":"function","z":"2db5c9af.b6aef6","name":"PLANT","func":"var PID = msg.payload; // PID Value\nvar cycle; // PWM duty cycle\nvar adjustment = 0; // duty cycle adjustment\n\nvar cycle = parseFloat(adjustment) + parseFloat(PID);\n\nif(cycle<0){\n    msg.payload=0;\n    return msg;\n}\n\nelse if(cycle>100){\n    msg.payload=100;\n    return msg;\n}\n\nelse\n{\n    msg.payload = parseFloat(cycle); \n   // 0 - 100\n    return msg;\n}\n\nreturn null;","outputs":1,"noerr":0,"x":320.7738227844238,"y":300.46430110931396,"wires":[["27434a7b.ecbbe6","76609f65.4027b"]]},{"id":"27434a7b.ecbbe6","type":"debug","z":"2db5c9af.b6aef6","name":"","active":true,"console":"false","complete":"false","x":544.1428833007812,"y":355.4286117553711,"wires":[]},{"id":"b93afaf7.4a5928","type":"mqtt out","z":"2db5c9af.b6aef6","name":"MQTT-pidController","topic":"topic/pidController","qos":"2","retain":"","broker":"1441bd92.da41e2","x":567.5714285714286,"y":112.85714285714285,"wires":[]},{"id":"634bcec1.d274c","type":"mqtt in","z":"2db5c9af.b6aef6","name":"MQTT-pidController","topic":"topic/pidController","qos":"2","broker":"1441bd92.da41e2","x":124.71428571428572,"y":300.2857142857143,"wires":[["f2b31368.f60fa"]]},{"id":"7eb41e08.9b9cf","type":"comment","z":"2db5c9af.b6aef6","name":"PID - PLANT","info":"","x":96.14285714285714,"y":256.71428571428567,"wires":[]},{"id":"39090c04.923b64","type":"comment","z":"2db5c9af.b6aef6","name":"Chart & Gauge : CPU temperature","info":"","x":172.85714721679688,"y":371.42857551574707,"wires":[]},{"id":"76609f65.4027b","type":"rpi-gpio out","z":"2db5c9af.b6aef6","name":"","pin":"7","set":false,"level":"0","out":"pwm","x":530,"y":301.71429443359375,"wires":[]},{"id":"1441bd92.da41e2","type":"mqtt-broker","z":"2db5c9af.b6aef6","broker":"localhost","port":"1883","clientid":"","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willRetain":null,"willPayload":"","birthTopic":"","birthQos":"0","birthRetain":null,"birthPayload":""},{"id":"e199b404.e60e58","type":"ui_group","z":"2db5c9af.b6aef6","name":"PID Controller For CPU Temperature of Raspberry Pi","tab":"669cc6a9.2a11d8","disp":true,"width":"9"},{"id":"669cc6a9.2a11d8","type":"ui_tab","z":"2db5c9af.b6aef6","name":"Home","icon":"dashboard"}]
leehaesung

Flow Info

created 11 months ago
updated 10 months, 2 weeks ago

Node Types

Core
  • comment (x4)
  • debug (x3)
  • exec (x1)
  • function (x2)
  • inject (x1)
  • mqtt in (x3)
  • mqtt out (x2)
  • mqtt-broker (x1)
  • rpi-gpio out (x1)
Other
  • PID control (x1)
  • ui_chart (x2)
  • ui_gauge (x2)
  • ui_group (x1)
  • ui_tab (x1)

Tags

  • PID
  • RaspberryPi
  • PIDtuning
  • CPU
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option