Calibrate the Joysticks

Start 

Why We Need to Calibrate the Joysticks 

Each joystick uses two analog channels. One for the x-axis and one for the y-axis. The values on each channel change according to the joystick's position is a similar way to the potentiometer. As you twist the potentiometer you are changing it's resistance and therefore changing the voltage reading that goes to the Arduino.

The joystick also has a button feature. Normally another feature like this would need another channel but using some clever engineering we don't. The potentiometer goes from 0 to 1023. Each joystick channel only goes from about 200-800. When the button is pressed it overwrites the value on the x-axis to 1023. Knowing this, whenever we see 1023 on the x-axis channel of the joystick, we know that can only come from the button being pressed. We sacrifice some resolution in the positioning for the button feature.

We need to calibrate the joysticks because each one is going to have a different maximum and minimum value in that 200-800 range.

Upload

Upload the following code. The left joystick is in A0 and the right joystick is in A2.

Download file Copy to clipboard
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
    ________    _           __    __
   /_  __/ /_  (_)___ ___  / /_  / /__
    / / / __ \/ / __ `__ \/ __ \/ / _ \
   / / / / / / / / / / / / /_/ / /  __/
  /_/ /_/ /_/_/_/ /_/ /_/_.___/_/\___/
      ____        __          __  _              __ __ _ __     ___
     / __ \____  / /_  ____  / /_(_)_________   / //_/(_) /_   |__ \
    / /_/ / __ \/ __ \/ __ \/ __/ / ___/ ___/  / ,<  / / __/   __/ /
   / _, _/ /_/ / /_/ / /_/ / /_/ / /__(__  )  / /| |/ / /_    / __/
  /_/ |_|\____/_.___/\____/\__/_/\___/____/  /_/ |_/_/\__/   /____/
*/

//Change this to "right" when ready
String joystick = "left";

//Holds raw measurements
int xVal;
int yVal;

//Holds xAxis measurements
int xMax;
int xMin;

//Holds yAxis measurements
int yMax;
int yMin;

//Trigger for x or y measuring
bool measuringX = true;

void setup()
{
  //Start Serial communcation to computer
  Serial.begin(9600);
}

void loop()
{
  //If we are calibrating the left joystick
  if (joystick == "left") {
    //Use A0 and A1 for readings
    yVal = analogRead(A0);
    xVal = analogRead(A1);
    //If we are calibrating the right joystick
  } else {
    //Use A2 and A3 for readings
    yVal = analogRead(A2);
    xVal = analogRead(A3);
  }

  //If you are pressing the button for the first time
  if (yVal > 1020 && measuringX) {
    //Display current status
    Serial.println("___________________");
    Serial.println("Changeing to Y AXIS");
    Serial.println("___________________");
    Serial.println("Please stop pressing the Joystick");
    //Resets yVal. If this was not done it would be 1023
    yVal = 500;
    // Give user time to respond
    delay(2000);
    //Set this to change axis
    measuringX = false;
    //If you are pressing the button for the second time
  } else if (yVal > 1020) {
    //Print out all results
    printResults();
    //Purposefully get stuck in endless loop to stop measuring
    while (1);
  }

  //Checks to see which axis you want to measure
  if (measuringX) {
    //If the xVal is bigger than xMax
    if (xVal > xMax || xMax == 0) {
      //Make it the new Max
      xMax = xVal;
    }
    // If the xVal is smaller than xMin
    if (xVal < xMin || xMin == 0) {
      //Make it the new Min
      xMin = xVal;
    }

    //Print out current results
    Serial.print("The MAX X and MIN X are:");
    Serial.print(xMax);
    Serial.print(",");
    Serial.println(xMin);
    Serial.println(" ");
    delay(200);

  } else {
    //If the yVal is bigger than yMax
    if (yVal > yMax || yMax == 0) {
      //Make it the new Max
      yMax = yVal;
    }
    // If the yVal is smaller than yMin
    if (yVal < yMin || yMin == 0) {
      //Make it the new Min
      yMin = yVal;
    }

    //Print out current results
    Serial.print("The MAX Y and MIN Y are:");
    Serial.print(yMax);
    Serial.print(",");
    Serial.println(yMin);
    Serial.println(" ");
    delay(200);

  }
}

void printResults() {

  Serial.println("**********************************");
  Serial.println("Calibration Results for " + joystick + " Joystick");
  Serial.println("**********************************");
  Serial.print("The MAX X and MIN X are:");
  Serial.print(xMax);
  Serial.print(",");
  Serial.println(xMin);
  Serial.println(" ");

  Serial.print("The MAX Y and MIN Y are:");
  Serial.print(yMax);
  Serial.print(",");
  Serial.println(yMin);
  Serial.println(" ");

  Serial.print("**********************************");
}

How to Calibrate the Joysticks 

How to Calibrate the Joysticks

Open up the Serial Monitor. For each axis we will be rotating the joystick 2-3 times. This makes sure we hit the maximum and minimum values. Click the joystick and wait about 4 seconds then repeat. Click again and the Serial Monitor will print out the calibration results. Write this down somewhere. A post-it, text document, or screenshot. We are going to need these numbers. You're not done yet! change the joystick variable from left to right and repeat the whole process.

Starting with the Left joystick
Starting with the Left joystick
Rotate the joystick
Rotate the joystick
Rotate the joystick
Click joystick
Rotate the joystick
Rotate the joystick
Rotate the joystick
Click joystick
Changing code for Right joystick
Rotate the joystick
Rotate the joystick
Rotate the joystick
Click joystick
Rotate the joystick
Rotate the joystick
Rotate the joystick
Click joystick

Next Section 

With both joysticks calibrated and values written down let's jump to the next section.