Arduino PID DC Brushed Motor CNC Axis Control
Arduino PID DC Brushed Motor CNC Axis Control
(eng_arduino_tut103.php)
IN this tutorial we will see how to control one axis but using BRUSHED DC
MOTORS. Usually CNC axis will move using stepper motors in an open loop
control. Here we will see an example of close loop control with an optic
encoder as feedback. Why do this? well, for bigger motors, the price of
stepper motors and drivers could get very high. Using DC brushed motors
with a gear box and close loop control could get cheaper. Lets see how to do
this.
Tonton di
Authorized Distributor
I will make just only one axis. See full part list also here on this link
(eng_arduino_tut102_parts1). For that we need the parts below. The motor
with the encoder in quadrature, the H bridge driver, the Arduino with a
potentiometer and the rest of components. For the motor you could get only
the motor and then make your own gear system or buy directly the motor
witha gear box. See the scheamtic for all the connections.
We need:
PART 2 - Schematic
(eng_arduino_tut102_sch1.php)
PART 3 - Code
Copy the code from below or download it from the link. You will need to install
the needed libraries. Go to the link below and find those libraries and install
them to your Arduino IDE. Read all the lines in the code to understand more.
The setpoint is given with the potentiometer but you could use Serial read for
example and give precise values for the setpoint and see the results.
////////////////////////////////////////////////////
//////////
/////////////////////DEFINE PID constant
s/////////////////////
////////////////////////////////////////////////////
//////////
int kp = 2, ki = 0.01, kd = 0.02;
////////////////////////////////////////////////////
//////////
////////////////////////////////////////////////////
//////////
////////////////////////////////////////////////////
//////////
////////////////////Define the pins we us
e////////////////////
////////////////////////////////////////////////////
//////////
#define Encoder_A 2
// Quadrature encoder A pin
#define Encoder_B 8
// Quadrature encoder B pin
#define Motor_CW 11
// PWM outputs to L298N H bridge motor driver module
#define Motor_CCW 3
#define led 13
#define END_stop 4
void setup(void)
{
pinMode(Encoder_A, INPUT);
// quadrature encoder input A
pinMode(Encoder_B, INPUT);
// quadrature encoder input B
pinMode(END_stop, INPUT); Ad
10K Ohm With Turn Counting Dial Rotary Pote
(https://www.ebay.com/itm/16218559
3590S-2-103L (https://www.ebay.com/itm/16
// Input from the end stop switch $2.68
mkcid=1&mkrid=711- sold by atom.2014
attachPCINT(digitalPinToPCINT(Encoder_A), encoder,
FALLING); // We update encoder position each fallin
g edge detected in the interrumption
TCCR2B = TCCR2B & 0b11111000 | 1;
// set 31Kh PWM to prevent motor whine (timer 2)
Timer1.initialize(sampleTime * 1000);
// setup timer 1
Timer1.attachInterrupt(Compute);
Serial.begin(115200);
//Just for debugging if you want to print values
while(digitalRead(END_stop))
{
analogWrite(Motor_CW, 0);
// Rotate the motor CCW
analogWrite(Motor_CCW, 255);
}
analogWrite(Motor_CW, 0);
// Stop the motor
analogWrite(Motor_CCW, 0);
}
void Compute()
{
setpoint = map(analogRead(0),0,1024,1024,0) * 110;
// setpoint position is made with a potentiometer bu
t could be given by serial monitor or other...
input = encoderPos;
// we get the data from the encoder interrumption
error = setpoint - input;
iTerm += ki * error * sampleTime;
if (iTerm > outMax) iTerm = outMax;
// prevent that the I term from PID gets too big
else if (iTerm < outMin) iTerm = outMin;
dInput = (input - lastInput) / sampleTime;
output = kp * error + iTerm - kd * dInput;
// The PID output is the sum of P I and D values
if (output > outMax) output = outMax;
// limit output to 0 and 255 for the analog write
else if (output < outMin) output = outMin;
lastInput = input;
//Remember to save the last input value for the next
loop
pwmOut(output);
//Change the analog write for the motor control
}
void encoder() {
// pulse and direction, direct port reading to save
cycles
if (PINB & 0b00000001) encoderPos++;
// if(digitalRead(encodPinB1)==HIGH) count ++; //W
e increase steps by 1
else encoderPos--;
// if (digitalRead(encodPinB1)==LOW) count --; //W
e decrease steps by 1
}
void loop(void)
{
digitalWrite(led, !digitalRead(led));
// blink led or do something else...
delay(200);
}
PART 4 - Video
See the video for more information and see what options you have for the
encoder, how to detect the pulses and so on. I hope you will like this project.
Comment below and help the community. COnsider supporting me on
PATREON (https://www.patreon.com/ELECTRONOOBS).
Ad
10K Ohm With Turn Counting Dial Rotary Pote
One axis PID encoded DC motor control (https://www.ebay.com/itm/16218559
3590S-2-103L (https://www.ebay.com/itm/16
Electronoobs $2.68
mkcid=1&mkrid=711-
sold by atom.2014
100% Postive feedback
53200-
Tonton di
(https://www.youtube.com/c/ELECTRONOOBS)
(https://www.instagram.com/ELECTRONOOBS)
(https://www.facebook.com/Electronoobs)
(https://twitter.com/ELECTRONOOBS)
DC motor PID
page 1/1
Tonton di
(https://www.pcbway.es/?from=electronoobs.com00)
Ad
10K Ohm With Turn Counting Dial Rotary Pote
(https://www.ebay.com/itm/16218559
3590S-2-103L (https://www.ebay.com/itm/16
$2.68
mkcid=1&mkrid=711-
sold by atom.2014
100% Postive feedback
53200-
(https://s.click.aliexpress.com/e/_9jqPR0?bz=300*250)
(https://bit.ly/2ZNWgqy)
Ad
10K Ohm With Turn Counting Dial Rotary Pote
(https://www.ebay.com/itm/16218559
3590S-2-103L (https://www.ebay.com/itm/16
$2.68
mkcid=1&mkrid=711-
sold by atom.2014
100% Postive feedback
53200-
(https://s.click.aliexpress.com/e/_AF7EeE?bz=500*500)
Ad
10K Ohm With Turn Counting Dial Rotary Pote
(https://www.ebay.com/itm/16218559
3590S-2-103L (https://www.ebay.com/itm/16
$2.68
mkcid=1&mkrid=711-
sold by atom.2014
100% Postive feedback
53200-
(https://bit.ly/2ZNWgqy)
Tonton di
Tonton di
TUTORIALS
Arduino (eng_tutoriales_arduino.php)
Circuits (eng_tutoriales_circuitos.php)
Robotics (eng_tutoriales_robotica.php)
Arduino Course (https://electronoobs.io/shop/index.php/courses/curso-arduino-desde-
cero-nivel-1)
Blogs (blogs.php)
SUPPORT
Terms and Conditions (terms_and_conditions.php)
Privacy Policy (privacy_policy.php)
Contact me (eng_contacto_mobile.php)
Forum (https://electronoobs.io/forum)
ADVERTISERS
Donate (eng_donate.php)
Reviews (eng_reviews.php)
Advertising (eng_advertising.php)