0% found this document useful (0 votes)
18 views15 pages

Arduino PID DC Brushed Motor CNC Axis Control

This tutorial explains how to control a single axis using brushed DC motors with closed-loop control via an optical encoder, as a cost-effective alternative to stepper motors. It includes a parts list, schematic connections, and Arduino code for implementation. The project aims to provide a comprehensive guide for building the motor control system, including necessary components and coding instructions.

Uploaded by

DavinYudika
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)
18 views15 pages

Arduino PID DC Brushed Motor CNC Axis Control

This tutorial explains how to control a single axis using brushed DC motors with closed-loop control via an optical encoder, as a cost-effective alternative to stepper motors. It includes a parts list, schematic connections, and Arduino code for implementation. The project aims to provide a comprehensive guide for building the motor control system, including necessary components and coding instructions.

Uploaded by

DavinYudika
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/ 15

Panel Cookies

PID DC BRUSHED MOTOR CONTROL

Help me by sharing this post

PREVIOUS TUTORIAL (eng_arduino_tut101.php) NEXT TUTORIAL

(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.

One axis PID encoded DC motor control


Electronoobs

Tonton di

Authorized Distributor

P&P Process & Pneumatics Open


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-

PART 1 - What we need

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:

1 x Arduino NANO/UNO: LINK eBay (https://www.ebay.com/itm/For-Arduino-UNO-R3-Atmega16U2-Expansion-


Board-Microcontroller-Development-Board/163262286994?
_trkparms=ispr%3D1&hash=item260330cc92:g:EiUAAOSwq~Jbny5q&enc=AQAEAAAB4BPxNw%2BVj6nta7CKEs3N0qW
53200-19255-0&siteid=0&customid=link&campid=5338106513&toolid=20001&mkevt=1)

1 x DC motor with encoder: LINK eBay (https://www.ebay.com/itm/130-


Motor-DC6V-12V-7800RPM-Metal-Speed-Encoder-Tachometer-Motor-AB-
Phase-EF/293143757827?
epid=18003843567&_trkparms=ispr%3D1&hash=item4440bac403:g:09oAAOSwEMdc77gz&enc=AQAEAAAB4BPxNw%
53200-19255-
0&siteid=0&customid=link&campid=5338106513&toolid=20001&mkevt=1)

1 x H bridge L298N: LINK eBay (https://www.ebay.com/itm/new-L298N-DC-Stepper-Motor-Driver-Module-Dual-H-


Bridge-Control-Board-for-Arduino/281684154507?
epid=21024969493&_trkparms=ispr%3D1&hash=item4195af188b:g:3BQAAOSwstxVSgiL&enc=AQAEAAAB0BPxNw%2B
53200-19255-0&siteid=0&customid=link&campid=5338106513&toolid=20001&mkevt=1)

1 x end stop SWITCH: LINK eBay (https://www.ebay.com/itm/5pcs-Mechanical-End-Stop-Endstop-Limit-Switch-


Cable-For-3D-Printer-RAMPS-1-4/282871404400?
_trkparms=ispr%3D1&hash=item41dc731770:g:zKIAAOSw~Yxamjaw&enc=AQAEAAAB0BPxNw%2BVj6nta7CKEs3N0q
53200-19255-0&siteid=0&customid=link&campid=5338106513&toolid=20001&mkevt=1)

1 x Potentiometer 10 turns: LINK eBay (https://www.ebay.com/itm/10K-Ohm-


With-Turn-Counting-Dial-Rotary-Potentiometer-Pot-10-Turn-3590S-2-
103L/162185599516?
_trkparms=ispr%3D1&hash=item25c303da1c:g:JrYAAOSwWMNbrEkf&enc=AQAEAAAB4BPxNw%2BVj6nta7CKEs3N0q
53200-19255-
0&siteid=0&customid=link&campid=5338106513&toolid=20001&mkevt=1)

2 x smooth rod 8mm: LINK eBay (https://www.ebay.com/itm/8mm-3D-Printer-Axis-Chromed-Smooth-Rod-Steel-


Linear-Rail-Shaft-8-kind-length/254234888867?_trkparms=ispr%3D1&hash=item3b31948aa3:m:mi--
vKPFqMfOC4ob207UJBQ&enc=AQAEAAAB0BPxNw%2BVj6nta7CKEs3N0qUEZlPPEEzJ02DIsBre8q8nFjyeQW1h6XACh
53200-19255-0&siteid=0&customid=link&campid=5338106513&toolid=20001&mkevt=1)
Ad
10K Ohm With Turn Counting Dial Rotary Pote
(https://www.ebay.com/itm/16218559
1 x lead screw: LINK eBay (https://www.ebay.com/itm/T8-lead-screw-Pitch1-2mm-Lead-1-2-4-8mm-Rod-Stainless-
3590S-2-103L (https://www.ebay.com/itm/16
$2.68
Lead-Screw-brass-color-nut/272356600799? mkcid=1&mkrid=711- sold by atom.2014
100% Postive feedback
53200-
_trkparms=ispr%3D1&hash=item3f69b7ebdf:m:maZjziQnQT3BlSz1rOVjdvg&enc=AQAEAAAB0BPxNw%2BVj6nta7CKEs
53200-19255-0&siteid=0&customid=link&campid=5338106513&toolid=20001&mkevt=1)

4 x lineal bearing LM8UU: LINK eBay (https://www.ebay.com/itm/Linear-Deep-


Groove-Ball-Bearing-LM6UU-623ZZ-KFL08-Mounted-Block-Lager-
Bush/183550010052?
_trkparms=ispr%3D1&hash=item2abc6ee2c4:m:mMlUm9KTIsnAZTiMved8bIg&enc=AQAEAAAB4BPxNw%2BVj6nta7CK
53200-19255-
0&siteid=0&customid=link&campid=5338106513&toolid=20001&mkevt=1)

1 x STL 3D files: LINK eBay (images/Arduino/tut_102/3D_files.zip)

1 x GEARS: LINK eBay (https://www.ebay.com/itm/82-pcs-Plastic-Gear-Package-Kit-DIY-Gear-Assortment-


accessories-set-for-Toy-Car/253767385641?
_trkparms=ispr%3D1&hash=item3b15b70229:g:EAEAAOSwHola2y2y&enc=AQAEAAAB0BPxNw%2BVj6nta7CKEs3N0qV
53200-19255-0&siteid=0&customid=link&campid=5338106513&toolid=20001&mkevt=1)

or motor directly with gear: LINK eBay (https://www.ebay.com/itm/DC-12V-


27RPM-High-Torque-Reversible-Encoded-Worm-Gear-Motor-
Reduction/264289337544?
hash=item3d88df40c8:g:l8IAAOSwCQ5cuAOW&mkcid=1&mkrid=711-53200-
19255-
0&siteid=0&customid=link&campid=5338106513&toolid=20001&mkevt=1)

Extra wire, solder, soldering-iron, breadboard, etc

I've mounted everything on a plywood baord. We also need some M3 screws


for that or maybe some glue. Some lubrificant for the rods and lead screw
also would be good.
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-

PART 2 - Schematic

Make the connections as below. Supply a different connection to the H bridge


with enough current. Remember to share GND with all the modules. The End
stop swithc will need a pullup and give a low pulse when the siwthc is
pressed, otherwise you will need to change that in the code. The encoder
needs 5V, GND and two encoder outputs.

(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.

Download transmitter code  (eng_arduino_tut102_code1.php)


Ad
10K Ohm With Turn Counting Dial Rotary Pote
(https://www.ebay.com/itm/16218559
//Include the libraries we need 3590S-2-103L (https://www.ebay.com/itm/16
$2.68
mkcid=1&mkrid=711-
#include <PinChangeInterrupt.h> sold by atom.2014
100% Postive feedback
53200-
#include <PinChangeInterruptBoards.h>
#include <PinChangeInterruptPins.h>
#include <PinChangeInterruptSettings.h>
#include <TimerOne.h> // Ti
mer one interrupts

////////////////////////////////////////////////////
//////////
/////////////////////DEFINE PID constant
s/////////////////////
////////////////////////////////////////////////////
//////////
int kp = 2, ki = 0.01, kd = 0.02;
////////////////////////////////////////////////////
//////////
////////////////////////////////////////////////////
//////////

int input, output, setpoint;


int iTerm = 0, lastInput = 0, dInput = 0, error = 0;
int outMin = -255, outMax = 255;
int sampleTime = 10;
//This values is is milliseconds
volatile long encoderPos = 0;

////////////////////////////////////////////////////
//////////
////////////////////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

pinMode(led, OUTPUT); 53200-


100% Postive feedback

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 pwmOut(int out) {


// to H-Bridge board Ad
10K Ohm With Turn Counting Dial Rotary Pote
(https://www.ebay.com/itm/16218559
3590S-2-103L (https://www.ebay.com/itm/16
if (out > 0) { $2.68
mkcid=1&mkrid=711-
sold by atom.2014

analogWrite(Motor_CW, out); 53200-


100% Postive feedback

// Rotate the motor CW


analogWrite(Motor_CCW, 0);
}
else {
analogWrite(Motor_CW, 0);
analogWrite(Motor_CCW, abs(out));
// Rotate the motor CCW
}
}

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

PREVIOUS TUTORIAL (eng_arduino_tut101.php) NEXT TUTORIAL


(eng_arduino_tut103.php)

Help me by sharing this post


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-
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://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

Build an optic encoded HIGH precision step motor


Electronoobs

Tonton di

AFFILIATE DISCLOSURE (AFFILIATE_DISCLOSURE.PHP)


ADVERTISERS
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://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)

Brushed motors Arduino drone - Part2 - Prototype + NRF24 code


Electronoobs

Tonton di

DIY Inverter tutorial - Arduino or 555


Electronoobs

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)

ELECTRONOOBS Copyright ©2023, All rights reserved |


Terms and Conditions (terms_and_conditions.php) | Privacy
Ad
10K Ohm With Turn Counting Dial Rotary Pote
(https://www.ebay.com/itm/16218559
3590S-2-103L (https://www.ebay.com/itm/16

Policy (privacy_policy) $2.68


mkcid=1&mkrid=711- sold by atom.2014
100% Postive feedback
53200-

You might also like