Introduction: Easy Analog Joystick Calibration and Centering

Picture of Easy Analog Joystick Calibration and Centering

If you ever tried to control something with a joystick on Arduino you'll probably have noticed that the joysticks are not centered and calibrated. To solve this problem can be a real pain. To make it easier for you to control something with a joystick, I made this instructable. I will show you how to connect, calibrate and use your joysticks the easy and correct way.

Step 1: Stuff You Need

  • Arduino with USB cable and PC
  • analog joystick with optional push button
  • some dupont wires
  • Arduino IDE

Step 2: Connecting the Joysticks

Connect the joystick(s) to the Arduino like this:

  • vcc --- 5V (or 3V3 on 3V3 boards)
  • GND --- GND
  • X --- analog 0 (any analog pin)
  • Y --- analog 1 (any analog pin)
  • switch --- digital 2 (any digital pin)

You can connect the X and Y to any analog input as long as you define the X and Y pin. You don't have to connect the switch to use the joystick.

Although almost everyone tells you to connect buttons using a pull-up resistor, you don't have to connect the button using a pull-up. Just connect the button to any digital pin. We are using the build-in pull-up resistor on the Arduino to pull the signal up by default.

Step 3: Uploading the Sketch and Defining Pins

Download the sketch which is included in this step and open it.

Define the pins for the joysticks or leave them as default when connected to A0 and A1

upload the code and follow the instructions on the serial monitor to calibrate your joystick

Step 4: Calibrating

Calibrating joysticks is really easy with this code, all you have to do is center the joystick , push it to the bottom left and to the top right corner. The joystick takes 100 readings in each position and calculates the average value and offset. The code does also recognize the joystick limits being less than 0 or 1024 and creates new limits, this way you can still use the range of 0-1024. if you hold the joystick half way the limit you can even make it give values of over 1024 or below 0.

An other feature is the possibility to invert the axis, to do this, just push it to the top right corner when asked to go to the bottom left(or top left if you only want to invert the Y axis)

If this code is popular, I will add the possibility to add more joysticks and other useful options like EEPROM storage for the calibration values so you don't need to calibrate each time you restart your code. If this code becomes really popular, I'll make a library for it for even better support and updates. If you have a nice idea or improvement please leave a comment

Comments

StephenR159 (author)2018-01-07

Hi there, I just tried to compile and upload this code and got an error on the second Y = mapfloat definition. It keeps telling me it was not declared in this scope. I know it's vague, but if you look at the code you'll see the line I'm talking about. Anyway, I'll try to upload an image in a bit but if you had any ideas I'd be grateful. Thanks :)

Joram_ (author)StephenR1592018-01-10

Hi, I think I solved this problem. It is caused by the map_float function being surrounded by '/* */' and not being active. You can activate this function at the bottom of the code (last 4 lines) and it should work, instructions are also in the first lines of the code.

StephenR159 (author)Joram_2018-01-12

Hi, I apologize for not reading the comments at the top! It totally is included and I embarrassingly just read it. Thanks for your help, despite my ignorance. :)

I'm basically fresh off the boat with this arduino thing, but I'm excited to keep learning. I appreciate your desire to help people with issues like this.

Thanks again!

NormanV1 (author)2016-07-08

Thanks a lot....very useful!

About This Instructable

1,369views

26favorites

License:

Bio: You can always make it more dangerous.
More by Joram_:temperature compensated ultrasonic range finder with ArduinoEasy Analog Joystick Calibration and CenteringHow to Upload Sketches to an Arduino With a Broken Usb Port / Chip
Add instructable to: