Search

Can a Joystick Control a DC Motor?

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.


Since I'm so nice and will continue to give you free code for the Atmega328p and the Raspberry Pico for electronic and robotics projects, go ahead and subscribe to me on Youtube and Odysee right now!









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!




ATTENTION



I am going to show you:

  1. How a potentiometer works

  2. How it's output of electricity affects the spin of a DC motor

  3. How a joystick works with a DC motor

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



Simple Hookup of DC Motor to Battery

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





Wait. What?

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.

Hold on...





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.



Why the transistor is the key to controlling your motor with a joystick




Even after hooking up all of the parts we have talked about so far and you have your code set up correctly you will find that the microcontroller pin doesn't put out strong enough voltages to spin the motor.


DAMNIT!


We tried so HARD and got so FAR but in the end it doesn't even mat~


Get used to this feeling. The frustration. This is what it feels like coding and hooking up electronics.


When you feel defeated you have only one option left.

Activate your trap card.



The transistor is your trap card here!




Transistors act as gates for a larger current of electricity that is ready to flow to our motor.


Think of the D as the leg connected to the motor.

Think of the S as the leg connected to our negative ground of our battery.

Think of the G as the middle leg connected to the microcontroller pin sending out the electrical pulses.


Each pulse opens that gate allowing the much stronger current from the battery pack to flow through the motor than the microcontroller can provide.


When our microcontroller sends out the tiny ~3v current out on one of its pins, it triggers the gate inside of the transistor to allow the much larger current waiting to flow through it! These are the basics of power amplification. This is essential to your progress so remember it.


Transistors are incredibly simple yet their applications are immensely important and powerful.


Some transistors are triggered by positive current (PNP) and some by negative (NPN).


I'm going to use an NPN transitor. Refer back to the picture of a transistor.

The outer legs are connected to negative ends.

We can connect the first leg (N) to the negative connection of our battery source.

We can connect the last leg (N) to the negative leg of our motor.

We can connect the middle leg (P) to the positive electrical pulsing pin we chose in our code on our microcontroller.


Re-read that again.

What this does is whenever the little pulse comes out of the microcontroller's pin it opens the path for the much stronger current of our battery pack to flow to the dc motor.


Another amazing fact about transistors is that you can put two of them so the same legs share the same rails on a breadboard and you can handle even more current! The transistors will automatically share the current load zipping through them if the first transistor reaches its limit!

Imagine what you could do with a whole rack of these that are larger and can handle massive amounts of current!


Some heavy duty machines have a bunch of these hooked up to move heavy torque motors, etc.


See the power and importance of transistors now?

If you are a fan of Iron Man check out the original comic book.

When he is escaping imprisonment in Vietnam after being captured following an explosion he talks about the power of his transistors as power amplifiers allowing him to blast his way out of there and become the Iron Man.


Check it out!







Here is the breadboard hookup for experimenting with the DC motor, the analog joystick, the micro controller, and the transistor.



Pay close attention to the connections, they were very confusing to me for several years. LOL. I hope to help you understand in this one post and move on with your life!


In my code and makefile for my Arduino Uno Atmega328p chip which you can download here


MotorJoystick
.zip
Download ZIP • 19KB

I have the Analog to Digital converter taking the input from the joystick on the pin connected to the pink wire. I then attempt to convert that new digital value into the lengths of the pulses so that as I move the joystick left and right it increases and decreases the length of the pulses causing the motor to spin faster and slower.


Even if you don't have this chip, you should study the code to understand what is going on. It's much easier to fix bugs in your code in a higher language like Python with a Raspberry Pi or Pico if you understand what happens in a lower machine type language like C. The higher languages hide much of this from you and if you have no idea how any of this works, you will miss out on easy solutions later on.


This is tricky stuff and why I saved this lesson for last in my basic beginner series.

I will always refer back to posts like this one and the basic lessons on this site in the future. If I didn't then it would be almost impossible to teach you how the stuff I make in the near future works.


I care so much that I delayed my 3d design and 3d printing build for a few years because I am an experienced teacher and will always be.


Did you know I used to teach kindergarteners almost every weekday?


I ran a successful, competitive language school in South Korea teaching English and other subjects to all ages for a decade and fell in love with electronics halfway through.


I can tell you more about all that later!