Arduino PID motor position and speed control

Sdílet
Vložit
  • čas přidán 15. 12. 2016
  • My attempts at PID control of a small DC motor with encoder.
    Arduino UNO clone + L298N H-Bridge. Links to the software: github.com/raydike/PID_positi...
    github.com/raydike/PID_speed_...
    New version:
    create.arduino.cc/editor/rayd...
    create.arduino.cc/editor/rayd...
    Libraries can be found here:
    playground.arduino.cc/Main/Pin...
    playground.arduino.cc/Code/PID...
    schematic sketch here: i67.tinypic.com/sczu9t.jpg or here: ibb.co/j6iXK5
    17-09-19 new version with interrupt: • Arduino PID motor cont...
    Different circuit diagram too!!!
  • Krátké a kreslené filmy

Komentáře • 194

  • @300henry
    @300henry Před 5 lety +1

    Thank you for sharing all the details of this solution. Superb work!

  • @didiersee
    @didiersee Před 5 lety

    Congratulations and thank you for sharing. 👏 👌 👏

  • @gortnewton4765
    @gortnewton4765 Před 5 lety

    Very nice indeed. I'll make this, thank you kindly.

  • @90FF1
    @90FF1 Před 6 lety +1

    raydike, thanks for the posting. Following your instructions, the set up worked for me "right out of the gate". Fun project to satisfy my curiosity. Now I'm looking for a means to display the shaft position. Possibly even a GUI to input desired position.

  • @user-yo4ku1ro6s
    @user-yo4ku1ro6s Před 6 měsíci

    Grate video sir

  • @AlexisLozada
    @AlexisLozada Před 7 lety

    What reference is the engine? In what electronic device can I find it to recycle?

  • @andersonmadureira5055
    @andersonmadureira5055 Před 6 lety

    Thank you so much!

  • @denisesilva6250
    @denisesilva6250 Před 5 lety +1

    What is the model of the enconder?
    Why in the code do you have 2 motors?M1 and M2?

    • @raydike
      @raydike  Před 5 lety

      For the model see the other comments. M1 and M2 are the PWM outputs to the H-bridge so the motor can turn both ways.

  • @Javier_Corado
    @Javier_Corado Před 6 lety

    Great project, but what kind of motor and encoder are you using? And how those two are put together into one?

    • @raydike
      @raydike  Před 6 lety

      The encoder is mounted on the rear of the electric DC motor and is connected to the motor shaft. It is an optical quadrature encoder. You can google this to find out more

  • @raydike
    @raydike  Před 6 lety

    Thanks!

  • @piyawat1989
    @piyawat1989 Před 6 lety

    Thank You

  • @gerardoramirez9450
    @gerardoramirez9450 Před 4 lety

    Hi! That is such a great video, however, looking at your code for the speed control I read this line: setpoint = temp / 500; I would like to know where does that 500 comes from? Is it the resolution from the encoder?
    Thank you and keep it that way. XD

    • @raydike
      @raydike  Před 4 lety

      yes proportional to encoder resolution.

  • @BarsBayraktutan
    @BarsBayraktutan Před 5 lety

    Arduino and L298N have made common GND connection but the Arduino is getting very hot. Can you help me ?

  • @nimanegarandeh1325
    @nimanegarandeh1325 Před 7 lety +1

    Hi, I have a project and in part of that, I need to control dc motor position. I tried to do similar work as what you did in this video, but my circuit doesn't work. I couldn't find the problem either. Can you send me a better sketch of this circuit or tell me how I can contact you? I really appreciate if you can help me.

  • @sandovaltrejocarlos3868
    @sandovaltrejocarlos3868 Před 2 lety +1

    Hi, I was wondering If the Encoder needs to be suplied with an independent external power suply, because I conected the encoder to the same power suply where I connected the L298 as well, Is It correct?

    • @raydike
      @raydike  Před 2 lety

      The encoder needs 5V this can be supplied by the 5V regulator on the L298 board or any other 5V source as long as they share a common ground.

  • @hamidou5126
    @hamidou5126 Před 4 lety

    Thanks for your work bro but we want to know how did you do for getting a model of system ? ( transfer function )

  • @skyOfpersia
    @skyOfpersia Před 7 lety

    hi thank you for your great video, where you have determine the potentiometer in your code?

    • @raydike
      @raydike  Před 7 lety +1

      The potentiometer is connected to A0 of the Arduino

  • @alexngo326
    @alexngo326 Před 5 lety

    hi ray
    Can i control more than one position of this program ?because i saw many example just control for one position
    if i want to change the controlled position at different time, how can i do?
    is it setting more encoderpos and timer?
    cause the different time 'for different position control?

    • @raydike
      @raydike  Před 5 lety

      I made a new, interrupt driven version: czcams.com/video/VkvO8l95RDo/video.html this will be easier to modify for your needs

  • @adriandylik7642
    @adriandylik7642 Před 6 lety

    best

  • @prans5037
    @prans5037 Před 5 lety

    Thanks for share

  • @oigencyofficial9038
    @oigencyofficial9038 Před 3 lety +1

    HELLO,,
    working with PID has been hectic for the last two weeks! thanks a lot for this great effort!!
    I have done a few changes on your code to test angles from serial(userInput)! but its not accurate ! would you please help me make the angle more precise!

  • @mateogermanorjuelagalvez3820

    hello I would like to know, what is the engine reference?

  • @al5502
    @al5502 Před 6 lety

    Never mind - just upgraded the IDE

  • @arthurilyuts7575
    @arthurilyuts7575 Před 5 lety

    Raydike!
    In original; code I downloaded this line looks like setpoint = analogRead(0) * 5;
    I did as you advise but it doesn't help :(
    What else I can do?

    • @raydike
      @raydike  Před 5 lety

      Check everything methodically. Do you get the right encoder signals, is your H-bridge wired properly is your motor ok etc.

  • @susanfontes2934
    @susanfontes2934 Před 7 lety

    What are you using for an encoder?

    • @raydike
      @raydike  Před 7 lety +1

      It's just an optical encoder that came with the motor. I guess about 100 pulses per revolution.

  • @woldecosgrove
    @woldecosgrove Před 2 měsíci

    Hello Ray, i finally get it works motor is running,, but unable to control speed via PoT, could you advise ?

  • @pandukamalalanayake3731
    @pandukamalalanayake3731 Před 6 lety +2

    github library v2.19 and v2.17 has errors i think. it cant verify . also i'm new to this , can you up-lode a working library file or inbox me. thanks

  • @juanea
    @juanea Před 7 lety

    Thank you for the video.
    Could you please explain me this part of the code:
    For the PID on the speed control.
    setpoint = temp / 500; // modify division to fit motor and encoder characteristics
    How did you get that division?
    Thank you.

    • @raydike
      @raydike  Před 7 lety +1

      Since I don't have the specs of the gearbox and encoder it was a bit of trial and error, to get the speed I needed. Smaller divisor = higher speed.

    • @juanea
      @juanea Před 7 lety

      Thank you.

    • @quannguyen-lw3gq
      @quannguyen-lw3gq Před 3 lety

      @@raydike can i ask you something? if i know the exact rpm of the motor and pulses per revolution of the encoder, how can i calculate the divisor?

  • @kifahzaidan4505
    @kifahzaidan4505 Před rokem

    Hi,
    Thanks a lot, its really greate effort and great knowledge.
    i need to do a project for a preset position linear actuator with a DC motor abd a stepper motor, with Arduino Mega or UNO but i am not able to write the code and the scheme, can you please help.
    Regards

  • @MrClickpumped
    @MrClickpumped Před 7 lety

    how can i use this code in a monster moto shield becose this shield got a 2 pines for the direction the motor a 2 for the motor b and 1pin to pwm motor a and 1 for pwm motor b pleaseeeee help meeee thanks!!!

  • @jatinvarshney4195
    @jatinvarshney4195 Před 5 lety

    what us the significance of this line " setpoint = analogRead(0) * 5".It is not used in further code anywhere. Will you please explain.

    • @raydike
      @raydike  Před 5 lety +1

      The setpoint is the position where you want your motor to go. It is part of the PID library. The PID algorithm drives the motor until the input is equal to the setpoint. Here the input is the encoder value. The setpoint is the potmeter position (0-1023) multiplied by 5 to give a bigger range. Maybe you should read about PID control a bit more then it will become clear.

    • @jatinvarshney4195
      @jatinvarshney4195 Před 5 lety +1

      @@raydike Thanks Raydike, I missed that it is an argument in PID function. Thanks again.

  • @al5502
    @al5502 Před 6 lety

    I can't compile w/o errors, error say something about PID

  • @stephanesimoens51
    @stephanesimoens51 Před 5 lety

    how does the L298N can work without enB ou enA wire? is there only (M1 and M2) in1 and 2 connected? how do you provide the PWM to the L298N from the arduino in the code ?

    • @raydike
      @raydike  Před 5 lety

      The enable pins A and B are already connected to +5 on the H-bridge module via jumpers. The PWM is sent to the module by the analogWrite commands. In arduino analogWrite is the same as PWM.

    • @stephanesimoens51
      @stephanesimoens51 Před 5 lety

      works fine with the jumper thanks

    • @stephanesimoens51
      @stephanesimoens51 Před 5 lety

      ENA to 5v supply

    • @stephanesimoens51
      @stephanesimoens51 Před 5 lety

      check the code there is an option with a comment like // try ???? is turn continuously

  • @JoaoVictor-ir3yu
    @JoaoVictor-ir3yu Před 4 lety

    Hi man, could you help me with something? I try to double the variables etc ... to use two engines but pid stops working (pid for speed control)

    • @raydike
      @raydike  Před 4 lety

      You will have to use two separate pin change interrupts for the encoders and be careful with the analogWrite commands, should not interfere with the timer one function. Why not use two arduinos?

    • @JoaoVictor-ir3yu
      @JoaoVictor-ir3yu Před 4 lety

      @@raydike
      Thank you so much for answering me, I will try to do that

  • @mernamohamed1682
    @mernamohamed1682 Před 3 lety

    what's the type of dc motor ?

  • @joseolimpoocampomarquez2129

    hola necesito saber como conseguir los elementos

  • @Boikenny
    @Boikenny Před 7 lety

    Hello, thank you for the codes. I am very new in this field so forgive my ignorance in advance. When I tried to verify the code on Arduino, it gave me that error "fatal error: PinChangeInt.h: No such file or directory". What could be the reason for that?

    • @keesreedijk7613
      @keesreedijk7613 Před 7 lety +1

      You have to install the libraries first. Links see description. Put them in your libraries folder then restart the arduino software

    • @Boikenny
      @Boikenny Před 7 lety

      Thank you, that will solve my problem

  • @jitu1973
    @jitu1973 Před 6 lety

    Where to get the code?

  • @keshavkushwah4750
    @keshavkushwah4750 Před 2 lety

    Can you please help me to make a code for dmx stapper motor with encoder

  • @shermeentajammal352
    @shermeentajammal352 Před 4 lety

    what is model of ur dc motor
    and is it with encoder

    • @raydike
      @raydike  Před 4 lety

      Bühler motor with optical encoder

  • @devonrails3502
    @devonrails3502 Před 7 lety +1

    hey, can you post a link to the circuit sketch of the above experiment?Thank you!

  • @willy7346
    @willy7346 Před 4 lety

    Good Project. how to set degrees of dc motor 0-180? Like servo code (0 , 1023 , 0, 180) on your arduino code?

    • @raydike
      @raydike  Před 4 lety

      Output angle depends on the encoder resolution and the gearbox. If you know those then not too hard to calculate.

  • @swapnilchaitanya
    @swapnilchaitanya Před 7 lety

    Specified folder/zip file does not contain a valid library
    Why it is showing such message when i am including pinchangeInt-v2.19beta.zip

    • @raydike
      @raydike  Před 7 lety

      try unpack the library yourself and put it in the libraries folder

  • @adnansarfaraz6610
    @adnansarfaraz6610 Před 3 lety

    how we can control with two dc motors plz share code for two motors

  • @mounicasarakam9272
    @mounicasarakam9272 Před 6 lety

    Does this works with MOC7811 homemade quadrature encoder

    • @raydike
      @raydike  Před 6 lety

      No that's not a quadrature encoder, just an opto interrupter. You need two channels A and B

  • @user-yo4ku1ro6s
    @user-yo4ku1ro6s Před 6 měsíci

    But j have some question can you help me...

  • @Centraldeirrigacao_7927

    sorry raydick, it did not work, but thank you for the help I will be in search of another project thank you hug

  • @PranshuTople
    @PranshuTople Před 7 lety

    that was really very helpful. can you plz tell me how to do the same just with a dc motor without an encoder. i have same motor driver

    • @raydike
      @raydike  Před 7 lety

      Without an encoder there is no feedback so you cannot use the PID algorithm. To control the power to a motor without encoder just use the analogWrite function.

    • @PranshuTople
      @PranshuTople Před 7 lety

      raydike and what if I give feedback through another potentiometer that is connected to motor shaft. then what will be the code

    • @raydike
      @raydike  Před 7 lety +1

      Then use the analogRead value of your feedback pot instead of the encoder position value. Try it out, it should work, of course only for position not for speed.

    • @denisesilva6250
      @denisesilva6250 Před 5 lety

      got?

  • @farazjankhattak5719
    @farazjankhattak5719 Před 2 lety

    where the enable pin of l298 is connected with arduino?

    • @raydike
      @raydike  Před 2 lety

      enable pin is connected to +5v

  • @shahinaqadir7037
    @shahinaqadir7037 Před 6 lety

    i am doing final year project on it. i want to drive dc encoder motor with l298n using arduino without using potentionmeter and i want to control its speed and position .can you plz help us? i need coding and schematic diagram.

  • @sinanali4564
    @sinanali4564 Před 4 lety

    What's the function of potentiometer in this?

    • @raydike
      @raydike  Před 4 lety

      It's the thing I'm turning in the video, it provides an input signal for position or speed

  • @akshaybhat5493
    @akshaybhat5493 Před 7 lety

    can we do position control without encoder?

    • @raydike
      @raydike  Před 7 lety

      No, you need an encoder

  • @josef7108
    @josef7108 Před 6 lety

    Hello, would you have a drawing schematically linking?

    • @raydike
      @raydike  Před 6 lety +1

      It is in the description: schematic sketch here: i67.tinypic.com/sczu9t.jpg

    • @Javier_Corado
      @Javier_Corado Před 6 lety +1

      +raydike the link doesnot work

    • @raydike
      @raydike  Před 6 lety +1

      Hmm here it works fine, maybe try this link: ibb.co/j6iXK5

    • @josef7108
      @josef7108 Před 6 lety

      thank you so much

  • @atnguyenthich3844
    @atnguyenthich3844 Před 7 lety

    how can you control speed motor ?
    I can't do that.thank you!great help

  • @chemsedinemounia725
    @chemsedinemounia725 Před 5 lety

    i work L293 schema pleas

  • @sen_schoolofengineers
    @sen_schoolofengineers Před 6 lety

    I want code of this project

  • @akshaybhat5493
    @akshaybhat5493 Před 7 lety

    y is it called a PID motor

  • @sadettinbalcan1769
    @sadettinbalcan1769 Před 4 lety

    Hi,
    How can ı see the degree of position and rpm of speed on the Serial screen ?

    • @sadettinbalcan1769
      @sadettinbalcan1769 Před 4 lety

      Do your PID and pinExchange libraries work on Arduino Mega2560?

    • @raydike
      @raydike  Před 4 lety

      @@sadettinbalcan1769 yes

    • @raydike
      @raydike  Před 4 lety

      add some serial commands to the code.. degrees and rpm depends on motor, encoder and gearbox, you will have to do some calculations

  • @vishalsaha3488
    @vishalsaha3488 Před 6 lety

    Give me that Connection diagram and programming plzzzz

  • @ieee-musbasstpublicoor318

    I keep getting a constant speed, it doesnt slow down or anything help please

    • @raydike
      @raydike  Před 5 lety

      Did you try switch the motor connections? If you have an oscilloscope check the encoder outputs

    • @oquyen8760
      @oquyen8760 Před 5 lety

      Have you found a solution?I'm having a similar problem :((((

  • @UmarFarooq110
    @UmarFarooq110 Před 7 lety

    How you have decided the value Kp, Ki and Kd?

    • @raydike
      @raydike  Před 7 lety

      Start with Kp, then Ki and Kd. Mostly trial and error, aim for smooth movement, not too slow, not too tight, no oscillation.

    • @UmarFarooq110
      @UmarFarooq110 Před 7 lety

      so you just adjusted the values PID without knowing actual plant transfer function.
      am I right ?

    • @raydike
      @raydike  Před 7 lety

      That's correct. Not very scientific but good enough for my purposes.

    • @UmarFarooq110
      @UmarFarooq110 Před 7 lety

      and can you please elaborate how you were viewing the actual response.

    • @raydike
      @raydike  Před 7 lety

      Sure, if the feedback is too loose you get a sluggish response. Too much will give motor noise and oscillation, Ki will influence this too, and will make for accurate positioning. Just try and see what happens when you play with varying the constants.

  • @TheFaisalAli
    @TheFaisalAli Před 7 lety

    can you post your full code? (including the potentiometer etc.) thank you! great help

    • @raydike
      @raydike  Před 7 lety

      See description it is all there. Potmeter connected to A0

    • @TheFaisalAli
      @TheFaisalAli Před 7 lety

      Thank you for your help! Everything is working fine now except i have a question, how do you determine the frequency to stop the hissing sound. I am still recieving it with the 31khz. Best Regards,

    • @raydike
      @raydike  Před 7 lety

      I think you will need to adjust the Kp, Ki and Kd settings, lower the values a bit and experiment. Switching off will make the system lose the zero point, that is normal. You need to program some homing function with an end switch to get a fixed home (zero) position.

    • @TheFaisalAli
      @TheFaisalAli Před 7 lety

      Ill try that! thank you !

  • @mehmetcannzam1579
    @mehmetcannzam1579 Před 3 lety

    where is pcb ?

  • @joshcarz
    @joshcarz Před 6 lety

    The code does not take into account time and the pid loop runs every cycle. This makes it hard to add any other code without slowing everything down.

    • @raydike
      @raydike  Před 6 lety

      Do you have suggestions on how to improve the code?

  • @omkarjoshi6252
    @omkarjoshi6252 Před 4 lety

    hey can i control it directly from arduino rather than using that thing ?

    • @raydike
      @raydike  Před 4 lety

      I guess by 'that thing' you mean the H-bridge? The answer is no, you will blow up your arduino if you try.

    • @omkarjoshi6252
      @omkarjoshi6252 Před 4 lety

      @@raydike not h-bridge the regulator one ?

    • @raydike
      @raydike  Před 4 lety

      You mean the potentiometer? yes you can modify the program to receive for example serial position commands. Best use the updated software with interrupts

    • @omkarjoshi6252
      @omkarjoshi6252 Před 4 lety

      @@raydike can you share me code if possible ? It would be great

  • @luckyram7681
    @luckyram7681 Před 6 lety

    can we control pid speed and position control at a time

    • @raydike
      @raydike  Před 6 lety

      Speed control is just updating the position rapidly. The faster the position update the higher the speed.

    • @raydike
      @raydike  Před 6 lety

      And of course the bigger the step size in the position update the higher the speed. That is what is used here. The step size is determined by the position of the potentiometer.

    • @luckyram7681
      @luckyram7681 Před 6 lety

      Thaks for fast reply

  • @luckyram7681
    @luckyram7681 Před 6 lety

    haii ,i tried this but the motor was continuously rotating without stopping.
    hope you will reply soon thankyou

    • @raydike
      @raydike  Před 6 lety

      Try reverse the motor connections. If it still runs full speed your encoder is not working as it should

    • @luckyram7681
      @luckyram7681 Před 6 lety

      I tried reverse connection also but rotating other direction continuously.
      sir i will take video of my project and will send to you for mistake detection,can you give me your email id please

    • @raydike
      @raydike  Před 6 lety

      Sorry man I don't have time to troubleshoot your project, get yourself an oscilloscope and check your signals

    • @yldrmozer1598
      @yldrmozer1598 Před 2 lety

      I have the same problem

  • @Centraldeirrigacao_7927

    aydike my IDE is 1.0.5

  • @akshaywahile2048
    @akshaywahile2048 Před 6 lety +4

    Can you plzz give me that circuit diagram

    • @njdk7796
      @njdk7796 Před 5 lety

      it's already attached in the video description

  • @daviddeanda2505
    @daviddeanda2505 Před 5 lety

    Can you pass me the code? I need for my proyect pleaseeee

    • @raydike
      @raydike  Před 5 lety +2

      Can you not read? it's all in the description

  • @pandukamalalanayake3731

    can you put arduino codes as a link

    • @raydike
      @raydike  Před 7 lety +1

      github.com/raydike/PID_position_OK/blob/test1/PID_position_OK.ino
      github.com/raydike/PID_speed_OK/blob/master/PID_speed_OK.ino

  • @gagalsarjana6138
    @gagalsarjana6138 Před 3 lety

    Readi

  • @akshaybhat5493
    @akshaybhat5493 Před 7 lety

    can yu post the link to circuit diagram please

  • @sunitamahato9230
    @sunitamahato9230 Před 4 lety

    please sir send details of this project

    • @raydike
      @raydike  Před 4 lety

      It's all in the description

    • @JoaoVictor-ir3yu
      @JoaoVictor-ir3yu Před 4 lety

      I am trying to use pid for speed control, I am using a dc motor with integrated encoder of the same type as the video, to make a mobile robot ...

  • @sankyppp
    @sankyppp Před 4 lety

    Hi. I am trying to build an automatic clutch using a wiper motor for my old test car. It needs to perform the way you have shown in the first few secs. The potentiometer position determines the position of the clutch. Please confirm if the above project can be used for the functionality I am looking for.
    czcams.com/video/Ug5UqfGNsEA/video.html

    • @keesreedijk7613
      @keesreedijk7613 Před 4 lety +1

      In theory yes but you will need a motor with an encoder and a beefier H-bridge. I don't think you need feedback, just a couple of end switches and a strong enough motor will do. Or a linear actuator. Best keep it simple.

    • @sankyppp
      @sankyppp Před 4 lety

      @@keesreedijk7613 Thanks for the reply.

  • @user-yo4ku1ro6s
    @user-yo4ku1ro6s Před 6 měsíci

    Please🙏

  • @Centraldeirrigacao_7927

    hello friend congratulatiom can you send code complet idont get to compiler bibliotec

    • @raydike
      @raydike  Před 6 lety

      You have to put the PID and PinChange libraries in your libraries folder first then compile the software

    • @Centraldeirrigacao_7927
      @Centraldeirrigacao_7927 Před 6 lety

      ray thank . i cant help me to add i dont faund bibliotec can you send for my Email luiztelcom@hotmail.com code and bibliot tank you much

    • @raydike
      @raydike  Před 6 lety

      Here you can read how to work with libraries: www.arduino.cc/en/Guide/Libraries

    • @Centraldeirrigacao_7927
      @Centraldeirrigacao_7927 Před 6 lety

      sorry raydick, it did not work out thank you for the help I'll be looking for another project thank you

  • @user-yo4ku1ro6s
    @user-yo4ku1ro6s Před 6 měsíci

    Please

  • @alancordeiro3491
    @alancordeiro3491 Před 5 lety

    Hmm, it reminds me of PWM reather than PID

    • @raydike
      @raydike  Před 5 lety

      Then maybe you should look again, the PID calculations are done in the included PID library

  • @barsbayraktutan640
    @barsbayraktutan640 Před 5 lety

    Arduino and L298N have made common GND connection but the Arduino is getting very hot. Can you help me ?

  • @oguncanmunuklu9402
    @oguncanmunuklu9402 Před 5 lety

    Arduino and L298N have made common GND connection but the Arduino is getting very hot. can you help me ?

    • @raydike
      @raydike  Před 5 lety +1

      Check the voltage on the Arduino with a volt meter, if more than 5V then the wiring is wrong or the regulator chip on the L298 board is dead.