Introduction: How to Make a Basic Computer Mouse Using the Joystick Module and Arduino Uno

This instructable will show you how to make a joystick controlled mouse using Arduino Uno and the joystick module.

Supplies

You will need-

  • One Arduino Uno
  • 5 jumper wires
  • One Joystick
  • Arduino IDE
  • Python

Step 1: Python Program

Download python from - https://www.python.org/downloads/

Then copy the file location showing in your files. 


Open command prompt and enter the following-

1. cd <paste the path of the python file you previously copied from your files i.e the file location>

2. py –m pip install –-upgrade pip

3. py –m pip install mouse

4. py -m pip install pyserial

Step 2: Make the Circuit

Make this circuit shown using your Arduino and Joystick.

Step 3: Uploading the Arduino Sketch

//The program that you can copy onto the Arduino IDE is-

void setup() 
{
  Serial.begin(9600);
  pinMode(9,INPUT);
  digitalWrite(9,HIGH);

}
int prev_state=0;   
void loop() {
  int z=0,xpos=0,ypos=0;
  int x=analogRead(A0);
  int y=analogRead(A1);
  int sensitivity=10;   
  if(x>=550)                    
  xpos=map(x,550,1023,0,sensitivity); 
  if(x<=450)                   
  xpos=map(x,450,0,0,-sensitivity);   
  if(y>=550)                    
  ypos=map(y,550,1023,0,sensitivity);  
  if(y<=450)                  
  ypos=map(y,450,0,0,-sensitivity); 
  int curr_state=digitalRead(9);
  if(curr_state==1 && prev_state==0)  
  z=1;
  else
  z=0;
  if(xpos!=0 or ypos!=0 or z==1) 
  {
  Serial.print(xpos);    
  Serial.print(":");
  Serial.print(ypos);
  Serial.print(":");
  Serial.println(z);
  }
  prev_state=curr_state;
  delay(10);
}

Step 4: Changing the Com Port in the Python Program

Paste this program in a Notepad -

import mouse, sys
import time 
import serial

mouse.FAILSAFE=False
ArduinoSerial=serial.Serial('com3',9600)  
time.sleep(1)               

while 1:
  data=str(ArduinoSerial.readline().decode('ascii'))  
  (x,y,z)=data.split(":")      
  (X,Y)=mouse.get_position()     
  (x,y)=(int(x),int(y))              
  mouse.move(X+x,Y-y)      
  if '1' in z:             
   mouse.click(button="left")  
   

Change the com3 part of the program to the specific port which is applicable to you. Save this file as Python.py in Notepad.

Step 5: Open the Python File

Open the python file in the Python software you just saved i.e. Python.py

Step 6: Upload the Program

Click on the Run tab and click on Run Module to run the program on the Arduino Board

Step 7: Your Program Is Ready to Go!

Once you finish all this steps you can use the joystick to move around the mouse pointer and if you click hard then it also has a clicking function.

Step 8: Understand How the Joystick Works and Understanding X and Y Axis

If you want to learn more about x and y axis's you can visit - https://www.math.net/x-and-y-axis.

How the Joystick works -

A joystick plainly consists of two potentiometers aligned in the x and y direction which I have given an explanation link for. The Arduino Uno reads Analog (we fixed the x and y pins in analog pin 0 and 1) values from the Joystick in the range of 0 to 1023. Thus, when the joystick is in its default position e.i. the center, the Analog value also becomes close to 510 - 511 (between 0 and 1023).

Made with Math Contest

Participated in the
Made with Math Contest