Arduino PID Controller - From Scratch!

  Рет қаралды 50,382

Ian Carey

Ian Carey

Жыл бұрын

In this video I dig into the details of a basic PID controller implemented on an Arduino. Check the link below for the code and reference materials to get started implementing your own controller.
Code: gist.github.com/careyi3/02a57...
RC Low Pass Filter Video: • RC Low Pass Filters - ...
Website: iancarey.ie
Patreon: / careyi3
#arduino #pid #controller #controltheory #pidcontroller #pidcontrol

Пікірлер: 70
@careyian
@careyian Жыл бұрын
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.
@dhaufjebzjchseis3828
@dhaufjebzjchseis3828 2 ай бұрын
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 2 ай бұрын
Thanks for your kind words! Glad you liked it!
@cosmacgrandpa
@cosmacgrandpa 9 ай бұрын
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 9 ай бұрын
Hey, thanks! That’s great to hear, really glad it was useful for you!
@Timmygardiner
@Timmygardiner 11 ай бұрын
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 11 ай бұрын
Hi Tim, glad you liked it, thanks for your feedback!
@dkraft
@dkraft 7 ай бұрын
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 7 ай бұрын
Hey, thanks for your comment! Glad you liked it
@derrickadusei7987
@derrickadusei7987 Күн бұрын
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 Күн бұрын
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.
@mahdiallaw887
@mahdiallaw887 4 ай бұрын
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 4 ай бұрын
That’s so great to hear! Wishing you all the best with your project!
@HSuomi
@HSuomi 8 ай бұрын
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 8 ай бұрын
You are very welcome! Glad it helped!
@mrlajel
@mrlajel 10 ай бұрын
very fascinating experiment and easy to understand, for something that I really struggle to learn.
@careyian
@careyian 10 ай бұрын
Great, glad it was helpful!
@abdallahmostafa125
@abdallahmostafa125 Жыл бұрын
Man, this is perfect. ❤ Respect from EGYPT.
@careyian
@careyian Жыл бұрын
Very welcome!!
@mariorafaelbritopavon456
@mariorafaelbritopavon456 7 ай бұрын
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 7 ай бұрын
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/
@Letter_l
@Letter_l 2 ай бұрын
doing 3rd year mechanical, and wanted to see actual application of such, this video is great.
@careyian
@careyian 2 ай бұрын
Great to hear!
@mathiskose5348
@mathiskose5348 10 ай бұрын
Thank you so much for your effort 👍🏻 Greetings from Germany
@careyian
@careyian 10 ай бұрын
Welcome!
@user-cs5dq8tb8m
@user-cs5dq8tb8m 5 ай бұрын
Thank You so much!! Just what I needed.
@careyian
@careyian 5 ай бұрын
You're welcome!
@kleindavid9416
@kleindavid9416 Жыл бұрын
this video is just incredibly good
@careyian
@careyian Жыл бұрын
Thanks so much!
@jaszczurtd
@jaszczurtd 6 ай бұрын
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 6 ай бұрын
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 6 ай бұрын
@@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
@ThomasHaberkorn
@ThomasHaberkorn 5 ай бұрын
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 5 ай бұрын
Hi there, thanks for the comment, might do that in the future!
@ObsequiousV4
@ObsequiousV4 5 ай бұрын
Thanks for uploading this. Gigachad move
@careyian
@careyian 5 ай бұрын
No problem!!
@keshavharipersad2024
@keshavharipersad2024 10 ай бұрын
awesome! thank you very much. You dont know how much I needed this
@careyian
@careyian 10 ай бұрын
Glad I could help!
@adilshahid0503
@adilshahid0503 Жыл бұрын
Thankyou very much for this one
@careyian
@careyian Жыл бұрын
You are very welcome! Glad you enjoyed it, hope it was helpful my friend
@GapRecordingsNamibia
@GapRecordingsNamibia Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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 Жыл бұрын
Love it! Exciting project, hope it goes well for you!
@pankajgodbole8456
@pankajgodbole8456 4 ай бұрын
It is awesome! Very useful video for me
@careyian
@careyian 4 ай бұрын
Glad it was helpful!
@pankajgodbole8456
@pankajgodbole8456 4 ай бұрын
@@careyian sure
@harivigneshm9170
@harivigneshm9170 3 ай бұрын
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 3 ай бұрын
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 3 ай бұрын
@@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
@abdoelzohiery2133
@abdoelzohiery2133 22 күн бұрын
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 22 күн бұрын
Hey there, what do you mean by adaptive?
@abdoelzohiery2133
@abdoelzohiery2133 20 күн бұрын
@@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 20 күн бұрын
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.
@dhaufjebzjchseis3828
@dhaufjebzjchseis3828 2 ай бұрын
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 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.
@admbruce
@admbruce 11 ай бұрын
Appreciate for sharing !~ Would be grateful if could also share the hardware screenshot as well 😀
@careyian
@careyian 11 ай бұрын
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.
@fikretkus106
@fikretkus106 9 ай бұрын
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 9 ай бұрын
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 9 ай бұрын
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 8 ай бұрын
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 8 ай бұрын
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 8 ай бұрын
@@careyian But the values will change 1000 times for ms. How this will not effect the result? I don't get it.
@TheRealROCCETS
@TheRealROCCETS 7 ай бұрын
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 7 ай бұрын
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.
@aliaklee765
@aliaklee765 Жыл бұрын
thx your ass pro❤❤❤❤
@laurenvickers8615
@laurenvickers8615 Жыл бұрын
Sassy 💁🏻‍♀️
Ball & Beam Kit - Part 3
30:59
Ian Carey
Рет қаралды 1,6 М.
DC motor PID speed control
15:29
Curio Res
Рет қаралды 225 М.
WHAT’S THAT?
00:27
Natan por Aí
Рет қаралды 13 МЛН
PID Controller Explained
9:25
RealPars
Рет қаралды 801 М.
PID vs. Other Control Methods: What's the Best Choice
10:33
RealPars
Рет қаралды 113 М.
How to Tune a PID Controller
18:14
PID Explained
Рет қаралды 100 М.
PID Controller Implementation in Software - Phil's Lab #6
20:06
Phil’s Lab
Рет қаралды 205 М.
PID Balance+Ball | full explanation & tuning
13:13
Electronoobs
Рет қаралды 734 М.
I made a SUPER FAST Line Follower Robot Using PID!
10:02
Shyam Ravi
Рет қаралды 302 М.
How to control a DC motor with an encoder
9:30
Curio Res
Рет қаралды 427 М.
What is a PID Controller? | DigiKey
22:19
DigiKey
Рет қаралды 68 М.
Hardware Demo of a Digital PID Controller
2:58
Gregory L. Holst
Рет қаралды 979 М.
PIDs Simplified
13:07
SiieeFPV
Рет қаралды 289 М.
Как удвоить напряжение? #электроника #умножитель
1:00
Hi Dev! – Электроника
Рет қаралды 979 М.
Смартфон УЛУЧШАЕТ ЗРЕНИЕ!?
0:41
ÉЖИ АКСЁНОВ
Рет қаралды 1,2 МЛН
Как правильно выключать звук на телефоне?
0:17
Люди.Идеи, общественная организация
Рет қаралды 1,8 МЛН
تجربة أغرب توصيلة شحن ضد القطع تماما
0:56
صدام العزي
Рет қаралды 59 МЛН
Samsung laughing on iPhone #techbyakram
0:12
Tech by Akram
Рет қаралды 2,4 МЛН
Здесь упор в процессор
18:02
Рома, Просто Рома
Рет қаралды 397 М.
Лазер против камеры смартфона
1:01
NEWTONLABS
Рет қаралды 609 М.