Arduino PID motor position and speed control
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
Thank you for sharing all the details of this solution. Superb work!
Congratulations and thank you for sharing. 👏 👌 👏
Very nice indeed. I'll make this, thank you kindly.
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.
Grate video sir
What reference is the engine? In what electronic device can I find it to recycle?
Thank you so much!
What is the model of the enconder?
Why in the code do you have 2 motors?M1 and M2?
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.
Great project, but what kind of motor and encoder are you using? And how those two are put together into one?
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
Thanks!
Thank You
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
yes proportional to encoder resolution.
Arduino and L298N have made common GND connection but the Arduino is getting very hot. Can you help me ?
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.
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?
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.
Thanks for your work bro but we want to know how did you do for getting a model of system ? ( transfer function )
PID CONTROL
hi thank you for your great video, where you have determine the potentiometer in your code?
The potentiometer is connected to A0 of the Arduino
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?
I made a new, interrupt driven version: czcams.com/video/VkvO8l95RDo/video.html this will be easier to modify for your needs
best
Thanks for share
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!
hello I would like to know, what is the engine reference?
Never mind - just upgraded the IDE
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?
Check everything methodically. Do you get the right encoder signals, is your H-bridge wired properly is your motor ok etc.
What are you using for an encoder?
It's just an optical encoder that came with the motor. I guess about 100 pulses per revolution.
Hello Ray, i finally get it works motor is running,, but unable to control speed via PoT, could you advise ?
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
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.
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.
Thank you.
@@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?
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
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!!!
what us the significance of this line " setpoint = analogRead(0) * 5".It is not used in further code anywhere. Will you please explain.
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.
@@raydike Thanks Raydike, I missed that it is an argument in PID function. Thanks again.
I can't compile w/o errors, error say something about PID
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 ?
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.
works fine with the jumper thanks
ENA to 5v supply
check the code there is an option with a comment like // try ???? is turn continuously
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)
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?
@@raydike
Thank you so much for answering me, I will try to do that
what's the type of dc motor ?
hola necesito saber como conseguir los elementos
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?
You have to install the libraries first. Links see description. Put them in your libraries folder then restart the arduino software
Thank you, that will solve my problem
Where to get the code?
Can you please help me to make a code for dmx stapper motor with encoder
what is model of ur dc motor
and is it with encoder
Bühler motor with optical encoder
hey, can you post a link to the circuit sketch of the above experiment?Thank you!
Thank you!!
Good Project. how to set degrees of dc motor 0-180? Like servo code (0 , 1023 , 0, 180) on your arduino code?
Output angle depends on the encoder resolution and the gearbox. If you know those then not too hard to calculate.
Specified folder/zip file does not contain a valid library
Why it is showing such message when i am including pinchangeInt-v2.19beta.zip
try unpack the library yourself and put it in the libraries folder
how we can control with two dc motors plz share code for two motors
Does this works with MOC7811 homemade quadrature encoder
No that's not a quadrature encoder, just an opto interrupter. You need two channels A and B
But j have some question can you help me...
sorry raydick, it did not work, but thank you for the help I will be in search of another project thank you hug
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
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.
raydike and what if I give feedback through another potentiometer that is connected to motor shaft. then what will be the code
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.
got?
where the enable pin of l298 is connected with arduino?
enable pin is connected to +5v
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.
Im doing the same.Maybe u can help me.
hi can you find ?
What's the function of potentiometer in this?
It's the thing I'm turning in the video, it provides an input signal for position or speed
can we do position control without encoder?
No, you need an encoder
Hello, would you have a drawing schematically linking?
It is in the description: schematic sketch here: i67.tinypic.com/sczu9t.jpg
+raydike the link doesnot work
Hmm here it works fine, maybe try this link: ibb.co/j6iXK5
thank you so much
how can you control speed motor ?
I can't do that.thank you!great help
use the link to the speed control firmware
thanks you!
i work L293 schema pleas
I want code of this project
y is it called a PID motor
Hi,
How can ı see the degree of position and rpm of speed on the Serial screen ?
Do your PID and pinExchange libraries work on Arduino Mega2560?
@@sadettinbalcan1769 yes
add some serial commands to the code.. degrees and rpm depends on motor, encoder and gearbox, you will have to do some calculations
Give me that Connection diagram and programming plzzzz
I keep getting a constant speed, it doesnt slow down or anything help please
Did you try switch the motor connections? If you have an oscilloscope check the encoder outputs
Have you found a solution?I'm having a similar problem :((((
How you have decided the value Kp, Ki and Kd?
Start with Kp, then Ki and Kd. Mostly trial and error, aim for smooth movement, not too slow, not too tight, no oscillation.
so you just adjusted the values PID without knowing actual plant transfer function.
am I right ?
That's correct. Not very scientific but good enough for my purposes.
and can you please elaborate how you were viewing the actual response.
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.
can you post your full code? (including the potentiometer etc.) thank you! great help
See description it is all there. Potmeter connected to A0
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,
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.
Ill try that! thank you !
where is pcb ?
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.
Do you have suggestions on how to improve the code?
hey can i control it directly from arduino rather than using that thing ?
I guess by 'that thing' you mean the H-bridge? The answer is no, you will blow up your arduino if you try.
@@raydike not h-bridge the regulator one ?
You mean the potentiometer? yes you can modify the program to receive for example serial position commands. Best use the updated software with interrupts
@@raydike can you share me code if possible ? It would be great
can we control pid speed and position control at a time
Speed control is just updating the position rapidly. The faster the position update the higher the speed.
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.
Thaks for fast reply
haii ,i tried this but the motor was continuously rotating without stopping.
hope you will reply soon thankyou
Try reverse the motor connections. If it still runs full speed your encoder is not working as it should
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
Sorry man I don't have time to troubleshoot your project, get yourself an oscilloscope and check your signals
I have the same problem
aydike my IDE is 1.0.5
Can you plzz give me that circuit diagram
it's already attached in the video description
Can you pass me the code? I need for my proyect pleaseeee
Can you not read? it's all in the description
can you put arduino codes as a link
github.com/raydike/PID_position_OK/blob/test1/PID_position_OK.ino
github.com/raydike/PID_speed_OK/blob/master/PID_speed_OK.ino
Readi
can yu post the link to circuit diagram please
please sir send details of this project
It's all in the description
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 ...
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
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.
@@keesreedijk7613 Thanks for the reply.
Please🙏
hello friend congratulatiom can you send code complet idont get to compiler bibliotec
You have to put the PID and PinChange libraries in your libraries folder first then compile the software
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
Here you can read how to work with libraries: www.arduino.cc/en/Guide/Libraries
sorry raydick, it did not work out thank you for the help I'll be looking for another project thank you
Please
Hmm, it reminds me of PWM reather than PID
Then maybe you should look again, the PID calculations are done in the included PID library
Arduino and L298N have made common GND connection but the Arduino is getting very hot. Can you help me ?
Arduino and L298N have made common GND connection but the Arduino is getting very hot. can you help me ?
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.