Connecting an analog joystick to a netduino

Atari Joystick by Derek Erdman

The netduino project that I’m currently working on requires an analog thumb joystick for user input. Such parts can be scavenged from game console controllers (PS/2 or Xbox 360 for instance)  or can be bought from Sparkfun and Ada Fruit among others.

The joystick that I used in this article is Sparkfun’s and the datasheet for it is located here.

This 2-axis analog joystick is composed of two 10K potentiometers and features a push-button. Sounds like a piece of cake to work with, right? Well, it is but the devil is in the details as usual: each potentiometer is spring-loaded so that the joystick can return to the center when released.

If the springs aren’t perfectly balanced or with wear and tear, they will cause constant fluctuations in the readings even when the joystick is at rest. Even worse, the joystick might start drifting in one direction all the time!

For that reason, it’s important to calibrate an analog joystick at least once before using it so that the driver reading the potentiometer can compensate for the fluctuations.

The AnalogJoystick netduino driver that I wrote (see the links at the bottom of the post) does this automatically when an analog joystick instance is created by taking a bunch of sample readings and tracking the min / max values of the fluctuations, later using these are the ‘center’ area of the joystick. The caveat here is that the joystick must not be touched during that process. Fortunately, it only takes a fraction of a second to perform this calibration.

Connecting the joystick to the netduino

  • We’ll need to connect the potentiometer of the joystick to the analog pins on the netduino. I used the 3.3 volt power rail. Using analog pins first requires connecting the AREF pin of the netduino to the 3.3 volt power rail. Using the 5 volt power rail would work too, as long as the joystick and AREF are connected in a voltage divider configuration (v3.3 is the max on the netduino’s analog pins).
  • Designate one of the potentiometer as the X axis and the other as the Y axis. It does not matter which one is which.
  • Designate an analog pin on the netduino for the X axis (say GPIO_PIN_A0) and another one for the Y axis (say GPIO_PIN_A1).

Notice in the schematics that each potentiometer has 3 pins:

Connect them as follows:

  • Pot. pin # 1: netduino GND rail
  • Pot. pin #2: a netduino analog pin (GPIO_PIN_A0 / GPIO_PIN_A1 in this example)
  • Pot. pin #3: netduino v3.3 power rail

The switch can be connected to any given digital pin on the netduino. Lady Ada has a fantastic tutorial on how to do it properly so I won’t elaborate on the subject 🙂

The only thing that I will mention here is that I wrote an interrupt-driven  PushButton driver which will call back a user-defined method when needed, which is way more efficient than polling the digital input.

That’s it!

Action shot

You can see a video of the analog joystick used for moving a sprite on an LED matrix, building upon my previous posts discussing connecting an SD card to a netduino and driving an 8×8 LED matrix using persistence of vision:

Netduino analog joystick driver and test code

The analog joystick test code can be downloaded here.

Happy hacking!

The Atari Joystick artwork was used with permission from Derek Erdman. Thanks man! 🙂