Turn any DC Motor into a Servo Motor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello Dan here from how to makeatronics.com and in this video we will learn how to turn any DC motor into a standalone Servo with a lot of features unlike regular servos that have limited motion of 180 or 270 degrees this one has unlimited range of 360 degrees and in addition to that we have the ability to adjust the range of rotation to whatever value we need that's quite handy I think and on top of that we can even adjust the center point of the servo so we can adjust both the center point and the range of rotation at the same time another feature is that we can adjust the sensitivity or how quickly the servo will respond to our input talking about input we can have three different input modes we can control the servo with an analog voltage input or using a potentiometer we can control the servo motor with an RC transmitter as well as control the servo through a Serial Port by entering angle values through the serial monitor on our PCR laptop we can also do that simultaneously control the server by entering values through the serial Monitor and manually move the server using the RC transmitter the servo motor will know its current position at all times and we can see it on the serial monitor on top of this Servo motor features list is the continuous rotation mode we can control and keep track of the servo motor position even in this continuous rotation mode we can set the servo motor shaft to go to any position with an infinite number of turns all of this is possible thanks to the 12-bit encoder the servo motor employs the as5600 magnetic rotary position sensor and the implemented PID control for driving the DC motor I made this custom Servo motor control board which includes its own microcontroller and everything else to easily turn any DC motor into a standalone Servo motor we just have to position the controller board at the center of the output shaft including a specific magnet on the shaft connect any size of DC motor with up to 3.5 amps rated current power the whole system with 12 volts and that's it we get a Servo motor from a normal DC motor with all these features now I will walk you through the entire process of making this custom build Servo motor so you can also make one on your own I will explain the working principle of a Servo motor a closed loop controller a PID controller how I designed the custom PCB for it and the gearbox as well as explain the code behind it so we got quite a lot to cover but before we do that let's take a look at this video sponsor on shape which I also used for Designing the gearbox for this custom build Servo motor onshape is a cloud native cat plus PDM system used by businesses and there is also a free version for at home use believe it or not onshape was actually created by the founders of SolidWorks with the rise of cloud computing the SolidWorks Founders realized that creating a cat system from scratch in the cloud could create many new benefits that exist in SolidWorks users can't experience one benefit for example is that on-shaped users are able to collaborate in real time similar to how Google Docs Works which allows engineering and design teams to be more productive than ever and you also no longer need to be concerned with who has the most recent version of a file or checking a file in and out of an expensive and hard to manage PDM system onshape also works in a browser which means it works across all operating systems and devices including IOS and Android devices whether your company uses SolidWorks already and you are looking to modernize your engineering and design or if you just want to try it out for at home use you can create a free own ship account at onshape.pro forward slash how to make a Tronics you can find the link in the description of the video thanks onshape for sponsoring educational content like this back to Topic in order to explain the working principle of a Servo motor let's take apart a typical RC Servo and see what's inside we can notice that it has a small DC motor a controller board a potentiometer and three wires connection two wires for the power and one for the input signal also there are some gears for reducing the speed and increasing the torque of the DC motor this is a typical setup for most RC or hobby Servo Motors the potentiometer is attached to the output shaft of the DC motor and it acts as a position sensor it tells the controller the current position of the servo motor shaft the controller board controls the DC motor based on the input signal the desired position and the actual position that we get as feedback from the potentiometer this represents a closed-loop control system the input signal or the desired position is compared to the actual position of the motor that we get from the position feedback sensor the difference that occur which is called the error is then processed in the controller which commands the motor to move until it reaches that desired position so if we want to build our own Servo motor with some bigger DC motors than these typical RC Servos use we can implement the same closed loop control system we just need a position sensor attached to the output shaft in some way and a microcontroller for driving the DC motor now as for the position sensor the simplest solution is to use a simple potentiometer just like the one we saw in the RC service the problem with these types of potentiometers though is that they have limited range of rotation of only 270 degrees which directly limits the range of rotation of the servo motor there are also other types of potentiometers that can make multiple turns and can provide better range and resolution but still they have limited rotation if we want the servo motor to have unlimited range of rotation then we need to use an encoder encoders are electromechanical devices that can keep track of the angular position of the shaft with an unlimited rotation there are many types of encoders like incremental or absolute or depending on the sensing technology Optical magnetic or capacitive of course each of them has their own advantages and disadvantages but more on that on the website article I chose to use a magnetic encoder or the as5600 magnetic rotary position sensor because it is very Compact and easy to implement encoder providing High Precision or resolution just take a look at how small this microchip is it has a built-in hole effect sensor that can detect changes in the direction of the magnetic field so we just have to attach a magnet to the output shaft of the motor and position it close to the microchip at a distance from 0.5 to 3 millimeters now as the rotor shaft and the magnet will rotate the whole effect sensor will capture those changes in the magnetic field Direction with the help of the built-in 12-bit analog to digital converter the as5600 sensor can output 4096 positions per turn or 360 Degrees rotation that means it can detect changes of the angular position as small as 0.0878 degrees that's pretty impressive and with the fact that it's very affordable and easy to get I think it's the right choice for a custom built servo motor alright so what else do we need a microcontroller and a driver for the DC motor I chose the drv8871 DC driver motor which can handle up to 3.5 amps of current and the atmega 328 microcontroller I chose the surface mount version of it as it's much more compact than the Deep version and my goal was to make as small as possible custom PCB on which I can include everything so that the servo motor can work as a standalone device here's the complete circuit diagram of this custom built Servo motor so we have the atmega 328 microcontroller along with its recommended bare minimum circuit which includes a 16 megahertz oscillator few capacitors and a resistor for powering the microcontroller and the other components that require 5 volts we are using the AMS 1117 voltage regulator which will drop the 12 volts power input to 5 volts here's the as5600 position sensor with its recommended circuit which includes two capacitors and two pull up resistors for the I Square C communication the drv8871 DC motor driver needs just one resistor for limiting the current and two decoupling capacitors then we have the two potentiometers connected to the analog inputs of the microcontroller one for adjusting the range of rotation and the other for adjusting the sensitivity of the server the push button is used for setting the center point of the servo and a two-way dip switch for selecting the working modes of the server there's a pin header for the inputs of the servo either analog voltage input or a digital pwm input from an RC receiver along with a 5 volts and a ground pin there's also a pin header for programming the microcontroller through the espi protocol and the serial port here's a recap of this circuit and its workflow so the input or the desired angular position is received through these two pins it can be either an analog voltage coming from a potentiometer or a digital pwm signal coming from an RC receiver the input goes into the microcontroller where it's compared to the actual angular position that is detected by the encoder or the as-5600 position sensor this sensor communicates with the microcontroller through the I Square C protocol then the microcontroller does the math it calculates the error and according to it it sends pwm signal to the drv8871 driver which drives the DC motor until it reaches that desired position the whole circuit is powered by 12 volts and the as117 voltage regulator provides 5 volts for the microcontroller and the other components appropriately according to the circuit diagram I tried to design a PCB as small as possible and it came out to be 40 by 40 millimeters I positioned the encoder at the bottom side and exactly at the center point of the PCB so it can be easily mounted and aligned with the output shaft of the servo all other components are located on the other side so that they won't interfere with the encoder and the output shaft I ordered the PCB from PCB way here we can simply upload the Gerber file choose the properties for our PCB and order it at a reasonable price I designed this PCB to have four layers the middle ones are foreground which increase the price a little bit I didn't change any of the default properties except for the PCB color which I chose to be blue you can find and download the Gerber file for this PCB on the website article or from the PCB Way project sharing Community through which you can also directly order the PCB nevertheless after several days the PCB arrived the quality of the PCB is great and everything is exactly the same as in the design alright so now we can move on with soldering the components to it I started with the smaller components like this indicator LED and the capacitors and the resistors this is actually my first time soldering this small SMD components and I was actually really really bad at it the most challenging was soldering the atmega 328 microcontroller as the pins are really small and very close to each other but I somehow managed to do it the as-5600 encoder microchip was a bit easier to solder on the back side of the PCB as well as the bigger through-hole components like the dip switch the potentiometers the terminal blocks and the pin headers anyway Here's the final appearance of the controller board which after all it turned out decent I think now it's time to make a suitable gearbox for the DC motor and this microcontroller board I designed the gearbox for these custom built Servo motor using own shape the design of the gearbox depends on the DC motor of course as I mentioned we can use any size DC motor in combination with this controller board we just made here I'm using a DC motor with 37 millimeters diameter and a built-in gearbox that outputs 50 rpm 50 rpm is a good speed for a Servo motor but I wanted to go a bit lower than that to get better torque so I made a gearbox with 3 times speed reduction I used herringbone gears for that purpose as they are efficient and easy to make with a 3D printer of course here we have the freedom to make this gearbox design however we want as it depends on the DC motor that we want to use and what output speeds we want to get I positioned the controller board on the back side of the gearbox and aligned it perfectly in the center of the output shaft in case we want to use the DC motor shaft directly as an output we can use a gear set with a ratio of one to one so that we can track the position of the shaft correctly or we could also use a build system in such a case like I said we have endless possibilities for making the gearbox you can view and explore the 3D model of this custom built Servo motor directly on your web browser with own shape the link to it is in the video description of course you can also download the 3D model as well as the STL files needed for 3D printing the parts on the website article okay so here are the 3D printed parts for this build so we can start assembling the servo motor now along with them we need some M3 bolts and shredded inserts and some bearings first I secured the DC motor to the base plate with some M3 bolts with eight millimeters length then we can install the two gears in place the smaller gear goes directly to the DC motor shaft and the bigger gear will be the output of the servo though the output shaft is composed of two parts I installed threaded inserts on both sides of this output shaft part on one side for connecting the gear to it and on the other side for attaching things on the output of the servo I also installed threaded inserts to the smaller gear which will be used for securing it to the DC motor shaft now we can slide the paired gears into their position as these are herringbone gears we must slide them both in place at the same time otherwise we cannot pair them if we insert them one by one using a grab screw I secured the small gear to the DC motor shaft I applied 12 volts to the DC motor to check whether the gear set work properly we can finish the gearbox assembly by inserting the side panel the ball bearing for the output shaft and the top cover I installed some entry threaded inserts on the back plate so we can secure the whole assembly with some 20 millimeters long M3 bolts I tested the gearbox again and it works great we can notice how the out shaft rotates on the back side and here we need to insert the magnet that the as5600 encoder will keep track of we can secure the controller board to the gearbox using some M2 bolts and nuts the as-5600 position sensor is now perfectly aligned with the magnet and so when the output shaft will rotate it will measure the change in the magnetic field properly please note here that the magnetization direction of the permanent magnet is very important depending on whether it's axially or diametrically magnetized we should position the magnet either perpendicular or parallel to the as5600 IC I ended up changing the direction of my magnet as it didn't have the right magnetization so that the as5600 encoder could measure it next I soldered two wires to the DC motor and connected the motor to the controller with the terminal block as for the power I connected two wires to the power terminal block which on the other side have a DC power connector for connecting a 12 volts power supply and that's it our custom built Servo motor is now done what's left to do now is to give life to this Servo or program the controller for that purpose first we need to burn a bootloader to the atmega 328 microcontroller without a bootloader the microcontroller won't be able to understand the language or the code that we will send to it to burn the bootloader to the atmega 328 microcontroller we need an Arduino board and in my case I will use an Arduino Nano board we will use the SPI communication so we need to connect the suitable aspa pins on the Arduino board to our controller board Now using the Arduino IDE we need to open the Arduino ISP example sketch and upload it to the Arduino Nano board with this code the Arduino Nano is now capable of burning the bootloader to the atmega 28 microcontroller next from the tools menu as a programmer we need to select Arduino as ISP and then click burn bootloader while burning the bootloader we can notice that the Arduino Nano lights will blink a lot and that will result in a successful bootle River once we are done with that now we can program or upload the code to the controller board with the help of an USB to uart interface module the controller board has dedicated pins for easily connecting them just like shown in this circuit diagram now we can open the code for this custom build Servo motor that I made and upload it to the controller before we do that though we should first install the libraries for the as5600 sensor and the PID control we can easily do that from the Arduino IDE Library manager once we hit the upload button the code will be written to our atmega 328 controller through the USB to uart interface module and that's it our custom build Servo motor is now done now we can connect the potentiometer to it and test it out just note that the analog input goes to the s pin on the controller board instead of the a pin when designing the PCB I have connected these two pins wrongly at the atmega 328 then we can select the analog input mode through the Deep speech and power the servo and here it is we can control the servo motor position with the help of the analog input from the potentiometer we have successfully converted our DC motor into a Servo motor we can also control the servo motor with the help of an RC transmitter we just have to connect the RC receiver's signal pin from any channel to the a pin on the controller and we can also use the 5 volts and ground pins for powering the RC receiver the servo motor works perfectly like I mentioned at the beginning it has few additional features than a typical RC servo the range of rotation is not limited to 270 degrees but it can move through all 360 Degrees also we can adjust the range of rotation of the servo we can easily do that by adjusting the potentiometer on the controller the range of rotation can be set to any value we need with the other potentiometer we can adjust the responsiveness of the servo or how quickly it responds to our input it adjusts the value of the proportional component or the proportional gain of the PID controller of course we can also adjust the other components of the PID controller the integral and the derivative component in the code and so we can get our server model respond the way we want you can find more details about the PID controller on the website article anyway in addition to that we can also adjust the center point of the servo there is a push button on the controller and when we press it it sets the current position of the servo motor as a new center point so we can set the center point of the servo motor to whatever position we want and it works in combination with the already mentioned feature adjusting the range of rotation there is one more feature and that's the continuous rotation mode in this mode we can control the position of the servo motor with an infinite number of turns the servo motor will always know its position we can even control the servo through the serial Port by entering angle values through the serial monitor on our PCR laptop we can also do that simultaneously control the servo motor by entering values through the serial Monitor and manually move the servo using the RC transmitter the servo model will keep track of the position at all times now let's take a quick look at the code of this custom built Servo motor and on the website article you will be able to find more details how it works as well as download it so we start the loop by reading the encoder value or the current position of the shaft then if we are in the continuous rotation mode we accept values from the serial Monitor and use them as a set point or desired angle for the PID controller if the input mode is set to potentiometer we read the analog input and correct the set point value depending how far we turn it we do the same set point correction if the input is the RC receiver here we convert the row encoder values into angle values and with this if statements we keep tracking which quadrant the current position of the sensor is with this information we keep track of how the shaft rotates and when it will make a full turn the total angle is the input value for the PID controller on the other hand if we are in The Limited rotation mode first we read the potentiometer value which is used for adjusting the range of rotation and adjust the left and right limit of rotation accordingly if the input mode is the potentiometer we use its value as a set point value for the PID controller if the input mode is the receiver we read the incoming pwm value from the RC receiver and use that value as a set point for setting a different center point we check whether we have pressed the push button and capture that position as a new Center Point according to it then we have to adjust the actual readings from the encoder and offset them by the angle difference between the new and the old Center Point we use that value as an input value for the PID controller using the analog input from the other potentiometer we adjust the proportional gain of the PID controller and finally We Run The PID process to get the output value we use that output value for driving the DC motors with pwm signal left or right or in steel position depending on the output value from the PID controller or depending on the error between the desired and the actual position of the encoder reads so that would be all for this video please note that the code is not well optimized and there is room for improvement also if you try recreating this project you should be prepared for troubleshoot there are a lot of things that could go wrong especially when soldering those small SMD components I didn't get this Servo motor working from the first try initially I had some wrong connections on the PCB then I ordered a new PCB again with new updates but still needed few more tries to get it working nevertheless I hope you enjoyed this video and learned something new so you can make something on your own don't forget to subscribe and for more tutorials and projects visit howtomakatronics.com
Info
Channel: How To Mechatronics
Views: 280,580
Rating: undefined out of 5
Keywords: servo motor, servo, servo motor control, servos, custom servo, as5600, as5600 encoder, magnetic encoder, closed loop control system projects, closed loop, DC Motor, PID control, atmega328p, PCB, 3D model, PCB design
Id: tD2Ki3vw704
Channel Id: undefined
Length: 25min 24sec (1524 seconds)
Published: Mon Oct 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.