# Calibrating a Joystick Potentiometer

9,446

12

11

## Introduction: Calibrating a Joystick Potentiometer

I was trying to control the speed of an RC car using a Joystick Potentiometer, when i came across a problem. The centered joystick have a default value different than 0. It was the first time that I use this kind of potentiometer, so I started searching for a solution on how to calibrate this component and get a clear default analog value of 0 on start. I wasn't lucky ! So I decided to make my own Arduino sketch and then keep it in my notebook for the next time I needed to use this stick.

In this tutorial, I assume you know what a potentiometer is and how to use it, so I am gonna skip that part and just give you some information about the type we are using here and a simple coding way to calibrate this kind of potentiometer.

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: Step 1: How Does Joystick Potentiometer Work ?

This joystick is an analog similar to the ‘analog’ joysticks on the PlayStation 2 controllers. It's made of two potentiometers of a 10k value each, one for x-axis and the other for y-axis and a tactile button for select which is activated when the joystick is pressed down.

The two potentiometers are connected with a gimbal mechanism separating the horizontal and vertical movements. When in idle position, the stick is centered which means each potentiometer is in its center position or value. Back to axes notion, you can choose which potentiometer for which axis x or y randomly. In the picture above, you can see the way i chose my axes (for this example, the axes won't matter. You can choose any of the potentiometer to try the sketch).

As for the hardware connection, the middle pin of the potentiometer is for the analog data and the two others for power. It won't matter which pin would be connected to 5V or GND, it will simply affect the default data value a little bit but no change are necessary in the codes.

## Step 2: Step 2: Arduino Codes

Here are the codes for calibrating this kind of potentiometer.

/*
Date: ----------------------------------------- December 22, 2015
Title: ---------- JOYSTICK POTENTIOMETER CALIBRATION
Author: --------------------- By Firas Helou ------------------------

Learn how to calibrate a joystick potentiometer with Arduino Uno

*/

int defaultData; //Setting a variable for default potentiometer data on start
int data; //Variable to store data read on analog Arduino pin A0 coming from the potentiometer
int startTime = 0; //Time variable data type is defined as integer because we do not need more than 10 //microseconds
int TimeGap = 10; //Defining the time gap

int remap; //Remap variable to store the remaped data coming from the analog Arduino pin A0

//VOID SETUP() FUNCTION
void setup() {
//Setting Serial communication at default rate
Serial.begin(9600);
//We set time to define the default value read on the analog Arduino pin A0, because we have a centered joystick pin, //so its default value is different than 0
if ( micros() - startTime >= TimeGap ){
//Potentiometer connected to analog Arduino
}

}

//VOID LOOP() FUNCTION
void loop(){
//We set data value equal the one read on the analog Arduino pin A0
data = analogRead(A0); //In this case, the default value read on start is 488
if(data < defaultData + 1){
//We must remap the data value read on analog Arduino pin A0 to a smaller value between 0 and 255
//but we must subtract 1 from the default value, because if we don't, we will not have a default value of 0 when the //stick position is centered
//instead we will have a floating value between 1 and 0
//so to avoid that floating problem we simply subtract - 1
remap = map(data, 0, defaultData - 1, 255, 0);

}else if(data >= defaultData){
//In this part, we do not need to subtract anything
remap = map(data, defaultData, 1023, 0, 255);

}

//Serial print the values we receive
Serial.print("Remapped value: ");
Serial.println(remap);
Serial.print("Default value: ");
Serial.println(defaultData);
delay(200);

}

Let's say i chose the y-axis, by uploading the code and open the serial monitor, we must see a default value of 488 in my case (you might get a different value). This is the idle centered position for the stick so we must calibrate it to see a 0 value instead of 488 as a start. And this is where this sketch come in place. The picture above shows the results which i received after i uploaded the final sketch above.

This is my first instructable tutorial. I hope this would help you somehow. Feel free to add your questions in the comments section if you've got any or if you have a better way of coding it would be nice to check it out, which would help me to improve myself. If you find areas where improvements can take place in this please add it in the comments or send me a private message. It will be helpful for others same as for me.

## Recommendations  Large Motors Class

13,973 Enrolled

## 11 Discussions

Let's say that the joystick is in the leftmost position (and we assume that the defaultData is 512):

- remap = map (0, defaultData-1,255,0) = 255

Now place the joystick to the full right position:
- remap = map (1023, defaultData, 1023,0,255) = 255 again !?!?

So, there is no difference between the left and right position !!!

davorin, the difference is in the position. As we know the joystick default position is in the middle which means not 0, so when i remapped it i told it to become 255 if the position is at the left and 0 in the middle and then 255 in the right, it doesn't matter if - or + only for debugging, i used this program into
bigger one to control a steering, it worked perfectly :)

Your code make no difference between left and right on x axis, and up and down on y axis.

To make a difference, the code should be like this:

if(data < defaultData + 1){
remap = map(data, 0, defaultData - 1, -255, 0);

}else if(data >= defaultData){
remap = map(data, defaultData, 1023, 0, 255);

}

Now, the remapped value will be in range between -255 (most left) and 255 (most right).