loading

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.

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
defaultData = analogRead(A0);
}

}

//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.

<p>how do i use the code? my remapped value is 14 and default value 482</p>
<p>Let's say that the joystick is in the leftmost position (and we assume that the defaultData is 512):</p><p>- analogRead returns 0<br>- remap = map (0, defaultData-1,255,0) = 255<br><br>Now place the joystick to the full right position:<br>- analogRead returns 1023<br>- remap = map (1023, defaultData, 1023,0,255) = 255 again !?!?<br><br>So, there is no difference between the left and right position !!!</p>
<p>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 <br>bigger one to control a steering, it worked perfectly :)</p>
<p>use the codes as it is and then Serial.print the last value and let me know what you get ? you should have a little difference of 6 or 5 from mine </p>
i got the remap value 0 and default value 482.<br>but how am i going to make it at 90 degree if i map the servo between 0,180. i usually get 88 or 84<br>
<p>if you got 0 then job done :) that's the point of my tutorial, now about servos, i worked with them very little long time ago so i can't give you the answer, you have to make a search on google or here on instructables by typing for example servo and joystick pot using arduino </p>
<p>okay thanks for your help</p>
<p>Your code make no difference between left and right on x axis, and up and down on y axis.</p><p>To make a difference, the code should be like this:</p><p>if(data &lt; defaultData + 1){ <br> remap = map(data, 0, defaultData - 1,<strong> -255, </strong>0);</p><p>}else if(data &gt;= defaultData){ <br> remap = map(data, defaultData, 1023, 0, 255); </p><p>} </p><p>Now, the remapped value will be in range between -255 (most left) and 255 (most right).</p>
<p>hello davorin.perkovic and thank you for your comment, actually mine is right and yours is right too, in fact there is many changes that can be done for the codes and still works properly. What I did is i reversed the data so it works totally fine. What you did is simply showing that you are in -x or +x</p>
<p>Great first instructable! Thanks for sharing and welcome to the community!</p>
<p>thank you tomatoskins i appreciate your comment :)</p>

About This Instructable

2,949views

10favorites

License:

More by firashelou:Calibrating a joystick potentiometer  
Add instructable to: