# Potentiometer jittering

**URL:** <https://forum.arduino.cc/t/potentiometer-jittering/1210100>\
**Category:** General Guidance\
**Created:** [January 12, 2024, 9:11am UTC](https://forum.arduino.cc/t/potentiometer-jittering/1210100 "2024-01-12T09:11:23Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Assamita](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/assamita/32/1129187_2.png) [@Assamita](https://forum.arduino.cc/u/Assamita)\
**Post date:** [January 12, 2024, 9:11am UTC](https://forum.arduino.cc/t/potentiometer-jittering/1210100/1 "2024-01-12T09:11:23Z")

</div>

Hi. I made a gaming device with an Arduino Pro Micro using the joystick library. The device has 2 PT10 10k potentiometers, and they jitter a bit in game. See video:

[![](https://europe1.discourse-cdn.com/arduino/original/4X/1/9/2/192342f1eed4cf39d8ca329e842769dd0c3411a2.jpeg "Potentiometer jitter") ](https://www.youtube.com/watch?v=fz_3Um5c9UQ)

The relevant code is very basic:

```arduino
  rudder_ = analogRead(gain);
  Joystick.setRudder(rudder_);

  throttle_ = analogRead(lev);
  Joystick.setThrottle(throttle_);

```

How can I remove the jitter? the pots are connected to VCC, GND and an analog pin each directly. Should I add a capacitor or is this something that can be fixed by code?

Here's the full code:

```arduino
#include "Joystick.h"
#include "Wire.h"
#include <Keypad.h>

#define NUMROWS 3
#define NUMCOLS 7
#define joyX A1
#define joyY A0
#define gain A3
#define lev A2
#define address 0x20
#define address2 0x21

byte buttonsMFD[NUMROWS][NUMCOLS] = {
  { 74, 72, 73, 61, 62, 63, 64 },
  { 71, 65, 66, 67, 68, 69, 70 },
  { 75, 77, 76, 78, 79, 80, 81},
};

byte rowPins[NUMROWS] = { 4, 5, 6 };
byte colPins[NUMCOLS] = { 7, 8, 9, 10, 16, 14, 15 };

Keypad MFD = Keypad(makeKeymap(buttonsMFD), rowPins, colPins, NUMROWS, NUMCOLS);

byte buttons[8][8] = {
  { 0, 1, 2, 3, 4, 5, 6, 7 },
  { 86, 86, 86, 86, 12, 13, 14, 15 },
  { 16, 17, 18, 19, 20, 21, 22, 23 },
  { 24, 86, 86, 86, 86, 86, 30, 32 },
  { 96, 97, 98, 99, 37, 38, 39, 40 },
  { 41, 42, 43, 44, 45, 46, 47, 49 },
  { 50, 51, 52, 87, 53, 54, 55, 57 },
  { 58, 59, 60, 83, 82, 85, 84, 86 },
};

byte buttons2[2][8] = {
  { 8, 9, 10, 11, 33, 34, 35, 36 },
  { 25, 26, 27, 28, 29 },
};

bool buttonvalues[8][8] = {
  { 1, 1, 1, 1, 1, 1, 1, 1 },
  { 1, 1, 1, 1, 1, 1, 1, 1 },
  { 1, 1, 1, 1, 1, 1, 1, 1 },
  { 1, 1, 1, 1, 1, 1, 1, 1 },
  { 1, 1, 1, 1, 1, 1, 1, 1 },
  { 1, 1, 1, 1, 1, 1, 1, 1 },
  { 1, 1, 1, 1, 1, 1, 1, 1 },
  { 1, 1, 1, 1, 1, 1, 1, 1 },
};

bool buttonvalues2[2][8] = {
  { 1, 1, 1, 1, 1, 1, 1, 1 },
  { 1, 1, 1, 1, 1 },
};

const bool initAutoSendState = true;

int xAxis_ = 0;
int yAxis_ = 0;
int rudder_ = 0;
int throttle_ = 0;

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_JOYSTICK, 86, 0, true, true, false, false, false, false, true, true, false, false, false);

void setup() {
  Wire.begin();
  Wire.beginTransmission(address);
  Wire.write(0xFF);
  Wire.write(0xFF);
  Wire.endTransmission();
  Joystick.begin();
}

void loop() {
  CheckGrips();
  CheckAllAxis();
  CheckMFD();
  CheckExtraPCF();
}

void CheckExtraPCF() {
  byte read_value1;
  byte read_value2;
  if (Wire.requestFrom(address2, 2) == 2) {
    read_value1 = (Wire.read());
    read_value2 = (Wire.read());
  }

  for (int i = 0; i < 8; i++) {

    if (bitRead(read_value1, i) != buttonvalues2[0][i]) {
      Joystick.setButton(buttons2[0][i], !bitRead(read_value1, i));
      buttonvalues2[0][i] = bitRead(read_value1, i);
    }
  }
  for (int i = 0; i < 5; i++) {

    if (bitRead(read_value2, i) != buttonvalues2[1][i]) {
      Joystick.setButton(buttons2[1][i], !bitRead(read_value2, i));
      buttonvalues2[1][i] = bitRead(read_value2, i);
    }
  }
}

void CheckGrips() {
  byte read_value1;
  byte read_value2;
  byte write_value = 1;
  for (int n = 0; n < 8; n++) {
    byte temp = ~write_value;
    Wire.beginTransmission(address);
    Wire.write(temp);
    Wire.write(0xFF);
    Wire.endTransmission();
    if (Wire.requestFrom(address, 2) == 2) {
      read_value1 = (Wire.read());
      read_value2 = (Wire.read());
    }
    for (int i = 0; i < 8; i++) {

      if (bitRead(read_value2, i) != buttonvalues[i][n]) {
        Joystick.setButton(buttons[i][n], !bitRead(read_value2, i));
        buttonvalues[i][n] = bitRead(read_value2, i);
      }
    }
    write_value <<= 1;
  }
  if ((buttonvalues[3][6] == 1) && (buttonvalues[3][7] == 1)) {
    Joystick.setButton(31, 1);
  } else {
    Joystick.setButton(31, 0);
  }
  if ((buttonvalues[5][6] == 1) && (buttonvalues[5][7] == 1)) {
    Joystick.setButton(48, 1);
  } else {
    Joystick.setButton(48, 0);
  }
  if ((buttonvalues[6][6] == 1) && (buttonvalues[6][7] == 1)) {
    Joystick.setButton(56, 1);
  } else {
    Joystick.setButton(56, 0);
  }
}

void CheckAllAxis(void) {

  xAxis_ = analogRead(joyX);
  xAxis_ = map(xAxis_, 0, 1023, 1023, 0);
  Joystick.setXAxis(xAxis_);

  yAxis_ = analogRead(joyY);
  Joystick.setYAxis(yAxis_);

  rudder_ = analogRead(gain);
  Joystick.setRudder(rudder_);

  throttle_ = analogRead(lev);
  Joystick.setThrottle(throttle_);
}

void CheckMFD(void) {

  if (MFD.getKeys()) {
    for (int j = 0; j < LIST_MAX; j++) {
      if (MFD.key[j].stateChanged) {
        switch (MFD.key[j].kstate) {
          case PRESSED:
          case HOLD:
            Joystick.setButton(MFD.key[j].kchar, 1);
            break;
          case RELEASED:
            break;
          case IDLE:
            Joystick.setButton(MFD.key[j].kchar, 0);
            break;
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [January 12, 2024, 9:18am UTC](https://forum.arduino.cc/t/potentiometer-jittering/1210100/2 "2024-01-12T09:18:48Z")

</div>

Hi, @Assamita  
Can you please post your code?

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?  
Hand drawn and photographed is perfectly acceptable.  
Please include ALL hardware, power supplies, component names and pin labels.

What is your power supply?

Thanks.. Tom... 😀 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [January 12, 2024, 9:34am UTC](https://forum.arduino.cc/t/potentiometer-jittering/1210100/3 "2024-01-12T09:34:48Z")

</div>

A capacitor may not work

Try this code:  
rudder\_ = 0;  
rudder\_ = rudder\_ + analogRead(gain);  
rudder\_ = rudder\_ + analogRead(gain);  
rudder\_ = rudder\_ + analogRead(gain);  
rudder\_ = rudder\_ + analogRead(gain);  
rudder\_ = rudder\_ + analogRead(gain);  
rudder\_ = rudder\_ + analogRead(gain);  
rudder\_ = rudder\_ + analogRead(gain);  
rudder\_ = rudder\_ + analogRead(gain);  
rudder\_ = rudder\_\>\> 8;

Joystick.setRudder(rudder\_);

---

<div class="post-metadata">

**Author:** ![johnerrington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnerrington/32/340106_2.png) [@johnerrington](https://forum.arduino.cc/u/johnerrington)\
**Post date:** [January 12, 2024, 9:44am UTC](https://forum.arduino.cc/t/potentiometer-jittering/1210100/4 "2024-01-12T09:44:28Z")

</div>

I'd prefer to use an exponential filter Jim - mainly because it will allow more time between readings.

eg  
int oldrudder = 512; //start with a centre value

rudder\_ = (3\*oldrudder + analogRead(gain))/4;  
oldrudder = rudder\_;

---

<div class="post-metadata">

**Author:** ![Assamita](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/assamita/32/1129187_2.png) [@Assamita](https://forum.arduino.cc/u/Assamita)\
**Post date:** [January 12, 2024, 10:29am UTC](https://forum.arduino.cc/t/potentiometer-jittering/1210100/5 "2024-01-12T10:29:47Z")

</div>

Hi TomGeorge. I did post my code. Check OP.  
All hardware will be complicated. It's an 86 buttons and switches devices, with a thumb joystick and two potentiometers. It runs two button matrices, one with diodes and managed by a 16 I/O port expander, one without diodes because that one doesn't require multiple buttons being pressed at the same time, connected directly to the arduino, and an additional port expander for more inputs.  
It's powered via USB because it's all inputs.

---

<div class="post-metadata">

**Author:** ![Assamita](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/assamita/32/1129187_2.png) [@Assamita](https://forum.arduino.cc/u/Assamita)\
**Post date:** [January 12, 2024, 10:33am UTC](https://forum.arduino.cc/t/potentiometer-jittering/1210100/6 "2024-01-12T10:33:03Z")

</div>

Why starting with a centre value? although it's called rudder, it's actually not used for a rudder, but for a knob, so values should go from 0 to 1023.  
How do I assign the value to the joystick output? Joystick.setRudder(oldrudder);?

---

<div class="post-metadata">

**Author:** ![johnerrington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnerrington/32/340106_2.png) [@johnerrington](https://forum.arduino.cc/u/johnerrington)\
**Post date:** [January 12, 2024, 5:14pm UTC](https://forum.arduino.cc/t/potentiometer-jittering/1210100/7 "2024-01-12T17:14:54Z")

</div>

Because you have to HAVE a start value (although you could read it in setup) but for a rudder straight ahead makes sense.

> [@Assamita](#):
>
> although it's called rudder, it's actually not used for a rudder,

yes, of course. That makes sense. (?)

> [@Assamita](#):
>
> so values should go from 0 to 1023.

Your readings from the ADC will go from low ( - around 0-20, depending on ADC offset error) to high (depending on the supply and ADC reference.)

> **[skillbank: Experiments and projects with an Arduino microcontroller](https://skillbank.co.uk/arduino/calibrate.htm#offseterror)**
>
> Voltage measurement with the Arduino board: Calibration

> [@Assamita](#):
>
> the pots are connected to VCC, GND and an analog pin each directly.

Like this

> **[skillbank: Experiments and projects with an Arduino microcontroller](https://skillbank.co.uk/arduino/measure1.htm#meas2)**
>
> Voltage measurement with the Arduino board (cont)

In your code I dont see where "gain" and "lev" are declared?

---

<div class="post-metadata">

**Author:** ![Assamita](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/assamita/32/1129187_2.png) [@Assamita](https://forum.arduino.cc/u/Assamita)\
**Post date:** [January 12, 2024, 5:56pm UTC](https://forum.arduino.cc/t/potentiometer-jittering/1210100/8 "2024-01-12T17:56:32Z")

</div>

> [@johnerrington](#):
>
> yes, of course. That makes sense. (?)

It's called rudder because I assigned this value to Joystick. **setRudder** (rudder\_); and I just copy/pasted some working code and adjusted to my needs, but it really doesn't matter what the variable is called. I just told you it was not a rudder to let you know that it didn't necessarily need to be a centered position pot, like a thumb joystick or a rudder. It's a knob.

> [@johnerrington](#):
>
> In your code I dont see where "gain" and "lev" are declared?

Lines 9 and 10

Anyway, I'll test your code and let you know how it works.

---

<div class="post-metadata">

**Author:** ![Assamita](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/assamita/32/1129187_2.png) [@Assamita](https://forum.arduino.cc/u/Assamita)\
**Post date:** [January 12, 2024, 6:05pm UTC](https://forum.arduino.cc/t/potentiometer-jittering/1210100/9 "2024-01-12T18:05:24Z")

</div>

I think that worked, yes. Thank you!! I'll do some more tests, but the preliminary one I just very quickly did seems to have gotten rid of the jitter

---

<div class="post-metadata">

**Author:** ![build\_1971](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/build_1971/32/689930_2.png) [@build\_1971](https://forum.arduino.cc/u/build_1971)\
**Post date:** [January 12, 2024, 6:26pm UTC](https://forum.arduino.cc/t/potentiometer-jittering/1210100/10 "2024-01-12T18:26:07Z")

</div>

Shouldn't that be \>\>4?

---

<div class="post-metadata">

**Author:** ![jim-p](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jim-p/32/922840_2.png) [@jim-p](https://forum.arduino.cc/u/jim-p)\
**Post date:** [January 12, 2024, 6:33pm UTC](https://forum.arduino.cc/t/potentiometer-jittering/1210100/11 "2024-01-12T18:33:32Z")

</div>

Duh! YES

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [January 12, 2024, 7:08pm UTC](https://forum.arduino.cc/t/potentiometer-jittering/1210100/12 "2024-01-12T19:08:38Z")

</div>

> [@jim-p](#):
>
> Duh!

Or \>\> 3.

a7

---

<div class="post-metadata">

**Author:** ![build\_1971](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/build_1971/32/689930_2.png) [@build\_1971](https://forum.arduino.cc/u/build_1971)\
**Post date:** [January 12, 2024, 7:18pm UTC](https://forum.arduino.cc/t/potentiometer-jittering/1210100/13 "2024-01-12T19:18:54Z")

</div>

You are right!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 10, 2024, 7:19pm UTC](https://forum.arduino.cc/t/potentiometer-jittering/1210100/14 "2024-07-10T19:19:31Z")

</div>

Closed on July 10, 2024, 7:19pm UTC

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