Updated: May 12
Yes, but actually, no. At least not in the way we would like it to...
Let me explain and I promise you will be blown away at your new knowledge of how motor control works, especially if you don't know ANYTHING about electronics. Curious?
We are going to do a little experiment and even if you don't have the materials you can still follow along and learn from this.
You will discover the simple concepts behind controlling a motor with a joystick first and I will provide the C code for the Atmega328p chip I use (Arduino chip) along with the Raspberry Pico's micropython code to control a dc motor with a joystick all combined in a single downloadable .zip file at the end and it's all FREE.
Learning to control various motors including DC, Servo and Stepper motors is essential to building any moving robot.
I have saved this article and accompanying video for last in my first beginner electronics for robotics series because it is the most difficult to understand
...aside from EEPROM and Pointers in C programming but we don't need that right now.
The basics I have covered on this site will remain here for any beginner to robotics and electronics to refer to. It is a lot of new information and ways of thinking and I don't have the time to cover it over and over. I need to move on to actually producing 3d printed builds for you all!
I want myself and you all to understand what is going on under the hood so that when we start using more advanced boards like the raspberry pi, etc and programming them in a higher language like micropython, we will be able to debug our code and solve problems MUCH MUCH easier and faster.
I can't tell you how important it is to understand the fundamentals, the basics of coding electronic components before you move on to making the more popular, complex projects. Just look at any forum of people asking for help. The first comments are always asking them if they know the basics and what they are actually trying to do.
We don't just want to copy and paste someone's code, we want to know HOW to do it and make our own as well!
I am going to show you:
How a potentiometer works
How it's output of electricity affects the spin of a DC motor
How a joystick works with a DC motor
Why you need a transistor and a microcontroller or microchip like an ARM (Raspberry Pi/Pico, ATMEGA (Arduino) or PIC micro to do a better job
You can find my long explanations of each of these topics here on my site.
However, I know you want to learn this quickly, and I respect that, so here we go!
I covered how to make SERVO motors move with a joystick in an article previously on this weblog.
Did you know the term "weblog" was the original word for "blog"? I think "blaaawwwg" sounds terrible so I renamed my articles "weblog". Ahhhh, so satisfying to hear now.
Materials you will need
If you are a "hands on", kinesthetic type learner like myself and most of the Earth you can experiment with the items that are listed below along with links to where you can buy them:
Any small 1.5 - 9v DC hobby motor
PS/2 style Joystick & Dupont wires: Male to Female, Male to Male
3v - 6v battery in a case with wire leads
(Anything like 2-3 x AAA, AA, C, D, or 1x 18650 4.3v battery will be enough juice)
*Optional Download my 3d printed battery case you can build yourself
Quick Explanation of HOW DC MOTORS WORK
If we remove the metal case of the motor we can see coils of wire around metal pieces which are connected to a metal ring around an axle which touches the + and - ends of the electricity coming into the motor.
When 1.5-9v of electricity from your battery is connected to the + and - tabs that stick out of the motor are connected, the electricity flows into the coils and the electric field acts like a wind that pushes on the electric fields of the magnets that are usually stuck to the inside of the motor's case and don't move.
If you haven't already, download my 3D printed 9v battery pack from Thingiverse!
I got so mad when the flimsy vinyl one I had ripped that I designed my own!
Look closely at the gold colored axle with the ring that attaches to the coils of wire which constantly lose the battle and move out of the way. That ring has breaks in it so the electricity flowing through each coil is ALWAYS pushing against the magnets. The coils are always conducting electricity so that the battle is ALWAYS N coil vs N magnet and S coil vs S magnet.
Study this ingenious design!
If you want to take a deeper look check out this page on DC Motors.
Quick Explanation of HOW POTENTIOMETERS WORK
Potentiometers are variable resistors.
Electricity goes in through the pin on the left and out through the ground connection (-) on the pin on the right.
Their silhouette of stacked blocks and cylinders look complicated from the outside but the only thing going on inside is varying the amount of electricity that goes out on the middle pin.
As you turn the top cylinder, a piece of resistive, non-conductive materials slides more and more onto the piece of conducting material.
So, the electricity going out on that pin is what we want to use.
Most DC hobby grade motors start spinning with 1.5v-12v.
Use a battery that won't burn out your motor.
I have a 9v in the diagram here but you might want to start with 3-6v to be safe!
What values do we get with our potentiometer?
If we hook up one tab of our dc motor to the potentiometer's middle leg and the other tab to the negative ground connection that the potentiometer and battery are also sharing it would look like this.
Depending on the total resistance your potentiometer is rated for will determine how much current can go through it to the motor.
If you have 10,000 ohm potentiometers like I do, you will see that barely any current makes it through even when you twist it all the way to the maximum current possible.
A weaker potentiometer will let you spin the motor better, so keep that in mind.
Play with it if you have these parts.
If you need some potentiometers, here is a pack that has different resistances.
Check out my main page explaining potentiometers if you need to.
Quick Explanation of HOW Joysticks WORK
Most joysticks operate the same way.
There is an x axis that lets you move the joystick left and right.
There is also a y axis that lets you move the joystick up and down.
Some joysticks also have a push button connected to a small arm on the side that works when you push down on the joystick.
Electricity flows in through the 5v (vcc) pin and out through the ground pin (gnd).
When you move the joystick on the x axis it acts as a variable resistor, just like a potentiometer!
That electrical flow comes out on one of the pins usually labeled "x-axis".
The same goes for the y axis.
If you have a pushbutton on the joystick there is usually a pin for that as well.
You DO remember how pushbuttons work, right?
If not, check out this page first.
Let's see how the voltage changes with this common ps/2 electronics joystick!
Now for the moment of truth!
The moment you have been reading this for!
It's finally here!
Let's hook up the motor's positive voltage pin to our x axis pin of our joystick.
Let's hook up the motor's negative pin to our ground voltage of our battery.
Let's also make sure that our joystick's ground voltage shares the same ground connection.
We do this because electronics work better when they all share the same ground connection as a reference to what "zero" voltage and current flow is.
It's good practice so let's do it!
Now, our joystick with motor experiment should be hooked up like this.
Get your thumb on that joystick and get ready to move that x axis left and right in
What do you mean...it's "not working"?
It's not rocket science...I mean, we have it all hooked up right.
Maybe you did something wrong.
Check your wiring.
Yeah, check it again....
Ok...well...I'm pretty sure it was supposed to work.
Hmmmm. Maybe this wasn't your fault after all.
Alright, I asked JiF and he says we have to go really slow.
This time what happens when we go slow?
What, JiF? Slower?
Alright, let's slow it down even more.
I know how you are feeling right now...
What's going on here?
Not all motors will turn. It depends on the voltage and current ratings for each.
Even a cheap 3-6v hobby motor might not work well.
If only you could get better resolution out of that joystick, something that would spread out the range of voltage going to the dc motor a little better.
Why you want a microcontroller to control your motor with a joystick
Connecting a joystick directly to the dc motor will always give you inconsistent results. It will either start spinning with a little pressure or not spin until you push the joystick all the way to the left or right.
If you go really slow, you will see the very narrow range of control you actually have in trying to get it to gradually speed up and down.
The whole reason we wanted to use the joystick was for better control but the result ends up being underwhelming.
What we can do is take our knowledge that an analog (non-digital) electrical voltage is coming out of the x and y axis pins and combine that with our knowledge that microcontrollers, like those found on the Arduino or Raspberry Pi can take those analog values and convert them into digital ones.
We also know that these same microcontrollers can output electricity in pulses of varying lengths called Pulse Width Modulation. This would help us to gradually spin the motor up and down as we move the joystick along the x and y axis.
If you are new to PWM I have a few videos about it on my channel and also explanations here on this site.
But here is a picture for you.
Here is another illustration of the lengths of the electrical pulses increasing and decreasing.
Here is one more way to look at it.
The longer the pulse shooting out of the microcontroller's pin that we connect to our motor's positive connection the more momentum it gains as it spins. It's just like that old game kids played in the past where they would hit a wheel with a stick to make it keep spinning. That is exactly what we are doing with PWM.
To recap: The joystick outputs analog voltage values which the microcontroller converts to digital values which we can write code to vary the pwm pulses going out on a pin connected to our dc motor.
Yeah, it is complicated so don't feel bad if you are lost.