1

How do I get the full range of motion from an analog joystick and the ESP32 WROOM32E using the arduino IDE with esp-idf installed?

Using a 2 axis analog joystick, the 5v/3.3v pin, ground pin and the following code I do not get the correct results printed out. I do get the correct range of values to print but when the Jstick is at 50% or greater it's distance away from center it will print out 50 or -50 when it should be closer to 25. I cannot use the full range of motion, I tried it with the 5v pin and did not have better results, I tried a second identical board, I swapped out my playstation style analog jsticks. Why does it do this? Is this a code probelem?

int       test  = 0;
const int pinVar  = 33;

int  read_jstick(int rxy)
{ int JXY = analogRead(rxy);  //int JXYr = map(JXY,0,4095,-50,51);   
  return JXY;
} 
void  print_jstick(int pjs)
{ Serial.print("Joystick-X:  "); Serial.println(pjs);
}
void setup() 
{ Serial.begin(115200); pinMode(pinVar,OUTPUT);
} 
void loop() 
{ test = read_jstick(pinVar);
  print_jstick(test);
} 

Adding:

looking at the profile the joystick motion is like this: L _\ | / _ R

When the joystick is like this: | print val is: ~2500 at 5v and ~1800 at 3v

When the joystick is like this: _ on the L side the print val is 0 When the joystick is like this: _ on the R side the print val is 4095

Cool, but...

When the joystick is like this: \ at about 50% the range of motion, the value reads 0. It should be closer to 1200.

When the joystick is like this: between \ and | (0 and 50% range of motion) the print values will vary within the range of 0 and the middle value

I would like to be able to read the full range of motion on the joystick and receive a values incrementally over the entirety of that distance, how do I accomplish this? Example: I would like to move in let's say 10 incremental 'steps' with the joystick physically and the printout should be 1 through 10 and that would cover the movement from stationary to full extent of motion.

I have multiple of the same dev board, multiple of the same joystick, I have swapped cables and wires and I don't see how this could be a hardware problem so let's focus on coding unless it's perfect and should work exactly how I want it to because then that means for sure it's hardware and I just wasted a bunch of money buy multiple of everything and it's all broken. Sorry that I am frustrated here.

Update: I believe that a standard analog joystick module cannot be used with the ESP32 properly because the ESP32 cannot read the resistance of the joystick beyond 1.1v which means that it stops reading values in the middle of its range. You cannot get a scaling range of return from the joystick so it's basically a bad analog version of a digital left right up down inputter.

Prove me wrong please. If there is a way to read an analog pin higher than 1.1v or some other way so I could use the joystick for it's full range of motion on an ESP32 please let me know. All I can do is get a scaling range from stationary to a short move in 1 direction and when the stick is pushed passed 50% that direction it's all maxed out read value wise when it should be related to the position along length of travel.

  • start debugging by printing the value of `axisVar` – jsotola Apr 01 '22 at 00:20
  • it makes a difference if you define variables as `signed` or `unsigned` – jsotola Apr 01 '22 at 00:22
  • thanks, I have it print the value now and that's how I can tell that half way through the joysticks motion it hits the max 50/-50. I will try them as signed/unsigned variables. – localmartian Apr 01 '22 at 03:35
  • I am saying to write the simplest code ... the `map` function is not simplest code ... print what is actually being read from the joystick without modifying the value – jsotola Apr 01 '22 at 06:26
  • Did that, it sits at around a val of 1800 with range between 0 and 4095 when I move the joystick. The problem here is that the values of 0 and 4095 are in the middle of the sticks motion from stationary to one extent, eg at half of it's range of motion it maxes at the high or low value. I can only use half of the joysticks range of motion on each axis. – localmartian Apr 02 '22 at 04:39
  • it is probably caused by using a signed integer ... I don't have an ESP32 right now to test your code ... btw you did not write the simplest code ... don't be calling functions for reading a joystick and for printing the value ... it could be the functions that are messing up the values – jsotola Apr 02 '22 at 06:02
  • That sounds to me like your joystick is the problem. If you have a multimeter you can verify it directly by measuring the voltage at the slider pin. If it doesn't change in the last half of the full side motion, then the joystick itself only supports a smaller range of motion. – chrisl Apr 02 '22 at 12:57
  • @chrisl - The Joystick works with my arduino correctly, I can receive readings along the full motion of the joystick and I have tried multiple joysticks and the full range of motion is not available on the ESP32 for some reason, I also don't know how to do what you said with the multimeter. Powered by either 5v or 3v the print has the same issue, it stops half way through the sticks motion at the max or min value instead of being half way through the range of values. I also tried this on multiple dev boards, the same type of board. Is this and ADC software issue that can be coded to solve? – localmartian Apr 02 '22 at 16:44
  • @jsotola - I don't understand your simple code thing because I will need functions down the line to handle the joystick values and I don't understand how what I wrote wasn't simple. So then Signed v unsigned seems to have no effect whatsoever that I noticed. If you don't know how to use the ESP32 then you might not be helping and could be leading me down the wrong rabbit hole. If this is a print issue then no one could use a joystick and pass its values, I think it's something internal to with the ADC that needs to be modified but I don't know what or how. Thanks for your time. – localmartian Apr 02 '22 at 16:54
  • yes, you will need functions, but how do you know if the function call somehow changes the reading? ... at this point, you do not know what value is actually returned by the `analogRead(rxy)` function ... that is why I suggested that you simply get the value and print it inside `loop()` – jsotola Apr 02 '22 at 16:58
  • @jsotola "const int pinVar = 33; void setup(){ Serial.begin(115200); pinMode(pinVar,INPUT); } void loop(){ int J = analogRead(pinVar); Serial.println(J); }" That gave me the same results... when the joystick is at 50% it's range (~25% the total range from max - to max + along the same axis) it prints out the max value and will do that for any position above it. I don't think you understand the problem. There is some ADC (analog to digital conversion) coding that neither of us know how to do. – localmartian Apr 02 '22 at 23:26
  • you may be right that I do not understand the problem ... try an unsigned integer `unsigned int J = analogRead(pinVar);` – jsotola Apr 02 '22 at 23:35
  • @jsotola I have already said that I tried that and that it did not change anything, I did the whole gamut of combos signed v unsigned and it was a waste of time. I'm almost 100% certain that the ESP32 cannot use a standard analog joystick correctly because it can only read up to 1.1v so no once you pass that 1.1v range on the stick it maxes out and that is at a point less than 50% the distance of travel in 1 direction on the axis. – localmartian Apr 03 '22 at 20:35
  • Same issue here. Did you get any answer? – Anzar Nazar Jul 25 '22 at 02:35

0 Answers0