Build a Custom Servo Motor with a DC Motor

Rek XIX
40 followers
7 Video Views·Oct 31, 2022
Build a Custom Servo Motor with a DC Motor
Today in the workshop we're designing and building a servo motor.
You'll see how we can use a Ceduino Shao to turn any DC motor into a custom servo motor.
We'll also learn about PID controllers.
We're moving into the right position today, so welcome to the workshop.
Well hello and welcome to the workshop.
Today we are going to be building a servo motor.
And the first question you probably have is, why are we building a servo motor?
I mean, after all, servo motors aren't exactly hard to obtain.
You can get them on eBay, you can get them on Amazon,
you can pick them up at your local hobby shop or electronic shop.
Well, sometimes they don't exactly have what it is you want.
The requirement that one of my friends has is for a very, very large servo motor.
One that has a lot of torque to it.
Much more torque than you'll get out of a small hobby servo motor.
Another requirement that he has is that his servo motor can spin a full two revolutions, 720 degrees.
As opposed to the 180 or 270 degrees that most hobby servos go.
And so before we get into this, I want to introduce you to this friend of mine.
My friend Jeremy Fielding is a fellow YouTuber who is a mechanical genius.
On Jeremy's channel, you can learn all sorts of things about working in the shop,
working with mechanics, working with metal, wood, 3D printing.
Jeremy does it all.
And Jeremy and I have a lot of things in common,
but one thing is that he tends to do things on a much bigger scale than me.
For example, when I built a robot arm, I built a little tiny me arm robot arm.
Jeremy, on the other hand, builds huge robot arms like Jarvis.
When Jeremy works with motors,
he doesn't work with the tiny little motors that I work with on the robot cars,
those little yellow things.
Jeremy works with huge motors, some of them so big that he can't even pick them up.
If you need to know anything about motors, Jeremy is the guy to go and see.
So I urge you to go over and check out his YouTube channel.
And a project that he's been working on this summer
is one that has been worked on in parallel with a project I'm working on.
Now, I'm building a six-wheel rover,
and you're going to be seeing a bit more about this rover very soon on YouTube.
Of course, if you're a newsletter subscriber, you already know about the rover
because you've had access to exclusive articles and videos about it.
But you'll be seeing my rover here on YouTube very soon.
Jeremy, on the other hand, is building something that he is calling a self-driving go-kart.
And for his self-driving go-kart, he needs a large servo motor to do the steering.
And Jeremy's servo motor, as I said, also needs to spin twice, 720 degrees.
So we've decided that the best way to do that
is just to build a custom servo motor out of a DC gear motor.
And so that's exactly what we're going to be doing today.
So let's take a look right now at what the requirements for building a servo motor are,
and then we'll go and start working on our custom servo motor design.
A servo motor is a motor whose shaft position can be controlled by an external signal.
Internally, most servo motors use a DC motor.
Hobby, or RC, servos use a pulse-width modulation input to control the shaft position.
A standard hobby servo rotates 180 degrees.
There are also 270 degree hobby servos available.
The price of these servo motors increases dramatically with the size and the torque specifications.
The pulse-width modulation input determines the motor shaft position.
On a hobby servo, this pulse-width ranges from 1,000 microseconds to 2,000 microseconds.
1,500 microseconds is top dead center position.
On a standard servo motor, this would be the 90 degree mark.
Internally, the operation of a servo motor is as follows.
Pulse-width modulation is accepted at the input.
This PWM is converted to the actual servo position.
The motor is driven, and the actual position is read by a sensor.
This position is fed back to a circuit which compares it to the input requirements,
and adjusts the motor accordingly.
When designing a custom servo motor, we can use a microcontroller to do all of the input and comparison functions.
The most common form of sensor to sense the output position is a potentiometer.
The microcontroller that we use for our servo requires an accurate analog-to-digital converter.
We'll be using a Ceduino Xiao, which has a 12-bit ADC.
The potentiometer used as a sensor needs to be a linear pot, or a non-tapered pot.
We could use a standard potentiometer for 180 or 270 degree designs.
For custom or multi-turn designs, a 10-turn potentiometer is best used.
The logic for our microcontroller is as follows.
First we'll read the PWM input signal.
Then we read the position of our sensor.
From these two, it's determined whether we need to move the motor.
If we don't, then we just go back up and start all over again.
If we do need to move the motor, however, we need to determine which direction to move it in.
Once we determine that, we move it either clockwise or counterclockwise,
and then go back to the top and start over again.
There are a number of factors that can influence how accurate our design will be.
Inaccuracies in the potentiometer will result in positioning errors.
Potentiometer drifting, in other words changing resistance when the pot is being held still,
will cause servo chattering.
The method of mechanical coupling to the potentiometer is also a factor.
The speed of our motor can affect performance.
If it's too fast, we can overshoot the desired position.
For this reason, we might consider using acceleration and deceleration in our design.
Now that we've gone over the requirements, let's start building our custom servo motor.
Here's the test setup that I'm going to be using today to test our servo motor design.
This is something that Jeremy kindly provided me to work with.
He's 3D printed a number of gears over here.
This final gear, which I'm going to pull off right now, is coupled to a potentiometer.
This is a 10-turn pot.
I've got another 10-turn pot over here that you can take a look at.
The wiper is actually this connection on the pot.
Then we've got the two extremes over here.
We've got the 10-turn pot mounted under here.
You can see the rest of the motor assembly and the gear assembly down over here.
The pot, of course, is coupled to this gear, which is going to spin whenever this gear spins.
On this, hopefully you can see it, we've got a little arrow.
It even says steering wheel. That's quite amusing, Jeremy.
The arrow basically points at the direction that we're going so we can observe our servo motor moving around.
Now, I'm going to be using it with this motor driver.
This is the Cytron MD25HV motor driver.
This is a very powerful motor driver.
It's got some very basic connections.
You've got the power supply connections over here.
I'm going to be powering it with 12 volts from my power supply here.
You've got the connections to the motor over here.
You can see the H-bridge actually over here, these four MOSFETs here.
Because they're using MOSFETs, they don't even need a heat sink.
It's a pretty neat device because it's also got connectors.
It comes with a potentiometer and a switch over here.
You can actually use this as a standalone device by plugging them into here
and using the pot to control speed and the switch to control direction.
You've also got these terminals here.
You've got a connection in for the direction and the PWM input.
Now, these are logic level signals.
They can work with 3.3 or 5 volt logic.
Then you've got a 5 volt DC output and a ground.
The 5 volt DC output, which I've connected to this red wire here, is going to be great
because that's going to let me power the entire microcontroller circuit.
And so it's a great motor driver and a very powerful one as well.
It will have no problem with this big motor over here.
So that's the test setup that we're going to be using today to test out our servo motor design.
So now that we've seen the servo motor requirements, let's start building our custom servo motor.
Now, I'm going to use a Seduino Xiao for the controller on this.
If you're not familiar with the Seduino Xiao,
I have done a video on the Xiao that will show you everything about hooking it up
and getting it to work with your Arduino IDE.
You're going to need to install an additional boards manager on the Arduino IDE.
And that video and its accompanying article will show you exactly how to do that.
Now, I'm using the Xiao for a couple of reasons.
One reason is it's very inexpensive. It's a $5 microcontroller.
The other reason is that it has a 12-bit analog-to-digital converter.
And this should give us a little bit more accuracy in our servo
than the 10-bit converter that you would find on something like an Arduino Uno.
Now, the Seduino Xiao is a 3.3-volt microcontroller.
And because of that, I'm using a logic-level converter
in order to make certain that the input pulse-width modulation doesn't overload the Seduino Xiao itself.
Now, you could probably have done this with a transistor,
and it probably would have been a better way to do it.
But the logic-level converter is very inexpensive and quite easy to use,
so I decided to do that.
So let's go and take a look at the hookup of this now.
And then after that, we're going to run a couple of tests and calibration sketches
that we're going to need to use before we can actually design the sketch
that we'll be using for the motor controller.
So, here's the hookup.
For our servo controller, you'll require a Seduino Xiao.
We're also going to be using a two-channel level converter
in order to convert the 5-volt in from the servo to the 3.3-volt the Xiao uses.
Now, I'm showing a bi-directional level converter, but you can use a different one as well.
And if you want information about hooking that up,
just check out the article accompanying this video on the DroneBotWorkshop.com website.
You'll also require a linear taper potentiometer.
Now, I'm using a 10-turn pot, but if your servo design only has to travel 180 degrees or less,
you could use a standard potentiometer.
Just make sure it's linear tapered.
You'll need a 3-pin DuPont connector for your servo input.
I'm also using a 2-pin DuPont connector as a power jumper.
This will allow you to supply 5 volts back to your servo control if required.
If you don't need that, you can eliminate this from the circuit.
We'll also require a four-position terminal strip,
and this will be the motor control output that goes to our motor driver.
We'll begin by connecting the wiper of the pot to pin 2 of the Siduino Xiao.
Now, I'm showing the wiper at the bottom because on my 10-turn pot, that's where it is,
but yours might be in the middle.
The best way to find out the wiring of your potentiometer is just to use a multimeter.
Also note that pin 2 of the Xiao is its third pin as it starts its numbering at pin 0.
The left end of the potentiometer will go to the Siduino Xiao pin 12, which is the ground.
The right end of the pot will go to the Xiao's pin 11, which is its 3.3-volt output.
We'll take the 5-volt input from the motor controller and connect it to the converter's V-in pin.
The PWM lead from the servo connector will go to the converter's 5A input.
There is no connection to the converter's 5B input as we're only using one channel.
We'll connect the motor control ground to the converter's ground lead.
Pin 11 on the Siduino Xiao, which was our 3.3-volt output, will be connected to the converter's 3V3 input.
Pin 10 on the Xiao will be connected to the converter's 3A output.
We'll also connect that 5-volt line to Siduino Xiao pin 13, which is its 5-volt input.
We'll connect the converter's ground to the Siduino Xiao's pin 12, which is its ground.
There's no connection to the 3B output, and you can choose to connect or eliminate the other ground connection.
It's internally connected anyway.
We'll connect the servo's ground input to the ground connection that we've already made to the Siduino Xiao.
Pin 6 on the Siduino Xiao will go to our motor's PWM output.
This is supplying the PWM signal for our motor controller.
Pin 5 on the Xiao will go to the direction connector.
This changes the direction of our motor's rotation.
And if you're using the power jumper, it's connected between the servo control power lead and the 5-volt line.
And this completes our wiring.
Now, here's a very small sketch that we're going to run just to test the input of our servo controller.
Now, remember the input is pulse width modulation.
And what we need to do is measure the width of that pulse in order to determine our servo shaft position.
Now, there is a function called pulse in.
And pulse in is precisely for what we want to do.
It measures the width of pulses.
So you might think this would be the way to resolve this issue.
However, pulse in has a number of problems when used in a servo controller design.
The first problem is that when pulse in is running, you can't run anything else.
And this can induce timing errors.
Another problem is that pulse in could very easily start measuring in the middle of a pulse and therefore create an erroneous reading.
In this case, we are going to be using interrupts instead of pulse in to measure our pulse width.
And this sketch just shows you how we do that.
So first of all, we're going to define a number of variables that we're going to use in that pulse width calculation.
The final result is something called PWM period.
We also have a flag that we set that we call done.
And this indicates we're finished our measurement.
And we have an unsigned long that holds a time value.
And we're calling that start.
It's the start time of our pulse.
We'll also declare a number of timestamp variables.
Current millis, which is going to measure the number of milliseconds that have elapsed since the microcontroller started.
Previous millis being the previous version of it.
And then interval time, which is the interval that we're going to use to measure our pulses.
We'll also define the pin that we are using for the input.
And that is pin number 10.
And we're calling that PWM in.
Now in our setup, we're going to set that pin up as an input.
And then we're going to attach it to an interrupt handler.
So we use the digital pin to interrupt function to determine which interrupt has been raised.
We're going to call a handler that we call measure pulse, which we'll see in a moment.
And we're going to do it on a chain.
So if the line goes high or if the line goes low, we will get an interrupt.
We'll also set up our serial monitor because this is how we're going to be displaying our results with this sketch.
Now measure pulse is the interrupt handler and it's the key to making everything work.
Now there are two conditions because we're using a change interrupt.
We could either have the interrupt going high or we could have it going low.
Now if it's high, we are at the beginning of the pulse.
So we're going to set our start variable to microseconds.
And this is the value of microseconds that have elapsed since the microcontroller was started.
So we're going to assign that to the start value.
However, if we're at the end of the pulse and it's gone low, we are going to be able to calculate our period.
Because it is the period between the current value of microseconds minus the original start value that we gave it over here.
And once we've done that, we're going to set our done flag to true to indicate that we've finished measuring a pulse.
Now we can just go into a loop and see how this works.
We're going to set the millis value to the current number of milliseconds.
Again, the number of milliseconds in this case, since the Arduino, or in our case, the Cduino Xiao has started.
Now let's see if the interval is greater or equal to the interval that we have defined, which was 20 milliseconds.
If it is, then we're going to reset everything because we've either missed the pulse or we're in the middle of it.
But if it's not, we are going to go down and print out the PWM period and then set our done flag up to false.
So it's actually a pretty simple sketch.
We're going to run it and look at the serial monitor and see if we can indeed determine the pulse width of our input signal.
Okay, so let's test the input to our servo motor driver to make certain that we're picking up the pulses correctly.
I've got my Cduino Xiao over here in the breadboard beside it.
I have got the logic level converter that we're going to be using.
And here is one of those little $3 servo motor testers.
And while it's not the most accurate source of PWM, it'll certainly suffice.
And it actually emulates the kind of signal that you would get out in the field from a radio controller or some other device.
Now, if you look at the serial monitor, I'm reading about 1573 and that makes sense.
I'm about the center of travel for this.
So I'm going to move it down toward one extreme.
And as you can see, it starts dropping.
It goes down to about 909, 910.
It should actually be dropping down to about 1000 at one end.
But that's pretty good for one of these little $3 testers.
I'll move it up right now.
And we'll move it all the way to the other extreme.
And again, that should be about 2000.
It's actually 2142, but the tester accuracy and all, that's pretty good.
One thing that's nice about it is it's pretty stable.
I'm just going to leave it in one position here right now.
And as you can see, the last digit fluctuates a little bit, but it actually works pretty well.
And so it looks like the input section of our servo controller is functioning.
And we can go ahead and design the rest of it now.
Now, before we can design our final sketch, we're going to need to know the endpoints of our potentiometer.
Remember, the potentiometer is our sensor.
And so each of its endpoints are going to determine the maximum angle that our servo motor can travel at.
And since it's a custom design, this may not be 0 to 180 degrees.
So all this sketch is going to do is simply going to measure the input from the potentiometer and put it up on the serial monitor.
That way we can physically move to both endpoints, get the values, and use them in our subsequent sketches.
And so it's a super simple sketch.
We're going to start off by defining the input pin that we use for the pot, which in our case is number 2,
and a value to indicate the value that the potentiometer is giving to the analog-to-digital converter.
The setup is quite simple.
We have to set the analog read resolution to 12 bits.
If you don't do this, the Siduino shall default to 10 bits so it can remain compatible with older sketches.
So make certain that you do that, and then set up your serial monitor in order to read the value.
And then it is a very simple matter of doing an analog read of the pot endpin to get our value and printing it out to the serial monitor.
We'll add a little delay in here in order to prevent flooding the serial monitor, and just do it over and over again.
So we can observe our serial monitor while we move the pot back and forth so we can get the actual values that we're going to use in our sketch.
So I'm running my endpoint test, and in order to facilitate that, I've taken the gear off of the motor so it doesn't bind all the rest of these gears.
And so I'm just going to manually move this.
Now I've got this at one end right now, and I'm getting some readings in the neighborhood of 23 or so.
So I think that's what I'm going to take.
Now you might want to set your pot so it isn't actually at the very end extreme,
and that might actually even be a better idea so that you have a little leeway over there.
And I'll move it up, and as I'm going up, you can see right now I'm in about the center, and I've got readings about 945.
And I'm going to move it, I guess, roughly about 180 degrees over here.
Readings about 1825, 1823.
And, of course, I'm not measuring this particularly accurately.
If you wanted to be precision about it, you would use something like a protractor or something to make certain that you know the two different extremes.
But that's really all the test is.
We're just reading the input to the analog-to-digital converter and trying to get those numbers.
And you'll need to write those numbers down.
So I'm going to write 1825 over here down, and 23.
And those numbers will be used in our final sketch for our servo driver because they represent the two extremes,
the two endpoints of the travel of the potentiometer, which in my case is under this gear here.
So now we've hooked up our controller, and we've run a few test circuits,
and we've also established the endpoints for our potentiometer values.
And that's pretty important.
If you haven't done that, you're going to need to go back and do it because you'll need those values in our final sketch.
Now, our final sketch is going to be based upon something called a PID controller.
And PID controllers are very important concepts when working with controlled devices.
Now, in order to do justice to explaining a PID controller, I would probably need to do an entire video on it.
And it would involve a lot of mathematics, including some calculus.
And I don't really think you want me to get into that right now.
If you do want to learn about PID controllers in more depth, there are a wealth of resources on the Internet.
Instead, I'm going to give you a basic introduction to PID controllers.
I'm not going to be using any mathematics in the introduction.
And you'll understand why a PID controller is very good for this design,
how it can make a servo controller that we can fine-tune to our purposes.
So let's go and learn a little bit about PID controllers.
PID is an abbreviation for a Proportional Integral Derivative Controller.
This is also sometimes called a three-term controller.
A PID controller is a control loop mechanism that employs feedback.
The controller continuously calculates an error value.
This error value is the difference between a set point value and a measured process variable.
The controller applies corrections based upon proportional, integral, and derivative terms.
Now, if you think that PID controllers are something new, you would be wrong.
They've been around for about a century.
They were used in the 1920s for navigating ships.
Now that last explanation may not have been that enlightening,
so let's try and look at that in a different way.
Let's take a look at a standard process that has a set point,
a controller, a controlled device, and a measured value.
An example of this would be a potentiometer connected to an Arduino,
which is driving the speed of a motor.
The potentiometer establishes the set point, the Arduino is the controller,
the motor would be the controlled device,
and the motor's actual speed would be the measured value.
This would be referred to as an open loop system.
With the open loop system we can never be certain that the actual motor speed
is exactly what we have dialed up on our potentiometer.
In order to fix that we can apply some feedback and send it back and compare it.
with the speed that we've asked for.
This would be called a closed loop system.
Now if this design looks familiar it's not surprising,
it's actually what we've been designing today.
In the case of our servo motor, the set point would be the PWM input.
The controller would be the microcontroller, in our case the Siduino Xiao.
The controlled device is the DC motor that we're driving,
and the measured value is determined by our potentiometer.
Take a look at our original servo diagram and you can see the similarities.
Now here's a diagram of a PID controller,
and while the math may look a little bit daunting,
it's really not as difficult as it looks.
RT would be our set point, in the case of our servo motor,
this would be the PWM input to request the motor position.
YT is the process value, in the case of our servo,
this would be the actual shaft position of the servo motor.
ET is the error value, the difference between the set point and the process value.
UT will be our control variable.
In this case of our servo motor, this is the signal that we send to the motor driver,
in order to reduce or eliminate the error.
Our control variable value is determined by proportional,
integral, and derivative functions.
The proportional function is proportional to the current error.
It's multiplied by a gain factor of k underscore p.
The integral function relies on the past values of the error.
It integrates these values over a period of time.
This is multiplied by a gain factor of k underscore i.
And the derivative function uses the estimated future trend of the error.
This provides a damping effect on the mechanism.
This is multiplied by a gain factor of k underscore d.
The gain for proportional, integral, and derivative is not the same,
and setting these values is referred to as PID tuning.
KP, the proportional variable, controls how fast the motor will move to its new position.
If this value is set too high, the motor will overshoot its target.
The integral gain function, k underscore i,
controls how the servo operates at the end of its movement.
You need to tune this to prevent oscillation and instability.
The derivative gain, k underscore d, controls the damping effect.
Properly applying this can reduce overshoot and oscillation.
And so now that we have the basics of PID controllers down,
let's see how we can apply this to our servo controller design.
Now here's a sketch that we're going to be using for our servo motor driver,
and it makes use of the Arduino PID library.
So before you run this, you're going to need to install that.
So go into Tools, and go into Manage Libraries,
and filter your search by the term PID.
Now you'll note there are a number of PID libraries.
We're going to need to scroll down a bit over here to get to the one that we are using,
and it's simply called PID by Brett Beauregard.
So I've already installed that.
You'll need to install that into your Arduino IDE
before you can get this sketch working,
and this indeed does all of the heavy lifting, so to speak, in our sketch.
So let's see how that works.
We're going to start off by including that library,
so we include the PID library.
And then we set up a number of variables for that library,
and you'll notice these variables and how they correspond
to that explanation I gave you on how PID controllers work.
So these are our three gain variables
for proportional, integral, and derivative gain, respectively.
Now the only one I'm using right now is a proportional gain,
and I've got it set to 1, which basically means no gain whatsoever.
And I'm not using integral or derivative at all.
But when you're doing this, you may need to change these values
for your own specific situation.
If you don't find that the servo is responding quick enough,
you can increase this value over here to get it to respond faster.
If it seems to be oscillating or hunting for its actual position,
you might add a little bit over here.
And if you want it to slow down a little bit more
before it reaches its position,
this is the number that you would change.
And remember, since these are doubles, you can use fractional values as well.
So I would suggest you start off with the values that I have over here
and then fine-tune them to your own specific needs.
These are the other PID variables that we have over here,
and you'll recognize them as well.
The set point, which is the point that we're trying to achieve initially,
the input point, and the output.
And the output is what we're going to be trying to calculate
so that we can actually move our motor into the correct position.
Then we go and we create a PID object.
In this case, it's called myPID, and we pass all those parameters to it.
Direct over here means that we want to go in the same direction,
in a forward direction.
The other parameter we could put in here is reverse,
but direct is by far the most common.
Now, these variables over here for the pulse width calculation,
you have seen before.
We looked at exactly the same variables when we did our input test,
and then we go and define a couple of the pins,
the PWM input pin, the input pin for the potentiometer,
the output pin that is going to our motor controller,
and also the direction pin for the motor controller.
The Cytron motor controller has an input,
which is going to our output pin that will take PWM,
and also a direction pin, which if it's high,
the motor will go forward, and if it's low,
the motor will go backwards.
So if you're using a different motor controller,
it should be a pretty simple matter of adapting this to your motor controller.
We have an integer to represent the value from the potentiometer,
one to represent the PWM value that we're sending to the motor,
and then we have two integers over here, potmin and potmax,
and these are the values that you obtained
when you ran the experiment to see what the minimum and maximum value
your potentiometer, or more accurately, your analog-to-digital converter
was giving out at the two extremes of your servo movement.
As you recall, mine were 23 and 1825,
but I doubt yours will be 23 and 1825.
So this is where you insert those two values
that you got in the last experiment.
So you'll need to run that experiment before you run this code.
Now we go into the setup, and most of the setup is pretty basic.
The PWM pin is an input.
The connections out to the motor controller are both outputs.
Once again, we're going to set the resolution
of our analog-to-digital converter to 12 bits
so that it doesn't default to 10,
and we'll attach our interrupt to the interrupt handler
in order to get the interrupts that we need to measure the input pulse width.
I've got the serial monitor going over here,
but it's only here for troubleshooting purposes.
In normal operation, you won't be using the serial monitor.
And now we're going to set up the PID controller.
The first thing we do is we set its mode to automatic.
The other choice here would be manual,
which means we would have to start the PID controller
every time we want to use it.
But instead, we just always want it to be on.
Now you need to do this, or else it'll default to manual.
We'll also set the limits of the output,
and we're setting it from negative 255 to positive 255.
Now these are our PWM values.
Negative numbers mean we want to make the motor go in reverse.
And so this is the range that we're going to cover,
and the sample time is equal to the duration that we specified earlier,
so this amount of time that we're going to do a sample in.
Now measure pulse you've already seen.
This is exactly the same function that we worked with in our earlier sketch
in order to measure the pulse width of the incoming pulse,
and this is what it's called every time an interrupt is generated.
And then we're going to go into the loop,
and the loop again starts off exactly the same as the pulse measurement did.
And then we'll go and we'll get the value from the potentiometer
and assign that to the input value.
And then we need to establish our set point and input for the PID,
and we'll do that with two different map commands.
The map command over here takes the period we've measured it and maps it.
It's going to have a range from 1,000 to 2,000,
and we're mapping it to a range of negative 255 to positive 255.
Now you'll remember when I did my experiment,
I was actually getting values down around 900 and something at the low end
and around 2,100 for the upper end.
So I've kept it at 1,000 and 2,000 because those are the standards,
but you could fine-tune this to your own application by running that experiment
and putting the lowest value in over here and the highest value here.
So it doesn't have to be 1,000 and 2,000,
but if not, you could just leave it at those values.
Now the input, we're going to take the input value
and we're going to map it between a number of negative 255 and positive 255 as well.
And we're going to use the minimum value from our pot
and the maximum value that we calculated earlier.
And so now set point and input are basically comparing apples to apples.
They're numbers between negative 255 and positive 255.
And so all we do now is we pass it into the PID and have it compute,
and that will present us with an output value, which is a variable we called output.
Now the serial USB prints are just for the serial monitor.
If you're having trouble and you want to troubleshoot it,
you can put your serial monitor up while you're running this
and it will give you the values that we're working with.
But we're going to look at output now.
Now if output ends up being zero because the two of them are equivalent,
then there is nothing we need to worry about.
The DC motor will just stay in one position because it's in the correct position.
But if the output is greater than zero, then we need to move our motor forward.
And so the PWM value will then just become the output value
because it will be a number between one and 255.
We will write that out to our motor with an analog write,
and we'll also set the motor to go forward.
So I do a digital write and I send that high.
Now if it's less than zero, we need to move the motor in reverse.
And so our PWM value now becomes the absolute of the output value
because this is going to be a negative number and we want a positive one.
We could have also achieved the same thing by multiplying output by negative one.
And then the same thing.
We're going to write that out to the PWM pin on the motor controller.
And this time we're going to set the direction pin low
to have the motor spin in the opposite direction.
Another serial so that we can see what the PWM value is.
And then we set our flag, our done flag, and we return and start the loop over.
So the sketch ends up being fairly simple,
and that is because of the power of the PWM library that we're using.
So I'm going to load that up onto my Seduino Xiao.
I'm going to hook everything up on the other workbench
so that I can test the motor, and we'll give it a spin.
Okay, so I've got my servo control demo all ready to go,
and I'm powering everything directly from the Cytron MD25HV motor controller.
So I'm using the 5V out here to feed back into the Seduino Xiao.
I've got my little PWM servo tester over here to test this,
and I've got my 12V power supply, which is right now on standby mode,
so it's not powering everything.
Now you'll just need to take a look at the arrow over here to watch the motor movement.
So let's give this thing some power right now.
We've got some power.
You can see the lights come on over here and on the Cytron controller,
and I'm able to move it one way.
I'm able to move it the other way,
and it does indeed seem to work.
It seems to go exactly in proportion to where I'm moving this controller.
So this does work.
Now we could fine-tune some of the PID parameters.
If we, for example, fine-tune the P parameter and made it a bit bigger,
it would move a bit faster.
The other two would affect the damping and the oscillation.
But essentially, this is a working circuit,
and it's a working method of turning a DC gear motor into a very powerful servo motor.
So as you can see, it's not that difficult to take a DC gear motor and a microcontroller
and create your own custom servo motor.
And by using a PID controller library,
we can adjust the parameters of that custom servo motor to suit our purposes exactly.
Now I'm sure you're interested to see how this is working out for Jeremy
and his self-driving go-kart,
and you can find the link right below this video,
which will take you to his video,
and you can check it out for yourself.
Now while you're on Jeremy's channel and while you're on this channel,
you'll probably want to subscribe.
If you haven't subscribed to either of our channels,
the procedure is exactly the same.
All you do is click on the red subscribe button,
and after you do, also hit that little bell notification,
and assuming that you've enabled notifications on your YouTube,
you will get notified every time Jeremy or I make new videos.
Now Jeremy's going to be continuing the series on his self-driving go-kart,
and I'm going to be doing the same with my six-wheel drive Rover project.
Now those of you who have subscribed to my newsletter
already have a little bit of advanced information about that Rover,
because you've got links to some exclusive videos and articles
about the construction of the Rover.
If you're not a newsletter subscriber, please consider doing so.
It is not a sales letter.
It's just my way of keeping in touch with you
and letting you know what goes on here in the workshop,
and of course it's a free newsletter.
All you need to do is fill out the form,
give me your email address, and you're all set.
Another thing you might want to consider joining is a DroneBot Workshop Forum.
Again, DroneBot Workshop Forum people are getting some exclusive looks
at some videos on the forum that are just for forum members,
and so you'll need to be a forum member in order to take a look at those,
and also the forum is a great place to discuss electronics, microcontrollers,
and all sorts of things with a bunch of like-minded people
who share similar interests.
So if you're having problems with one of your projects,
this is the place to go in order to get some help,
and there's a link to the forum right below the video as well.
So until we meet the next time, please take good care of yourself,
please stay safe, and I hope to see you again very soon
here in the DroneBot Workshop.
Goodbye for now.
