0% found this document useful (0 votes)
113 views4 pages

Control Speed Motor PID

This document provides instructions for controlling motor speed using a PID algorithm. It involves connecting a motor to an Arduino board programmed with PID control code. The Arduino code receives commands from a Visual Studio program to start/stop the motor and set the target speed. It calculates the motor's actual speed and uses PID parameters to determine the PWM pulse needed to minimize speed error. Running the Visual Studio program allows controlling the motor speed in real-time. More details on the Arduino and Visual Studio codes are provided in online links.

Uploaded by

Mas SE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views4 pages

Control Speed Motor PID

This document provides instructions for controlling motor speed using a PID algorithm. It involves connecting a motor to an Arduino board programmed with PID control code. The Arduino code receives commands from a Visual Studio program to start/stop the motor and set the target speed. It calculates the motor's actual speed and uses PID parameters to determine the PWM pulse needed to minimize speed error. Running the Visual Studio program allows controlling the motor speed in real-time. More details on the Arduino and Visual Studio codes are provided in online links.

Uploaded by

Mas SE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

instructables

Control Speed Motor PID

by AnnaMai

This is briefly instruction to control motor speed using PID algorithm. Also in this project, motor speed will be print
out at computer screen

https://www.youtube.com/watch?v=QNCWW4TX4Gg

Step 1:

Connect motor to Arduino through H-bridge motor_start = true;

Program Arduino as: }

void loop() { if (mySt.substring(0,7) == "vs_stop"){


if (stringComplete) {
digitalWrite(pin_fwd,0);
// clear the string when COM receiving is completed
digitalWrite(pin_bwd,0); //stop motor
mySt = ""; //note: in code below, mySt will not
become blank, mySt is blank until '\n' is received motor_start = false;

stringComplete = false; }

} if (mySt.substring(0,12) == "vs_set_speed"){

//receive command from Visual Studio set_speed =


mySt.substring(12,mySt.length()).toFloat(); //get string
if (mySt.substring(0,8) == "vs_start"){ after set_speed

digitalWrite(pin_fwd,1); //run motor run forward }

digitalWrite(pin_bwd,0); if (mySt.substring(0,5) == "vs_kp"){

Control Speed Motor PID: Page 1


kp = mySt.substring(5,mySt.length()).toFloat(); //get Visual Studio
string after vs_kp
}
}
//PID program
if (mySt.substring(0,5) == "vs_ki"){
if (motor_start){
ki = mySt.substring(5,mySt.length()).toFloat(); //get
string after vs_ki e_speed = set_speed - pv_speed;

} pwm_pulse = e_speed*kp + e_speed_sum*ki +


(e_speed - e_speed_pre)*kd;
if (mySt.substring(0,5) == "vs_kd"){
e_speed_pre = e_speed; //save last (previous) error
kd = mySt.substring(5,mySt.length()).toFloat(); //get
string after vs_kd e_speed_sum += e_speed; //sum of error

} if (e_speed_sum >4000) e_speed_sum = 4000;

} if (e_speed_sum <-4000) e_speed_sum = -4000;

void detect_a() { }

encoder+=1; //increasing encoder at new pulse else{

m_direction = digitalRead(pin_b); //read direction of e_speed = 0;


motor
e_speed_pre = 0;
}
e_speed_sum = 0;
ISR(TIMER1_OVF_vect) // interrupt service routine -
tick every 0.1sec pwm_pulse = 0;

{ }

TCNT1 = timer1_counter; // set timer //update new speed

pv_speed = 60.0*(encoder/200.0)/0.1; //calculate if (pwm_pulse <255 & pwm_pulse >0){


motor speed, unit is rpm
analogWrite(pin_pwm,pwm_pulse); //set motor speed
encoder=0;
}
//print out speed
else{
if (Serial.available() <= 0) {
if (pwm_pulse>255){
Serial.print("speed");
analogWrite(pin_pwm,255);
Serial.println(pv_speed); //Print speed (rpm) value to

Control Speed Motor PID: Page 2


} }

else{ }

analogWrite(pin_pwm,0); }

Step 2: Work at Visual Studio C++

Coding in Visual Studio C++ is quite long, see code at this link

https://drive.google.com/file/d/0B1UeJfx4pZ1GV1YzN1k0aElPb2c/view?usp=sharing

Step 3: RUN It

Run it with speed as you wish

See more instruction at our blog: http://engineer2you.blogspot.com/2016/12/arduino-motor-pid-speed-control.html

Control Speed Motor PID: Page 3


Control Speed Motor PID: Page 4

You might also like