A
Arduino4mo ago
koyo

Joystick buggy with ESP32

Hello. I uploaded a simple code to read joystick values to my ESP32 but the values aren’t reading the usual 0-1023 and instead are lying between 0-4095 with the values constantly fluctuating even when centered. I assume it has something to do with the hardware and connections messing with the signal but just in case I’ll show the code too
No description
No description
No description
22 Replies
koyo
koyoOP4mo ago
Materials: breadboard 5V step up converter ESP32 Standard arduino joystick 3.7V 1s LiPo battery ----------------------------------------------------------------------- Battery 3.7V —> power rail 1 Battery GND —> gnd rail 1 Converter Vin—> power rail 1 Converter GND —> gnd rail 1 Converter Vout —> power rail 2 Converter GND —> gnd rail 2 Joystick 5V —> power rail 2 Joystick GND —> GND rail 2 Joystick VRX —> pin 32 on esp Joystick VRY —> pin 33 on esp ESP32 gnd—> gnd rail 2
/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-joystick
*/

#define VRX_PIN 32 // ESP32 pin GPIO39 (ADC3) connected to VRX pin
#define VRY_PIN 33 // ESP32 pin GPIO36 (ADC0) connected to VRY pin

int valueX = 0; // to store the X-axis value
int valueY = 0; // to store the Y-axis value
int mappedyVal;

void setup() {
Serial.begin(9600);

// Set the ADC attenuation to 11 dB (up to ~3.3V input)
analogSetAttenuation(ADC_11db);
}

void loop() {
// read X and Y analog values
valueX = analogRead(VRX_PIN);
valueY = analogRead(VRY_PIN);
mappedyVal = map(valueY, 0, 4095, 0, 2);

// print data to Serial Monitor on Arduino IDE
Serial.print("x = ");
Serial.print(valueX);
Serial.print(", y = ");
Serial.print(valueY);
Serial.print(" ");
Serial.println(mappedyVal);
delay(200);
}
/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-joystick
*/

#define VRX_PIN 32 // ESP32 pin GPIO39 (ADC3) connected to VRX pin
#define VRY_PIN 33 // ESP32 pin GPIO36 (ADC0) connected to VRY pin

int valueX = 0; // to store the X-axis value
int valueY = 0; // to store the Y-axis value
int mappedyVal;

void setup() {
Serial.begin(9600);

// Set the ADC attenuation to 11 dB (up to ~3.3V input)
analogSetAttenuation(ADC_11db);
}

void loop() {
// read X and Y analog values
valueX = analogRead(VRX_PIN);
valueY = analogRead(VRY_PIN);
mappedyVal = map(valueY, 0, 4095, 0, 2);

// print data to Serial Monitor on Arduino IDE
Serial.print("x = ");
Serial.print(valueX);
Serial.print(", y = ");
Serial.print(valueY);
Serial.print(" ");
Serial.println(mappedyVal);
delay(200);
}
(for the record this is not my code but one I found online to test the joystick)
koyo
koyoOP4mo ago
proof of values fluctuating in centered state
No description
Kaito
Kaito4mo ago
I don't know hardly anything about esp32, but 1) I've heard that the ADC on them is less than perfect so there's going to be some noise, and 2) you can code in a dead zone in the middle so the jitter doesn't affect anything
AnonEngineering
AnonEngineering4mo ago
an Uno has a 10 bit ADC (0-1023), an ESP has a 12 bit ADC (0-4096) they also have different full scale ranges
koyo
koyoOP4mo ago
Oh that’s on me but every YouTube video I’ve seen on it still went from 0-1023 But I see alright I’ll just program it to have a set value if 10 or less apart Thank you guys wait but if I hold it in the middle shouldnt it be 2048 why is it in 1900s
AnonEngineering
AnonEngineering4mo ago
since esp has a 3.3v range you should power the joysticks with 3,3v
koyo
koyoOP4mo ago
oooooooooooooooooooooooooooohhhhhhhhhhhhhhhh
AnonEngineering
AnonEngineering4mo ago
it'll be somewhere around 2048 in the middle, depending on mechanical trim of the stick, and joystick values always bobble a count or two
koyo
koyoOP4mo ago
well its fine in that case I suppose as long as the middle value is around halfway and not hundreds off
AnonEngineering
AnonEngineering4mo ago
powering the joysticks with 5v might damage the esp as at one end the joystick outputs Vcc
koyo
koyoOP4mo ago
oh right for the read gotcha its still outputting the same after its been converted to 3.3V
AnonEngineering
AnonEngineering4mo ago
does it go from about 0 to about 4096?
koyo
koyoOP4mo ago
yeah wwait i had it on 3.3V the whole time mb
AnonEngineering
AnonEngineering4mo ago
those cheap joysticks probably don't but many joysticks have a mechanical trim adjustment
koyo
koyoOP4mo ago
at 5V it goes crazy random
AnonEngineering
AnonEngineering4mo ago
yes, you are overdriving a 3.3v pin
koyo
koyoOP4mo ago
if it keeps up like this I guess ill find antoher one
AnonEngineering
AnonEngineering4mo ago
1900 isn't that far off, you'll never find one that give a rock solid 2048 😉
koyo
koyoOP4mo ago
i kind of patched it up with tape and called it a day using code lol
AnonEngineering
AnonEngineering4mo ago
generally you do. in code create a "dead band" for the center, for example, if x > 2000 or x < 2100 call it centered or whatever values your stick gives you
koyo
koyoOP4mo ago
Yepp I gave it 50 on both ends to be “centered” Thank you once again 👍
AnonEngineering
AnonEngineering4mo ago
i don't recall if i shared this, probably could be done more efficiently but it works 😉 https://wokwi.com/projects/431574631222675457

Did you find this page helpful?