Hey everyone
Today I will be explaining about my new project. In this post, you will be learning to utilize your Dual Axis Joystick module as a pointing device with the help of Arduino and Python.
Hardware Components used in this project
- Arduino Uno - You could use any other development board
- Dual Axis XY Joystick Module (KY-023)
- Male - to - Female Jumper wires (x5)
Software apps
- Arduino IDE
- Python IDLE
Setup
Schematic
- GND - Ground
- VCC - 5V
- VRx - A0
- VRy - A1
- SW - D3
Code
Before moving on to coding, you will need to understand how the Joystick module works. To learn about the module, please visit this page: How 2-Axis Joystick Works & Interface with Arduino + Processing.
You will need to use both Arduino IDE and Python IDLE for this project. Before moving on to the coding of this project, let me guide you to install the Python software along with some important python modules.
Download the software from here. I would recommend you to download the latest compatible version for your OS. In this project, I will be using Python 3.8.1.
Next, you will need to install the PyAutoGUI module and pyserial module to make this project work. Download these modules by following the hyperlinks below:
- PyAutoGUI module - Allows you to control the mouse and keyboard via the python scripts.
- pySerial module - Allows you to access the serial port
Installing PyAutoGUI and pySerial modules
You can skip this step if you have already installed the software and the modules. Install the python software and open command prompt. Change the directory to the folder where you have installed the software. PIP is a packet management system that lets you install and manage software packages written in python. You will need to upgrade PIP before you install these modules. In order to do this, you will need to use the following function:
python - m pip install - upgrade pip
Install the PyAutoGUI module by typing the following function in your command prompt, after PIP is upgraded:
python - m pip install pyautogui
Repeat the process to install the Pyserial module after the installation of PyAutoGUI module is completed. You will be needing the following function for this:
python - m pip install pyserial
Arduino
The Joystick module produces analog readings which range from 0 to 1023 depending on its position. When it is not moved, it will produce a reading of 512 for both X and Y. The wires used for connecting the Joystick module to the development board and the voltage supply of the development board can affect your readings.
The image shown above can be helpful to you when programming your Arduino Uno. You can also try to obtain readings using the Joystick module and write them down to get the range of readings produced when the Joystick is moved in each direction. You can use this code to obtain the readings from the Joystick module.
The Arduino microcontroller will be programmed to print in values in the range from -20 to +20. Now, you can change the value from 20 to any number you wish but you have to keep in mind that this will be the sensitivity of your pointing device. If your values are fluctuating, I would recommend you to keep the value within 10 - 20.
We will also print in the status of the button to give us readings when the joystick module is pressed and released. The x, y direction values and the button status will be printed together with a colon between them, like this ---> 10: -10: 1
Keep on reading to understand why I am printing the values in this way.
If you have followed the first step in this section, you would have obtained the range of readings when the joystick module is moved.
Now, you have to set threshold values so that the microcontroller can map the analog readings to the sensitivity levels.
Create global variables for the button and its state. You will also need to create global variables to store the readings for X and Y and their mapped values.
Within void setup( ), set the data rate in 9600 baud (bits per second) for serial data transmission. Call pinMode( ) to configure the Joystick button as INPUT and set it to HIGH using the digitalWrite function.
Within void loop( ), store the state of the button in the 'state' variable. Use analogRead function to obtain the analog values produced from VRx and VRy pins, and store them in the their respective variables.
According to the image shown in this section, the values for Y axis will increase when moved upwards. When the Y values exceeds the upper threshold, the values should be mapped to the sensitivity values. But make sure that the extreme value for Y value should be mapped to -sensitivity. It can be confusing but you will understand when you learn the next section 'Python'.
The Y values will decrease below the lower threshold when you move the joystick downwards. When this happens you should map the Y values. When the Y values reach zero, it should be mapped as -20 (the minimum sensitivity value).
The X values will decrease when you move the joystick to the right (as shown in the image above). When the X values decrease below the lower threshold, the values should be mapped to the sensitivity values. The VRx pin will give 0 when the joystick is moved to extreme right. But this value should be mapped to 20 (the maximum sensitivity value) so that the cursor would move to the right.
The X values will exceed the upper threshold when you move the joystick to the left. When this happens, you should map the X values. When the X values reach 1023, it should be mapped as -20 (the minimum sensitivity value).
If the button is pressed, the value stored in 'z' variable should be 1 or else it should be 0.
If the joystick is moved, when the X or Y mapped values are not zero or when the button is pressed, the Serial monitor should print in x, y direction values and the button status together with a colon between them as already mentioned above.
Python
Now let's move on to the coding in Python. Open the Python IDLE and open a new file. Import the modules which we installed in the first step along with the time module. You will be needing the time module to use delay function. Create serial port object and open serial port. You can find pg.11 of this documentation useful for this process. Wait for 3 seconds while the communication establishes.
You will be needing the Mouse control functions from PyAutoGUI module, so this website can be helpful in learning the functions of this module.
The functions should be entered within the while loop when it is true. Use readline function to read the incoming message via Serial communication and decode it into ascii as you will need to convert a segment of this to integer type. Convert the result to str data type. The split string method should be used to split the x, y mapped values and button state and the colon character should be used as the separator. The result will be a list with the separated values as the elements. Create separate variables to store the elements.
Create x and y variables and store the separated elements in these variables after converting them to integer type.
Call move function from the pyautogui module and set the x and y variables as the parameters.
The joystick module button should mimic the left button of the mouse so if the value stored in the z variable is 1, call the click function from the pyautogui module and give 'button='left'' as the parameter.
Now you have completed the python coding.
If anyone has any questions with the coding, please feel free to comment below.
Final Look
If anyone has any questions, or suggestions, about this project, please feel free to comment below.
Comments
Post a Comment