Avatar

PID DC BRUSHED MOTOR CONTROL

Aug 19th, 2019 | by: ELECTRONOOBS
Views 786
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. 

What we need


I will make just only one axis. See full part list also here on this link. 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. 1 x Arduino NANO/UNO: LINK eBay
  2. 1 x DC motor with encoder: LINK eBay
  3. 1 x H bridge L298N: LINK eBay
  4. 1 x end stop SWITCH: LINK eBay
  5. 1 x Potentiometer 10 turns: LINK eBay
  6. 2 x smooth rod 8mm: LINK eBay
  7. 1 x lead screw: LINK eBay
  8. 4 x lineal bearing LM8UU: LINK eBay
  9. 1 x STL 3D files: LINK eBay
  10. 1 x GEARS: LINK eBay

      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. 

The axis mounted

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.


Schematic

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.
FILES TO DOWNLOAD FOR THIS STEP
PID example code  DOWNLOAD
PinChangeInterrupt library  DOWNLOAD
003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d0063006f006d006d0065006e007400220020007300740079006c0065003d00220063006f006c006f0072003a00200072006700620028003100330036002c0020003100330036002c00200031003300360029003b00200066006f006e0074002d00660061006d0069006c0079003a0020004d0065006e006c006f002c0020004d006f006e00610063006f002c00200043006f006e0073006f006c00610073002c0020002600710075006f0074003b0043006f007500720069006500720020004e00650077002600710075006f0074003b002c0020006d006f006e006f00730070006100630065003b00200066006f006e0074002d00730069007a0065003a00200031003300700078003b00200066006f006e0074002d007700650069006700680074003a0020003400300030003b002000770068006900740065002d00730070006100630065003a0020007000720065002d0077007200610070003b0022003e002f002f0049006e0063006c00750064006500200074006800650020006c006900620072006100720069006500730020007700650020006e006500650064003c002f007300700061006e003e003c007300700061006e0020007300740079006c0065003d00220063006f006c006f0072003a0020007200670062002800360038002c002000360038002c0020003600380029003b00200066006f006e0074002d00660061006d0069006c0079003a0020004d0065006e006c006f002c0020004d006f006e00610063006f002c00200043006f006e0073006f006c00610073002c0020002600710075006f0074003b0043006f007500720069006500720020004e00650077002600710075006f0074003b002c0020006d006f006e006f00730070006100630065003b00200066006f006e0074002d00730069007a0065003a00200031003300700078003b00200066006f006e0074002d007700650069006700680074003a0020003400300030003b002000770068006900740065002d00730070006100630065003a0020007000720065002d0077007200610070003b0020006200610063006b00670072006f0075006e0064002d0063006f006c006f0072003a00200072006700620028003200340030002c0020003200340030002c00200032003400300029003b0022003e000a003c002f007300700061006e003e003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d006d00650074006100220020007300740079006c0065003d00220063006f006c006f0072003a0020007200670062002800330031002c0020003100310033002c00200031003500330029003b00200066006f006e0074002d00660061006d0069006c0079003a0020004d0065006e006c006f002c0020004d006f006e00610063006f002c00200043006f006e0073006f006c00610073002c0020002600710075006f0074003b0043006f007500720069006500720020004e00650077002600710075006f0074003b002c0020006d006f006e006f00730070006100630065003b00200066006f006e0074002d00730069007a0065003a00200031003300700078003b00200066006f006e0074002d007700650069006700680074003a0020003400300030003b002000770068006900740065002d00730070006100630065003a0020007000720065002d0077007200610070003b0022003e0023003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d006d006500740061002d006b006500790077006f0072006400220020007300740079006c0065003d00220063006f006c006f0072003a00200072006700620028003100310034002c0020003100340032002c002000300029003b00200066006f006e0074002d007700650069006700680074003a00200062006f006c0064003b0022003e0069006e0063006c007500640065003c002f007300700061006e003e0020003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d006d006500740061002d0073007400720069006e006700220020007300740079006c0065003d00220063006f006c006f0072003a0020007200670062002800370037002c0020003100350033002c00200031003900310029003b0022003e0026006c0074003b00500069006e004300680061006e006700650049006e0074006500720072007500700074002e0068002600670074003b003c002f007300700061006e003e003c002f007300700061006e003e003c007300700061006e0020007300740079006c0065003d00220063006f006c006f0072003a0020007200670062002800360038002c002000360038002c0020003600380029003b00200066006f006e0074002d00660061006d0069006c0079003a0020004d0065006e006c006f002c0020004d006f006e00610063006f002c00200043006f006e0073006f006c00610073002c0020002600710075006f0074003b0043006f007500720069006500720020004e00650077002600710075006f0074003b002c0020006d006f006e006f00730070006100630065003b00200066006f006e0074002d00730069007a0065003a00200031003300700078003b00200066006f006e0074002d007700650069006700680074003a0020003400300030003b002000770068006900740065002d00730070006100630065003a0020007000720065002d0077007200610070003b0020006200610063006b00670072006f0075006e0064002d0063006f006c006f0072003a00200072006700620028003200340030002c0020003200340030002c00200032003400300029003b0022003e000a003c002f007300700061006e003e003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d006d00650074006100220020007300740079006c0065003d00220063006f006c006f0072003a0020007200670062002800330031002c0020003100310033002c00200031003500330029003b00200066006f006e0074002d00660061006d0069006c0079003a0020004d0065006e006c006f002c0020004d006f006e00610063006f002c00200043006f006e0073006f006c00610073002c0020002600710075006f0074003b0043006f007500720069006500720020004e00650077002600710075006f0074003b002c0020006d006f006e006f00730070006100630065003b00200066006f006e0074002d00730069007a0065003a00200031003300700078003b00200066006f006e0074002d007700650069006700680074003a0020003400300030003b002000770068006900740065002d00730070006100630065003a0020007000720065002d0077007200610070003b0022003e0023003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d006d006500740061002d006b006500790077006f0072006400220020007300740079006c0065003d00220063006f006c006f0072003a00200072006700620028003100310034002c0020003100340032002c002000300029003b00200066006f006e0074002d007700650069006700680074003a00200062006f006c0064003b0022003e0069006e0063006c007500640065003c002f007300700061006e003e0020003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d006d006500740061002d0073007400720069006e006700220020007300740079006c0065003d00220063006f006c006f0072003a0020007200670062002800370037002c0020003100350033002c00200031003900310029003b0022003e0026006c0074003b00500069006e004300680061006e006700650049006e00740065007200720075007000740042006f0061007200640073002e0068002600670074003b003c002f007300700061006e003e003c002f007300700061006e003e003c007300700061006e0020007300740079006c0065003d00220063006f006c006f0072003a0020007200670062002800360038002c002000360038002c0020003600380029003b00200066006f006e0074002d00660061006d0069006c0079003a0020004d0065006e006c006f002c0020004d006f006e00610063006f002c00200043006f006e0073006f006c00610073002c0020002600710075006f0074003b0043006f007500720069006500720020004e00650077002600710075006f0074003b002c0020006d006f006e006f00730070006100630065003b00200066006f006e0074002d00730069007a0065003a00200031003300700078003b00200066006f006e0074002d007700650069006700680074003a0020003400300030003b002000770068006900740065002d00730070006100630065003a0020007000720065002d0077007200610070003b0020006200610063006b00670072006f0075006e0064002d0063006f006c006f0072003a00200072006700620028003200340030002c0020003200340030002c00200032003400300029003b0022003e000a003c002f007300700061006e003e003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d006d00650074006100220020007300740079006c0065003d00220063006f006c006f0072003a0020007200670062002800330031002c0020003100310033002c00200031003500330029003b00200066006f006e0074002d00660061006d0069006c0079003a0020004d0065006e006c006f002c0020004d006f006e00610063006f002c00200043006f006e0073006f006c00610073002c0020002600710075006f0074003b0043006f007500720069006500720020004e00650077002600710075006f0074003b002c0020006d006f006e006f00730070006100630065003b00200066006f006e0074002d00730069007a0065003a00200031003300700078003b00200066006f006e0074002d007700650069006700680074003a0020003400300030003b002000770068006900740065002d00730070006100630065003a0020007000720065002d0077007200610070003b0022003e0023003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d006d006500740061002d006b006500790077006f0072006400220020007300740079006c0065003d00220063006f006c006f0072003a00200072006700620028003100310034002c0020003100340032002c002000300029003b00200066006f006e0074002d007700650069006700680074003a00200062006f006c0064003b0022003e0069006e0063006c007500640065003c002f007300700061006e003e0020003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d006d006500740061002d0073007400720069006e006700220020007300740079006c0065003d00220063006f006c006f0072003a0020007200670062002800370037002c0020003100350033002c00200031003900310029003b0022003e0026006c0074003b00500069006e004300680061006e006700650049006e007400650072007200750070007400500069006e0073002e0068002600670074003b003c002f007300700061006e003e003c002f007300700061006e003e003c007300700061006e0020007300740079006c0065003d00220063006f006c006f0072003a0020007200670062002800360038002c002000360038002c0020003600380029003b00200066006f006e0074002d00660061006d0069006c0079003a0020004d0065006e006c006f002c0020004d006f006e00610063006f002c00200043006f006e0073006f006c00610073002c0020002600710075006f0074003b0043006f007500720069006500720020004e00650077002600710075006f0074003b002c0020006d006f006e006f00730070006100630065003b00200066006f006e0074002d00730069007a0065003a00200031003300700078003b00200066006f006e0074002d007700650069006700680074003a0020003400300030003b002000770068006900740065002d00730070006100630065003a0020007000720065002d0077007200610070003b0020006200610063006b00670072006f0075006e0064002d0063006f006c006f0072003a00200072006700620028003200340030002c0020003200340030002c00200032003400300029003b0022003e000a003c002f007300700061006e003e003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d006d00650074006100220020007300740079006c0065003d00220063006f006c006f0072003a0020007200670062002800330031002c0020003100310033002c00200031003500330029003b00200066006f006e0074002d00660061006d0069006c0079003a0020004d0065006e006c006f002c0020004d006f006e00610063006f002c00200043006f006e0073006f006c00610073002c0020002600710075006f0074003b0043006f007500720069006500720020004e00650077002600710075006f0074003b002c0020006d006f006e006f00730070006100630065003b00200066006f006e0074002d00730069007a0065003a00200031003300700078003b00200066006f006e0074002d007700650069006700680074003a0020003400300030003b002000770068006900740065002d00730070006100630065003a0020007000720065002d0077007200610070003b0022003e0023003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d006d006500740061002d006b006500790077006f0072006400220020007300740079006c0065003d00220063006f006c006f0072003a00200072006700620028003100310034002c0020003100340032002c002000300029003b00200066006f006e0074002d007700650069006700680074003a00200062006f006c0064003b0022003e0069006e0063006c007500640065003c002f007300700061006e003e0020003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d006d006500740061002d0073007400720069006e006700220020007300740079006c0065003d00220063006f006c006f0072003a0020007200670062002800370037002c0020003100350033002c00200031003900310029003b0022003e0026006c0074003b00500069006e004300680061006e006700650049006e007400650072007200750070007400530065007400740069006e00670073002e0068002600670074003b003c002f007300700061006e003e003c002f007300700061006e003e003c007300700061006e0020007300740079006c0065003d00220063006f006c006f0072003a0020007200670062002800360038002c002000360038002c0020003600380029003b00200066006f006e0074002d00660061006d0069006c0079003a0020004d0065006e006c006f002c0020004d006f006e00610063006f002c00200043006f006e0073006f006c00610073002c0020002600710075006f0074003b0043006f007500720069006500720020004e00650077002600710075006f0074003b002c0020006d006f006e006f00730070006100630065003b00200066006f006e0074002d00730069007a0065003a00200031003300700078003b00200066006f006e0074002d007700650069006700680074003a0020003400300030003b002000770068006900740065002d00730070006100630065003a0020007000720065002d0077007200610070003b0020006200610063006b00670072006f0075006e0064002d0063006f006c006f0072003a00200072006700620028003200340030002c0020003200340030002c00200032003400300029003b0022003e000a003c002f007300700061006e003e003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d006d00650074006100220020007300740079006c0065003d00220063006f006c006f0072003a0020007200670062002800330031002c0020003100310033002c00200031003500330029003b00200066006f006e0074002d00660061006d0069006c0079003a0020004d0065006e006c006f002c0020004d006f006e00610063006f002c00200043006f006e0073006f006c00610073002c0020002600710075006f0074003b0043006f007500720069006500720020004e00650077002600710075006f0074003b002c0020006d006f006e006f00730070006100630065003b00200066006f006e0074002d00730069007a0065003a00200031003300700078003b00200066006f006e0074002d007700650069006700680074003a0020003400300030003b002000770068006900740065002d00730070006100630065003a0020007000720065002d0077007200610070003b0022003e0023003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d006d006500740061002d006b006500790077006f0072006400220020007300740079006c0065003d00220063006f006c006f0072003a00200072006700620028003100310034002c0020003100340032002c002000300029003b00200066006f006e0074002d007700650069006700680074003a00200062006f006c0064003b0022003e0069006e0063006c007500640065003c002f007300700061006e003e0020003c007300700061006e00200063006c006100730073003d00220068006c006a0073002d006d006500740061002d0073007400720069006e006700220020007300740079006c0065003d00220063006f006c006f0072003a0020007200670062002800370037002c0020003100350033002c00200031003900310029003b0022003e0026006c0074003b00540069006d00650072004f006e0065002e0068002600670074003b003c002f007300700061006e003e00200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020003c002f007300700061006e003e

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.


0 Comments

Login or Sign Up to post comments on this tutorial.