Full width home advertisement

Arduino project

Raspberry project

Post Page Advertisement [Top]

With PID control, speed of motor can be archived exactly. This article mainly introduces about making program in Arduino Pro mini, program in Computer (Visual Studio) to control motor speed by PID algorithm.




Fig1. General connection

Arduino Pro mini is used to store motor control, PID algorithm, communicating with PC (through COM Port)
Computer will have HMI made by Visual Studio to communicate with Arduino. HMI will show motor speed graph, change motor speed setting
Fig2. HMI on Computer (made by Visual Studio)

Let's go detail:
Hardware needed:
1. Motor with Encoder
2. H-bridge PCB
3. Arduino Pro mini
4. UART PCB
5. Computer (with Visual Studio)
1. Motor with encoder

2. H-bridge

3. Arduino Pro Mini




Step 1. Hardware connection
This article doesn't mention about detail connection, it is based on previous article which can be found at this link




Step 2. Code works at Arduino
At the beginning of program, it will receive command from Computer (start/stop motor, motor speed setting, kP, kI, kD gain of PID)
Next is void detect_a(): encoder sum calculation -> used for speed calcluation in Timer interrupt routine.
Timer interrupt routine ISR(TIMER1_OVF_vect): every 0.1 this program is called, content includes:
   (1) Calculate motor speed
   (2) Send motor speed to Computer
   (3) Calculate PWM pulse (base on PID algorithm)
   (4) Push result of PWM to H-brigde
The entire of code for Arduino Pro mini can be download at this link

Step 3. Code works at Computer
Visual Studio 2012 is used to make HMI program, in which:
   (1) Send speed setting to Arduino
   (2) Send PID gain (kP, kI, kD) to Arduino
   (3) Receive motor speed -> show on graph
Fig3. Visual studio program

The whole code of Visual Studio program can be downloaded at this link
To make a Visual Studio program -> see detail step at this article
In general, the code will have:
#pragma endregion

 private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {

     serialPort1->Open();

     timer1->Start();

     mStr = "0";

     i=300;

    }

 private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

     serialPort1->WriteLine("vs_set_speed"+textBox1->Text); //send set_speed to Arduino

     serialPort1->WriteLine("vs_kp"+textBox2->Text); //send kP to Arduino

     serialPort1->WriteLine("vs_ki"+textBox3->Text); //send kI to Arduino

     serialPort1->WriteLine("vs_kd"+textBox4->Text); //send kD to Arduino

    }

 private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {

     String^ length;

     length=mStr->Length.ToString();

     if(mStr->Substring(0,5)=="speed"){

      speed=mStr->Substring(5,System::Convert::ToInt32(length)-6);

      label1->Text=speed;

      //print motor speed into Chart

      this->chart1->Series["Series1"]->Points->AddXY(i,System::Convert::ToDouble(speed));

      i++;

      this->chart1->ChartAreas["ChartArea1"]->AxisX->Minimum=i-300; //shift x-axis

     }

   }

 private: System::Void serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e) {

    mStr=serialPort1->ReadLine();

   }

private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {

    serialPort1->WriteLine("vs_start"); //start motor

   }

private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {

    serialPort1->WriteLine("vs_stop");  //stop motor

   }



Result video:




42 comments:

  1. Thanks, You wrote awesome, I have learn lots of things from your article. It's really helpful for any readers.
    Battery Operated Flow Meter

    ReplyDelete
  2. Will it work on arduino uno

    ReplyDelete
    Replies
    1. so we just didn't need the UART PCB and we don't need to edit arduino code even when using arduino uno?

      sorry to bother you but i really need a fast answer

      Delete
    2. Right, Arduino UNO already had UART PCB inside. When Arduino UNO connect to Computer via USB Port, an COM Port will be shown -> use this COM Port No. to communicate between Arduino UNO and Computer.
      About the code, i try to compile for UNO, it run perfectly. Let's try it!

      Delete
  3. How can i try to input the value of speed, kp, ki, kd by using serial monitor in arduino program?

    thank you
    Fungkyking01@gmail.com

    ReplyDelete
    Replies
    1. Hi. Unfortunately i already removed this model (motor, circuit,...) -> can't make project again as your mention.
      You should find an example to read data input from serial port -> then, import parameters (kP, kI, kD) to formular. It's not difficult, let's do it

      Delete
  4. Bro Can you kindly Provide the whole circuit diagram. its quite confusing which connection goes where. thnx in advance

    ReplyDelete
  5. Please come to thí link to see connection
    http://engineer2you.blogspot.com/2016/12/arduino-motor-speed-control-pwm.html?m=1

    ReplyDelete
  6. Bro I followed all the steps you mentioned and made the project with Arduino Uno. The problem I get is that the PID is not working, the motor keeps on rotating with a constant speed of 530 RPM and when I apply some load to the motor the speed reduces and does not go back to 538 RPM. Also I can't send commands to arduino. like i want the motor to rotate with a speed for 700 RPM and 300 RPM for example, it does not work when i press the send button on HMI.

    ReplyDelete
    Replies
    1. Hi,
      (1) First step is make sure you can send command (and convert it to integer) into Arduino. You can search on web this kind of project, or see here http://engineer2you.blogspot.com/2016/12/arduino-uart-communication.html
      (2) Tuning PID parameter is not easy. Each motor will have each parameter. First tuning, you should make kP, kI some small value, kD=0, then see motor reaction. Then gradually increase or decrease kP, kI. Normally, no need kD.

      Delete
  7. Is there an equivalent motor that can be used with this system, it is very difficult to find the one you used.

    ReplyDelete
    Replies
    1. You can use any DC motor, then find kP kI kD for it

      Delete
  8. Hi this is one s brilliant!! I'm want to design kind of this but slightly different..
    There is LCD or 7 segment using the rotary encoder rpm 0-8000 per 1 rpm changes, first we need to a rpm then once after pressing start arduino need to rpm of the motor
    Can you suggest me any enocder motor up to 8000rpm n how to control the motor?
    Help me pls

    ReplyDelete
    Replies
    1. Hi, There's rearly motor with encoder 8000 ppr (pulse per round). If you need 8000ppr encoder, you have to use external encoder, connect to motor shaft by coupling. I was wondering if what application you need 8000ppr encoder? Could you share? We will make it together (because i'm free now haha)

      Delete
  9. I want to draw Set Speed in chart with series 2 is color Red, Can you help me

    ReplyDelete
    Replies
    1. Please try yourself in Visual Studio. I think it's not difficult.

      Delete
    2. Set Speed in Textbox I run Series2 but i don't know convert Text

      Delete
    3. /this->chart1->Series["Series2"]->Points->AddXY(i,System::Convert::ToDouble(textBox1)); i++;
      // this->chart1->ChartAreas["ChartArea1"]->AxisX->Minimum=i;

      Delete
    4. Thank you: i did convert textbox1->text ok

      Delete
  10. Can you please guide me about the specifications of motor with encoder? And also where to buy it from in INDIA?

    ReplyDelete
    Replies
    1. Hi, you can buy any DC motor which has encoder inside.

      Delete
  11. Hey , can you explain to me how to convert speed RPM to PWM ?

    ReplyDelete
    Replies
    1. Hi, i don't know calculation to convert speed rpm to PWM. This calculation is difficult, depends on characteristic of motor, load.
      Instead of calculation speed to PWM, we use PID algorithm. How it works?
      Let's think, PWM=0% -> speed is 0rpm; PWM=100% -> speed is max, let's say 2000rpm
      PID will calculate PWM depend on error of speed feedback and speed setting -> output to PWM pulse to reach setting speed.
      Hope i can help you.

      Delete
  12. Hi
    thanks for sharing this brilliant works . I have couples of question , first what version of visual studio you use and if I have motor with 2 wires only and it's gear motor , do u think that i still need encoder cuz I want to control the speed by using ur way in where if I need 450 rpm and the motor should rotate the desired speed , Do u think my motor able to do , here is my motor characters https://www.alibaba.com/product-detail/VTV-24v-dc-motor-1500-rpm_60645604396.html?spm=a2700.7724838.2017115.195.2bc1680aS46x0Z
    thank you so much

    ReplyDelete
    Replies
    1. Hi,
      I use Arduino 1.6.11, but you can use latest version -> it's no problem
      Your suggested motor has no encoder -> impossible to control speed. Without encoder, how could we know speed? You should choose another motor with encoder.
      Thanks for visit my blog.

      Delete
  13. This comment has been removed by the author.

    ReplyDelete
  14. how i can calculate the parameter of dc motor (j.B.L.)?
    2-Are you can send me code of pid controller without using parameters(j.B.L)?

    ReplyDelete
    Replies
    1. Hi, i don't calculate parameter of DC Motor
      The code of PID controller (without using parameter DC Motor) is in my blog. Please read it all.
      Thanks for visit.

      Delete
  15. ok..
    but are you have any away to calculate parameter of dc by matlab ?

    ReplyDelete
    Replies
    1. Hi, i know your mean: using calculated parameter to make PID parameter. Unfortunately, this project is not aimed to do that, this just tuned PID by experience and make it run.

      Delete
  16. Hola, ill be making this the next week,i hope ull be here for my questions,and i want to thank you for sharing this project with the internet :)

    ReplyDelete
    Replies
    1. Sure. Lets keep in touch here

      Delete
    2. i didnt find near me the needed equipments, so can i use the same concept with Arduino UNO +ESC 30A + A brushless motor ?

      Delete
    3. Sure, but you must have a way to let Arduino UNO know speed of brushless motor.

      Delete
    4. Okey, do you have any suggestions how i do that?

      Delete
    5. Please search on Google the way how to read speed of brushless motor

      Delete
  17. good morning, i have the next problem Excepción no controlada del tipo 'System.FormatException' en mscorlib.dll could you help me?, please

    ReplyDelete
    Replies
    1. Hi, please make program with Visual Studio step by step, then try to find where error comes from

      Delete

Bottom Ad [Post Page]