Skip to main content

81. Utilizing Dual Axis Joystick Module as a Pointing device with Arduino and Python

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:

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

Popular posts from this blog

51. Buzz wire game using Arduino

Hello everyone! This is my first Arduino project in 2020 and it is going to be a fun and simple project. You would have heard of the Buzz Wire, a steady hand game, and today you will be learning to make one using Arduino. Hardware components used in this project Arduino Nano USB Type A to mini B cable (for Arduino Nano) Solderless Breadboard - Mini and Full-size LEDs (x2) - Green and Red Resistors (x2) - 220 Ω  Active Buzzer module (KY-012) LCD display module with I2C interface - 16x2 Male-to-Male Jumper wires (x4) - 10cm Female-to-Male Jumper wires (x5) - 20 cm Jumpers (x5) - to reduce the usage of wires Copper wire  Tape (or any form of insulation)  Setup Your hardware setup must look somewhat similar to the ones in the images above. The beginning and end of the copper wire maze must be taped to prevent conductivity between the wire loop and maze. Connections LCD display module with I2C interface GND - Ground VCC - 5V SDA - A4 SCL - A5

71. Buzz Wire Game ( Version 2.0 ) using Arduino

Hey Everyone! I hope that everyone's safe during this pandemic. Today I am going to explain you about the second version of my Buzz wire game. People who are bored of staying indoors can find this game pretty interesting and fun to play.  If you are a beginner, you can start off with my first version of this project :  51. Buzz wire game using Arduino   Hardware components used in this project Arduino Uno Solderless Breadboard (x2) - Full/ Full+ LCD display module with I2C module - 16x2 Potentiometer - B20K Push-buttons (x3) Resistor - 220 Ω Active Buzzer module (KY-012) LED - Red Copper Wire - 19/ 20 gauge thick Male-to-Male Jumper wires - 10cm and 20cm Wire - Long enough to connect the loop of copper wire to ground Other tools required for this project Cutting pliers - To cut the right amount of copper wire Round-nose pliers - To bend the copper wire and make a maze and loop Hardware setup Connections Wire Maze - D2 Wire loop - Ground (GND) B20K Potentiometer S - A0 (+) - 5V (-)

86. RFID Health tag (Arduino and Python)

 Hey everyone, Sorry I have not uploaded in a while. Today, I will be sharing an interesting project with all of you. This RFID Health tag project is useful when it comes to keeping track of vaccinated individuals, their biodata and their health conditions and medications. For this project, you will be needing Arduino and Python. Read on further to see how I did this project. Hardware components used in this project Arduino Uno Solderless Breadboard - Half+ MRFC522 RFID reader RFID key tags (x5) Push-buttons (x2) Male-to-Male Jumper wires (x12) USB Type A/ B cable (for Arduino Uno) Software required Arduino IDE - latest version recommended Python 3.8 Schematic MFRC522 RFID reader SDA/ SS - D10 SCK - D13 MOSI - D11 MISO - D12 IRQ - Not connected GND - Ground RST/ RESET - D9 3.3V - 3.3V Push-buttons Submit button - D4 Retrieve button - D5 Coding Arduino For this project, you will be using the following libraries: MFRC522 by miguelbalboa -  https://github.com/miguelbalboa/rfid SPI - In-bu