Today, I am going to show you how to use an Arduino Joystick module to control a couple of servos. The idea behind the project is to have a stand where you could place a camera or something else and have full control using the joystick.
You only need a few minutes and very simple components to finish the project. Here you have a video in case you want to take a peek at the final result.
What is a Joystick?
Let’s start with the basics…what is a Joystick?
For this project, we are going to use an Arduino Joystick module. It is a basic use of the module just for learning purposes but there are endless applications for it, especially in the space of robotics.
This Arduino joystick module is very similar to what you would find on many game controllers but if we look under the hood, all the magic happens thanks to 2 potentiometers, one for the X-Axis and another one for the Y-Axis.
The potentiometers are actually used to control 2D movement by generating analog signals. Some modules also have a push button that can be used for special events.
The Joystick module is a gadget that translates your hand movement into electrical signals, in other words, the joystick translates entirely physical movements into a digital form that can be interpreted by our controller, the Arduino in this case.
The old fashion joystick above is nothing but two potentiometers that allow us to measure the movement of the red stick in a double dimension.
The potentiometers are basically variable resistors which are used in the joystick module and act as sensors that provide a variable voltage depending on the movement of the joystick around its shafts.
Arduino Joystick Module
The Arduino Joystick module is just like any joystick with the advantage that it has pins made available to easily pass the signals to the microcontroller, Arduino in this case. We can control the X axis, Y axis and Z dimensions (the button for special events) using this joystick module.
As explained earlier, the potentiometers are used to handle the X and Y axis while the button is just a switch. The data pins for the X-Axis and Y-Axis dimensions are analog input signals while the Z dimension pin uses a digital input button. That is why, X and Y ports connect to analog pins of Arduino, while the Z port connects to digital pin of the microcontroller.
When you rotate the joystick arm, you are changing the resistance in the potentiometer. If the joystick stick is on the opposite direction of the path from the input connection terminal, then, the signal will experience maximum resistance.
Changing the resistance of the potentiometer changes the current in the connected circuit. In this way, the potentiometer converts the physical position of the rod into an electrical signal and passes it to the joystick port on the controller.
What is a servo motor?
The other component that we will be using today is a servo motor.
Servos are small and efficient motors that can be positioned very precisely with the use of a microcontroller. These features make them very useful for applications like toy cars, robots, and airplanes. They can also be used in industrial applications, robotics, in-line manufacturing, pharmacy, and pretty much anything you can imagine.
The servo circuit is built into the motor unit and has a positional shaft, usually equipped with gear. The motor is controlled by an electrical signal that determines the amount of motion of the shaft.
How does the servo motor work?
Servo motors are basically controlled by sending an electrical pulse of variable width. There are minimum pulses, maximum pulses, and repetition rates. Servo motors can usually only be rotated 90° in either direction for a total of 180° although you can find some that are able to rotate 360 degrees.
The neutral position of the motor is defined as the position at which the servo has the same amount of potential rotation in the clockwise or counterclockwise direction. The PWM signal sent to the motor determines the position of the axis and is based on the duration of the pulse sent through the control pin.
The servo expects to see a pulse every 20 milliseconds (ms), and the pulse length will determine the distance the shaft rotates. For example, a 1.5ms pulse will cause the motor to go to the 90° position. Move it counterclockwise to the 0° position for less than 1.5ms. Any position longer than 1.5ms will cause the servo to rotate clockwise toward the 180° position.
You don’t need to know all these things of course becuase we will be using a library to control the servo.
Arduino Joystick Project
The arduino joystick module provides a value from 0 to 1023, that value can be scaled and turned into a number of degrees from 0 to 180. The X-Axis of the joystick will be controlling one of the servos while the Y-Axis will control the other.
The final result will be a stand with 2 servos mounted, one of them will control the left/right movements and the other will do up/down. For the project, I am using a camera stand to facilitate mounting the servos but it is entirely optional.
Shopping List
Here is the list of components that you need to build the project. The camera stand is entirely optional but it will make your life easier.
Description | Units | |
Arduino UNO | 1 | BUY |
Servo Motors Sg90 | 2 | BUY |
Joystick Module | 1 | BUY |
Dual Servo Camera Stand | 1 | BUY |
Jumper Wires | 1 | BUY |
Let’s Start Building…
Arduino Servo Motor Connections
The first part is to connect both Servos to the Arduino. The connections are very simple, you only need to connect power and ground and the cable that drives the servo.
Servo Up/Down | Arduino | Servo Right/Left | Arduino |
Red Cable | 5 V | Red Cable | 5 V |
Black Cable | GND | Black Cable | GND |
Yellow or White Cable | PWM (4) | Yellow or White Cable | PWM (10) |
Check the diagram below for more details.
Arduino Joystick Connection
The diagram below might be a little bit confusing if you got the module from the link I gave you. Your Arduino joystick probably has 5 pins exposed to make things simple. If you get confused, go straight to the table after the picture.
Joystick | Arduino |
5V | 5V |
GND | GND |
U/D U/D (Y-Axis) | A4 |
L/R L/R (X-Axis) | A3 |
There is an extra pin generally called SW which sends a digital signal when the joystick is pressed but we are not using it for the project.
Once everything is connected it will look more or less like the picture below. I have added an ultrasonic sensor to give you an idea of things that you can mount in the stand but it doesn’t have any purpose.
Arduino Joystick Servo Controller Source Code
The source code is extremely simple but I have added comments on each line to make you that you don’t miss anything. Keep in mind that you need to add the servo library to be able to compile the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#include <Servo.h> const int servo1 = 3; // Pin for the first servo const int servo2 = 10; // Pin for the second servo const int joyH = 3; // L/R Parallax Thumbstick const int joyV = 4; // U/D Parallax Thumbstick int servoVal; // variable to read the value from the analog pin Servo myservo1; // create servo object to control a servo Servo myservo2; // create servo object to control a servo void setup() { // Servo myservo1.attach(servo1); // attaches the servo myservo2.attach(servo2); // attaches the servo // Inizialize Serial Serial.begin(9600); } void loop(){ outputJoystick(); // Display Joystick values using the serial monitor // Read the horizontal joystick value (value between 0 and 1023) servoVal = analogRead(joyH); servoVal = map(servoVal, 0, 1023, 0, 180); // scale it to use it with the servo (result between 0 and 180) myservo2.write(servoVal); // sets the servo position according to the scaled value // Read the horizontal joystick value (value between 0 and 1023) servoVal = analogRead(joyV); servoVal = map(servoVal, 0, 1023, 70, 180); // scale it to use it with the servo (result between 70 and 180) myservo1.write(servoVal); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there } /** * Display joystick values */ void outputJoystick(){ Serial.print(analogRead(joyH)); Serial.print ("---"); Serial.print(analogRead(joyV)); Serial.println ("----------------"); } |
After uploading the code to your Arduino UNO, you should be able to have full control over your servos. I hope you enjoyed the project!