Arduino PID Controller - From Scratch!

แชร์
ฝัง
  • เผยแพร่เมื่อ 31 ธ.ค. 2024

ความคิดเห็น •

  • @careyian
    @careyian  ปีที่แล้ว +40

    I designed this system with an approach based around how I would like to have been thought it originally years ago when I studied it. All credit to the education I got, it was excellent, but for all the theory I had a firm grasp of, I failed utterly at being able to apply it in real life outside of simulations in Matlab. I think if I had been able to sit down with a physical system in front of me, code the microcontroller and see the outputs in real time it would have done so much for my understanding. My hope is that this is an accessible resource for people who are coming at this as a beginner hobbyist or a student, like I was, with a good understanding of the theory but fuzzy on the real world application.

    • @regardtkotze314
      @regardtkotze314 2 หลายเดือนก่อน +2

      I am sitting in the same situation. I am currently studying electronic engineering and I can calculate Kp, Ki, and Kd perfectly according to my specification whether it be critically damped or whatever. However, this is only if I have a transfer function in the frequency domain. I had no idea on how to implement it in the real life so this video is a great resource. Well done and thank you

    • @careyian
      @careyian  2 หลายเดือนก่อน +2

      Excellent! I'm really glad this helped!!

  • @dhaufjebzjchseis3828
    @dhaufjebzjchseis3828 7 หลายเดือนก่อน +10

    one of the most through explanations ive seen, so refreshing to see behind the scenes instead of someone just using a library that makes everything into a black box

    • @careyian
      @careyian  7 หลายเดือนก่อน +3

      Thanks for your kind words! Glad you liked it!

  • @mahdiallaw887
    @mahdiallaw887 9 หลายเดือนก่อน +7

    Man, at my 3rd year of engineering and i am thinking of making a stability project with 2 brushless motor, but I wasn’t sure if how easy it is, but after watching your video now I’m 💯 sure i will be doing it.
    The hand gestures, the way you explain and the plan you choose to explain the code and the oscillation graph it was all perfect. Thank you ❤

    • @careyian
      @careyian  9 หลายเดือนก่อน +1

      That’s so great to hear! Wishing you all the best with your project!

  • @dkraft
    @dkraft ปีที่แล้ว +16

    I've been tuning PID's for 25 years from chain driven sloppy movements to oven temps to CNC, and thought I was accomplished when I used the PID library.. I wish I had seen this years ago. Very well done and simplified the black box magic. Those block diagrams are great but thinking thru the math is essential for tuning intuition.

    • @careyian
      @careyian  ปีที่แล้ว +1

      Hey, thanks for your comment! Glad you liked it

  • @nhatcao8411
    @nhatcao8411 4 หลายเดือนก่อน +4

    your code clean, your explaination clear, your voice good --> 10/10 video, thanks bro !

    • @careyian
      @careyian  4 หลายเดือนก่อน +1

      Thanks friend! Glad you liked it!!

  • @antaf56
    @antaf56 หลายเดือนก่อน +1

    Great work Ian, very well presented, it will enlighten many students (and engineers) who still struggle with this topic!!

    • @careyian
      @careyian  หลายเดือนก่อน +1

      Thanks for your kind words!

  • @caleb_wole
    @caleb_wole 2 หลายเดือนก่อน +1

    Thanks for explaining this so well. I wish my control classes in uni showed explicitly how the theory is applied to real-world problems. After watching this, I feel I have a better intuitive understanding of PID control. God bless you!

    • @careyian
      @careyian  2 หลายเดือนก่อน +1

      You're very welcome!

  • @cosmacgrandpa
    @cosmacgrandpa ปีที่แล้ว +4

    Thank you! This was exactly what I needed to implement a motor speed controller. Your explanation was concise and clear and your video was enjoyable to watch.

    • @careyian
      @careyian  ปีที่แล้ว

      Hey, thanks! That’s great to hear, really glad it was useful for you!

  • @DragonzaS
    @DragonzaS 2 หลายเดือนก่อน +1

    Great video! Now understand PID and used it for my filament dryer. Now I am tuning it and trying out different combinations. Thanks!

    • @careyian
      @careyian  2 หลายเดือนก่อน +1

      Hey that’s awesome!! Really glad this helped!! Well done friend, keep having fun!

  • @Timmygardiner
    @Timmygardiner ปีที่แล้ว +3

    Loved your explanation throughout, very easy to follow and understand. Also, it's visible that you love this stuff too. Makes it an interesting watch.

    • @careyian
      @careyian  ปีที่แล้ว +1

      Hi Tim, glad you liked it, thanks for your feedback!

  • @EngineerNotFound
    @EngineerNotFound 2 หลายเดือนก่อน +2

    It's hard to know what an intro to PID should look like, and where to find one, but this was enough, not too theory heavy to get me started. Thanks!

    • @careyian
      @careyian  2 หลายเดือนก่อน +1

      You are very welcome! Glad it helped

  • @mariorafaelbritopavon456
    @mariorafaelbritopavon456 ปีที่แล้ว +3

    Many thanks brother, I´ve been interested in making balancing robots for fun, and donate the project to my university so other students can see the helpfulness and importance of PID in control systems. With this explanation I feel more confident in starting this iniciative.

    • @careyian
      @careyian  ปีที่แล้ว

      That’s a great idea! Happy this helped you! I have a project that I intended for a similar usage, feel free to check it out here: careyi3.github.io/balance_beam_kit/

  • @abdallahmostafa125
    @abdallahmostafa125 ปีที่แล้ว +3

    Man, this is perfect. ❤
    Respect from EGYPT.

    • @careyian
      @careyian  ปีที่แล้ว +2

      Very welcome!!

  • @prathameshmore4739
    @prathameshmore4739 หลายเดือนก่อน +1

    Kudos!!! Great work. Easy explanation

    • @careyian
      @careyian  หลายเดือนก่อน +1

      Hey, thanks for that, appreciate it!!

  • @HSuomi
    @HSuomi ปีที่แล้ว +1

    Thanks for this video and especially the link to the code. This helped me to understand the PID calculation. In code format its much easier to understand than to read Wiki and see the long formula with strange symbols :D

    • @careyian
      @careyian  ปีที่แล้ว

      You are very welcome! Glad it helped!

  • @mrlajel
    @mrlajel ปีที่แล้ว +1

    very fascinating experiment and easy to understand, for something that I really struggle to learn.

    • @careyian
      @careyian  ปีที่แล้ว

      Great, glad it was helpful!

  • @Letter_l
    @Letter_l 7 หลายเดือนก่อน +1

    doing 3rd year mechanical, and wanted to see actual application of such, this video is great.

    • @careyian
      @careyian  7 หลายเดือนก่อน +1

      Great to hear!

  • @jaszczurtd
    @jaszczurtd 11 หลายเดือนก่อน +1

    Thank you for this video! I'm currently working on a personal project - creating something like a diesel engine controller based on the VP37 pump. There's a type of inductive solenoid that adjusts the fuel dosage based on a PWM signal it receives. The feedback signal is from a Hartley generator, generating a DC voltage that changes depending on the position of the actuator. It seems like a straightforward task - just drive the solenoid with the right waveform based on the feedback voltage... But unfortunately, it's not that simple! Unexpected problems arise, such as the coil heating up during operation, requiring compensation for temperature drift, or fluctuations in the solenoid's supply voltage... The solenoid itself is mounted on springs and can oscillate on its own... :) I struggled with this a bit, and I have to say that PID behaves impressively well in this regard, better than all my other solutions. I'm still dealing with the issue of voltage fluctuations - when the coil's supply voltage changes, the solenoid changes its position. After a while, the PID stabilizes it, but there's still some fluctuation in the actuator's position. Thanks again for the great explanation in your video!"

    • @careyian
      @careyian  11 หลายเดือนก่อน +1

      Wow, that sounds like a complex problem, but it sounds like you understand it very well, only a matter of time before you fully crack it for sure! You are welcome, I’m glad this helped you! Keep up the great work

    • @jaszczurtd
      @jaszczurtd 11 หลายเดือนก่อน +1

      @@careyian I'll likely get to it, but if you've got any ideas to speed up my thinking process, feel free to drop them! :D

  • @arshiafas9361
    @arshiafas9361 5 หลายเดือนก่อน

    thanks mate, you helped a bachelor student here :)

    • @careyian
      @careyian  5 หลายเดือนก่อน

      Love to hear it! You are very welcome friend

  • @sami0_0-0
    @sami0_0-0 10 หลายเดือนก่อน +1

    Thank You so much!! Just what I needed.

    • @careyian
      @careyian  10 หลายเดือนก่อน

      You're welcome!

  • @hassegreiner9675
    @hassegreiner9675 3 หลายเดือนก่อน +1

    I controlled the temperature in a greenhouse (90m*20m*6m) almost entirely on the derivative portion - the time constant of that volume of air required it.

    • @careyian
      @careyian  3 หลายเดือนก่อน +1

      Cool!

  • @ObsequiousV4
    @ObsequiousV4 11 หลายเดือนก่อน +1

    Thanks for uploading this. Gigachad move

    • @careyian
      @careyian  11 หลายเดือนก่อน

      No problem!!

  • @mathiskose5348
    @mathiskose5348 ปีที่แล้ว +1

    Thank you so much for your effort 👍🏻
    Greetings from Germany

  • @keshavharipersad2024
    @keshavharipersad2024 ปีที่แล้ว +1

    awesome! thank you very much. You dont know how much I needed this

    • @careyian
      @careyian  ปีที่แล้ว +1

      Glad I could help!

  • @derrickadusei7987
    @derrickadusei7987 5 หลายเดือนก่อน +1

    This is an amazing content . i wish there was a quick way to calculate the pid values and tune them quickly since as an engineer you have dead lines to meet

    • @careyian
      @careyian  5 หลายเดือนก่อน +1

      Great point, real engineers can build actual models of these things and simulate different PID values, it’s never going to be 100%, there is a trade off of detailed models and the time it takes to build etc. There are also methods for tuning PID controllers. However, it might end up being faster to do it manually. You also have to think about if you are building a product, the base tuning might come from a model and simulation, you only need to build that once, then you can tweak and tune it afterwards.

  • @Bedfford
    @Bedfford 2 หลายเดือนก่อน +1

    HI! great video! But I don't understand how you pass the 'dt' value into the pid() function without using a additional parameter, like pid(error, dt); . Arduino manage the 'dt' as local or global variable ? Thanks!

    • @careyian
      @careyian  2 หลายเดือนก่อน +2

      Hi there, yes dt is a global variable. I could have done the same with error. It doesn’t make much difference here. Glad you enjoyed the video!

  • @kleindavid9416
    @kleindavid9416 ปีที่แล้ว +1

    this video is just incredibly good

    • @careyian
      @careyian  ปีที่แล้ว +1

      Thanks so much!

  • @dhaufjebzjchseis3828
    @dhaufjebzjchseis3828 7 หลายเดือนก่อน +1

    one thing that confuses me is when the actual goes above the set point, won't the proportional and derivative terms become negative , so couldnt there be a case where the output is negative and you try and write a negative voltage to the pin?

    • @careyian
      @careyian  7 หลายเดือนก่อน +2

      Good spot, yes that can in theory happen here. In practice those negative values will be fighting against the accumulated positive integral error, so they aren’t likely to make the overall output negative. Specifically here a negative value being fed into analogWrite will not behave correctly as it will write the 8bit unsigned value of it directly to the register, which you wouldn’t want. What this implementation is really missing is scaling on the output to ensure that whatever the value that gets spit out, it’s always within a certain range.

  • @Asfera-x2g
    @Asfera-x2g 7 วันที่ผ่านมา +1

    Mr.can you explain how to get the pid parameters like Kp,Ki and Kd

    • @careyian
      @careyian  7 วันที่ผ่านมา +1

      @@Asfera-x2g I can’t I’m afraid, how you set those values varies wildly between different systems. Watch this video and then watch my other videos on the topic to gain a better understanding of of how to do this.

  • @GapRecordingsNamibia
    @GapRecordingsNamibia ปีที่แล้ว +3

    Well explained, but, this is a digital system which is a fast response system which means that the closed loop responds quickly, when you start implementing this with a slow response like a heating element this response is very slow because the closed loop has to wait for the "response" from the thermistor that monitors the surrounding heating. This gets very interesting very quickly...... Nice video and a good delivery.

    • @careyian
      @careyian  ปีที่แล้ว +2

      Thanks! Yeah, you are 100% correct, this is also a totally contrived example, but it gives a clear picture of what is going on and can be done with minimal parts etc. You have me thinking of a little desktop temprature based control demo now... Maybe something for a future video!

    • @GapRecordingsNamibia
      @GapRecordingsNamibia ปีที่แล้ว +1

      @@careyian 🤣😂 Yes... I am busy with a pulltruder that has to use PID to control the heated extruder with.... So this video had me for a full 29minutes and 50seconds...... PID is a dark art and can send one down a horrible rabbit hole!🤣😂

    • @careyian
      @careyian  ปีที่แล้ว +1

      Love it! Exciting project, hope it goes well for you!

  • @pankajgodbole8456
    @pankajgodbole8456 10 หลายเดือนก่อน +1

    It is awesome! Very useful video for me

    • @careyian
      @careyian  10 หลายเดือนก่อน

      Glad it was helpful!

    • @pankajgodbole8456
      @pankajgodbole8456 10 หลายเดือนก่อน +1

      @@careyian sure

  • @ShubhamShanbhag
    @ShubhamShanbhag 3 หลายเดือนก่อน +1

    Do you have any video or technique for autotuning the kp, ki and kd values?

    • @careyian
      @careyian  3 หลายเดือนก่อน +2

      Unfortunately no, I usually always manually tune controllers

  • @ThomasHaberkorn
    @ThomasHaberkorn 10 หลายเดือนก่อน +2

    very strong video! could you do a follow up video implementing a MPC control algorithm for the same system in Arduino? It would hightlight the key differences and relative performance really well I think

    • @careyian
      @careyian  10 หลายเดือนก่อน

      Hi there, thanks for the comment, might do that in the future!

  • @abdoelzohiery2133
    @abdoelzohiery2133 6 หลายเดือนก่อน +1

    That's a great explanation; however, I think we should consider making the PID gains more adaptive to enhance the code's generality and accuracy. Would this improvement be beneficial or will it add unnecessary overhead to the code?

    • @careyian
      @careyian  6 หลายเดือนก่อน +2

      Hey there, what do you mean by adaptive?

    • @abdoelzohiery2133
      @abdoelzohiery2133 6 หลายเดือนก่อน +1

      @@careyian I mean if we can make a simple method that changes the gains within the runtime based on the time the system takes to reach its steady state. Therefore we will have a better gains for the next time.

    • @careyian
      @careyian  6 หลายเดือนก่อน +2

      Yeah, I guess you could do something like that if you wanted to. This is just example code, I have no plans to do anything more with it.

  • @harivigneshm9170
    @harivigneshm9170 8 หลายเดือนก่อน +1

    Than you very much! Your video has given me a good understanding of how PID works. I do have a doubt though, the integral term accumulates over time so if it keep on adding up it would contribute more to the error calculation right? I have seen some vids where they have a saturation limit, but even then there is no way to reduce this unless it overshoots right? Should we reset the addition over time from time to time if it reaches the set-point?

    • @careyian
      @careyian  8 หลายเดือนก่อน +1

      That is a really excellent question. In theory if the error oscillates around the set point, the integral term should settle assuming the error goes to zero and so should end up stable. However, there are cases where this might not happen and you end up with something called integrator windup happening. You can could avoid this by placing limits on the max values or periodically clearing the integrator value back to zero when you approach stability.

    • @harivigneshm9170
      @harivigneshm9170 8 หลายเดือนก่อน +1

      @@careyian thank you very much for the response! I'll google about Integrator windup and include limits on max and min values in my implementation

  • @clumsydins9771
    @clumsydins9771 8 วันที่ผ่านมา +1

    What font are you using in the Arduino IDE? I find it very easy to read

    • @careyian
      @careyian  7 วันที่ผ่านมา +1

      Hey, it's Fira Code (with ligatures enabled)

  • @khanggggg
    @khanggggg 4 หลายเดือนก่อน +1

    Respect from Viet Nam

    • @careyian
      @careyian  4 หลายเดือนก่อน +1

      Thanks!!

  • @adilshahid0503
    @adilshahid0503 ปีที่แล้ว +1

    Thankyou very much for this one

    • @careyian
      @careyian  ปีที่แล้ว +2

      You are very welcome! Glad you enjoyed it, hope it was helpful my friend

  • @fikretkus106
    @fikretkus106 ปีที่แล้ว +1

    What if I don't use the dt value? I mean for integral instead of integral +=error*dt I will use integral+=error. The same for derivative. The dt determined by the timer interrupt. Meaning it is a constant value. But I don't use it in calculations. Will it work correctly?

    • @careyian
      @careyian  ปีที่แล้ว

      Great question, so the dt or sampling time really depends on what you are trying to control and what frequency your system will run at. For example if you have a slow system, it might be kind of pointless to sample it very quickly. Where as a fast system needs to be sampled more quickly. Without knowing what you are sampling, I’d say a basic model without the dt value might work. Assuming it’s a simple system and the period is always the exact same.
      Assuming you know dt based on the timer, I’d suggest just including it to reflect what is actually going on instead of abstracting it away.

    • @careyian
      @careyian  ปีที่แล้ว

      Well that’s a good point about being at optimal performance. If you use dt you can tune the system by altering the value and seeing how it responds. You are effectively assuming that that the sampling time is 1 second by leaving off dt. If you include it to match your system you can see what effects on the response the sampling rate will have on the PIDs outputs. In the real world, your sampling time is probably much faster, so you should include this so that the frequency that your system is operating at and the frequency that your PID appears to be operating at are the same.

    • @fikretkus106
      @fikretkus106 ปีที่แล้ว +1

      I am back with another question. I saw you convert the dt unit to second. Why? What should be the unit? Can I use ms, us or ns?

    • @careyian
      @careyian  ปีที่แล้ว

      So the units aren’t super important, they will effectively scale the values differently. However, seconds are the standard unit of measurement for time, so by converting to this you are ensuring the time measured reflects what’s actually happening. It won’t really break anything as such if you use different units

    • @fikretkus106
      @fikretkus106 ปีที่แล้ว +1

      @@careyian But the values will change 1000 times for ms. How this will not effect the result? I don't get it.

  • @iddy2122
    @iddy2122 2 หลายเดือนก่อน +1

    I thought it was between 0 and 1023 (ie 2**10 with 0 as '1')?

    • @careyian
      @careyian  2 หลายเดือนก่อน +1

      Sorry, I’m not exactly sure which part you are talking about. However, analogRead returns a value between 0-1023, and analogWrite accepts a value between 0-255. Did I mix these up at some point in the video?

  • @VinothkannanR-vu9yl
    @VinothkannanR-vu9yl 4 หลายเดือนก่อน +1

    To achieve critical damping what is the value?

    • @careyian
      @careyian  4 หลายเดือนก่อน +1

      Hi, I don't know what you mean, could you elaborate? The values will probably be different if you set this up yourself and you will have to tune them yourself.

    • @VinothkannanR-vu9yl
      @VinothkannanR-vu9yl 11 วันที่ผ่านมา +1

      @@careyian In a critically damped system, the steady-state error is zero; this means that the system will reach its desired output value without any overshoot or oscillation, essentially achieving the fastest possible response to a change in input while remaining stable

    • @careyian
      @careyian  11 วันที่ผ่านมา +2

      @@VinothkannanR-vu9yl Yes, I know what critically damped means, but the "value" you ask for will depend on what components etc. you are using, so I can't possibly answer that for you.

  • @TheRealROCCETS
    @TheRealROCCETS ปีที่แล้ว +1

    Great video 👍🏽
    I have one big question that I am not able to solve…
    In my scenario, I need a PID control that prevents my motor from accelerating beyond 6 km/h, even with the throttle fully engaged. That means my target is 6, and my actual value is the measured speed at the front wheel. However, the PID has to output a value between 0-255 for throttle regulation. How do I establish the correlation here? Your code example doesn't provide me with values within this range because I'm only feeding it with speeds.

    • @careyian
      @careyian  ปีที่แล้ว

      The trick here is you need to remember that the scale of your units doesn’t matter. If your input into the PID is some value of speed, as you mentioned k/h, you need to scale your PID gains to give you output values in the range you are interested in. You can also write some scaling logic if you want to bring them into the same kind of range, but that’s up to you.

  • @admbruce
    @admbruce ปีที่แล้ว +1

    Appreciate for sharing !~ Would be grateful if could also share the hardware screenshot as well 😀

    • @careyian
      @careyian  ปีที่แล้ว

      Hi Bruce, glad you liked it! If you follow the link in the description I have a circuit diagram there which should explain the hardware setup.

  • @aliaklee765
    @aliaklee765 ปีที่แล้ว +1

    thx your ass pro❤❤❤❤

  • @laurenvickers8615
    @laurenvickers8615 ปีที่แล้ว +1

    Sassy 💁🏻‍♀️