Joystick analog to digital conversion


I've been up to calibrating ADC values using a joystick to control the movement of a RC car in x and y directions. I have successfully wired up my joystick and I am able to read the values using the analogRead() method, but the problem is that even though I do not move the joystick, the middle value keeps fluctuating just like the serial output I have attached below. As long as I am correct, the ADC values of the joystick at their original position for both x and y directions should be between 511 to 513, which is the mid-value of the 10-bit ADC. The fluctuation makes it difficult for calibration. Is there any way to make this fluctuation stop? Thank you for your help in advance.

22:56:08.406 -> x:491
22:56:08.406 -> y:511
22:56:11.411 -> x:499
22:56:11.411 -> y:503
22:56:14.437 -> x:492
22:56:14.437 -> y:496
22:56:17.410 -> x:491
22:56:17.410 -> y:502
22:56:20.435 -> x:495
22:56:20.435 -> y:511
22:56:23.447 -> x:495
22:56:23.447 -> y:501
22:56:26.422 -> x:492
22:56:26.422 -> y:498
22:56:29.429 -> x:494
22:56:29.429 -> y:491
22:56:32.443 -> x:494
22:56:32.443 -> y:509
22:56:35.452 -> x:493
22:56:35.452 -> y:513
22:56:38.421 -> x:493
22:56:38.454 -> y:500
22:56:41.438 -> x:493
22:56:41.438 -> y:506

Correct about what?

The mid-value

Several, and for the right one(s) to be picked, it needs to be established why they fluctuate. There can be a number of sources of such variations, such as:
1 Variable voltage on the pot meter that constitutes the working end of the joystick
2 Variable analog reference voltage in the microcontroller
3 Mechanical jitter; i.e. the paddle of the joystick not being perfectly still/vibrating
4 Pot meter noise, especially in older/worn pot meters
5 Noise arising from any analog amplification circuitry if used
6 Inappropriate sampling approach

Probably a few more...

I'm going to take a wild guess (since you haven't provided much information yet) and assume that #5 is not relevant in your situation. I'll also disregard #3 and #4 (at least to an extent) and make a second assumption, i.e. that you've decided to not use defective equipment for your project :wink:
That leaves #1, #2 and #6.

#1: Voltage instability. Ensure proper power supply decoupling by placing a 100nF capacitor across the upper end of the joystick pot meter (the end that is connected to the positive power supply, typically 5V or 3.3V). I'd also recommend placing something like 10nF to 100nF across the wiper of the pot meter (i.e. the end that goes into your ADC pin). This reduces noise and also reduces the output impedance of the pot meter so you don't run into problems with spurious ADC readings in case of rapid joystick movements. But first and foremost make sure to use as stable a power supply as possible.

#2: Unstable analog reference. The ADC in your microcontroller essentially compares its readings against a fixed reference. By default in a typical Arduino this reference is set to the microcontroller's supply voltage. In terms of noise, this is unfortunate since it tends to be somewhat noisy, and hence, even if the absolute reading remains the same, if the reference against which it is compared fluctuates (even a little bit), the outcome will fluctuate with it. It's better to use a fixed/reliable external reference for both the upper end of the joystick pot meter and the analog reference. You will find an Aref pin on your Arduino board. In the software you must set the external reference as explained in the Arduino documentation (google it; easy to find). A decent analog reference voltage could be made with e.g. a TL431 or another purpose-made voltage reference, or even something like a LT1117-3.3.

#6: Inappropriate sampling regime: by now you've probably realized that theory is one thing, but this is the real world. And guess what: in the real world, 50% is not always 50.00000000%. It often ends up being 50.1% or 49.76423% or something. You can work hard to fix that (see above) and get it right to as many as possible decimals. But sometimes you have to throw a little statistics at the problem to smooth things out. So if you find that the median of your measurements looks actually pretty much OK, but the individual samples deviate from it, you just take several measurements and average them. So instead of relying on a single ADc reading, take for instance 32 or so and calculate the average. That's bound to be a pretty reliable value you end up with, as long as you didn't f&^* up too badly with the stuff above.

Now, since all this is still theory, perhaps you'd be so kind as to post the schematic of your project and your code so that we can actually have a look at what might cause your problem instead of attempting to write a textbook explanation of what might be the cause.

511 -513 is darn good. It is very difficult to get rid of all of the noise.

Add a bit of hysteresis to compensate for the noise. Like:

int hysteresis = 10;
if(analogIn > 512 - hysteresis && analogIn < 512 + hysteresis)
     // joystick in center  502 to 522

Putting a 0.1uF ceramic cap from the analog input to ground (low pass filter) can help to clean up the signal.

It is. Or rather, it would be. In reality, it seems to fluctuate between about 491 and 511 if you look at his Serial output.

OK I fixed my post.

Thank you all for your comments. As I am using pretty pristine equipment, I also assume there are no mechanical issues. I've done some calculations to get the median and it turns out that it stays within the range of the mid-value. But still, I wish to get some cleaner data with less noise, I will try to put the capacitor and see how will it work.

Some variation is normal but yours is a little too high. I would be looking at the wiring first. Any long floating leads? Power devices like motors, solenoids nearby? What about your power supply? Is it clean?

You should get an A/D fluctuation of one value.
An unstable value returned from a joystick/pot indicates that there could be a wiring problem.
Never share a pot supply/ground with anything through a breadboard.
Using an external reference is not the answer.
Show us a picture of the setup.

It is common to code a deadband around the idle point for a joystick.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.