Introduction: Arduino Thumbstick Controlling Computer Cursor

Picture of Arduino Thumbstick Controlling Computer Cursor

This is a test project i made to improve my programming with c++/c# and processing java language.

This project is about controlling your computer cursor with the joystick that connected to Arduino.

Although it's not fully complete yet but it will still work as i wanted.

Step 1: Prepare

Picture of Prepare

What you will need for this project is :

  1. Any kind of Arduino ( i am currently using Nano )
  2. Thumbstick ( i ripped it off from my old PS1 controller )
  3. Few jumper wires
  4. Basic programming knowledge

You will also need Visual Studio and Processing for this project.

Step 2: Make Your Joystick

Picture of Make Your Joystick

I follow the adafruit's joystick copper line to make my own on the perf board.

This is optional if you already have Adafruit's Joystick.

Step 3: Connection

Picture of Connection

This is the connection i made using microsoft paint.

Connect Y to A0 on Arduino pin

Connect X to A1 on Arduino pin.

Step 4: Programming Arduino

Picture of Programming Arduino
#define xPin A1
#define yPin = A0

#define buttonPin 12

int xPosition;
int yPosition;
int button;

void setup() {
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(buttonPin, INPUT);

void loop() {
// example value 500,500,0; -- x,y,button -- data[0],data[1],data[2]
//delay(100); // to see serial value properly

void buttonState() {
button = digitalRead(buttonPin);
if (button == HIGH) {
digitalWrite(13, HIGH); .// indicating button is pressed.
}else {
digitalWrite(13, LOW);
void readX() {
xPosition = analogRead(xPin);
xPosition = map(xPosition, 1023, 0 , 0, 1920); // prevent invert axis.

void readY() {
yPosition = analogRead(yPin);
yPosition = map(yPosition, 0, 1023, 0, 1080);

Step 5: Programming Processing for Testing

Picture of Programming Processing for Testing
import processing.serial.*;
Serial port;
String serial;
PFont f; 
String x,y;
float xPos, yPos;
void setup() {
  f = createFont("Arial",16,true);
  port = new Serial(this, "COM3", 9600);
  serial = port.readStringUntil('\n');
  serial = null;
void draw() {
  fill(255, 0, 0); 
  text("Developed by FirmanJamal",10,23);
  text(xPos, 920, 23);
  text(yPos, 920, 43);
  while(port.available() > 0) {
      serial = port.readStringUntil('\n');
  if (serial != null) {
       String[]a = split(serial, ',');
       x = a[0];
       y = a[1]; 
       xPos = float(x);
       yPos = float(y);
void cursor() {

Step 6: C# Moving Cursor

Picture of C# Moving Cursor
using System;<br>using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Windows.Forms;
using System;
using System.Runtime.InteropServices;
namespace Serial_Mouse_Thumbstick
    class Program
        static void Main(string[] args)
            Console.Title = "Developed by Firman Jamal";
            SerialPort port = new SerialPort();
            port.PortName = "COM3";
            port.BaudRate = 9600;
            int x, y, button;
                string message = port.ReadLine();
                string[] newData = message.Split(',');
                //Console.WriteLine("X COORDINATE : " + newData[0] + " Y COORDINATE : " +newData[1]+" BUTTON : " +newData[2]);
                x = Convert.ToInt32(newData[0]);
                y = Convert.ToInt32(newData[1]);
                button = Convert.ToInt32(newData[2]);
                if (button == 1)<br>                {
                    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
                    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
                Cursor.Position = new System.Drawing.Point(x, y);
                Console.WriteLine("X: " + Cursor.Position.X + " Y: " + Cursor.Position.Y + " Button : " + button);
            } while (true);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;

Be sure to add System.Drawing at the references.

Step 7: Final Test

Congratulation if your test works perfectly as mine, feel free to upgrade the codes!


Lab290A (author)2015-01-28

One option to increase the movable area is to use the map function again to remap the 0-1023 value to your overall screen size (0-1920 for instance)

FirmanJml (author)Lab290A2015-01-28

Oh wait, it actually works

João Severino (author)2015-01-28

If you had access, you could simplify and use an Arduino with an Atmega32u4 such as the Arduino Leonardo.

Using the built in Mouse and Keyboard libraries to do this, you wouldn't require any additional software running.

The Arduino would be recognized as a standard mouse

About This Instructable




More by FirmanJml:Arduino Thumbstick controlling computer cursorInterfacing c# with Arduino
Add instructable to: