Joystick mode problems Teensy 4.0

cjluck

New member
I have a teensy 4.0 with the USB mode set to serial/mouse/keyboard/joystick, and loaded with this example code using the arduino IDE. If I connect a 10k potentiometer between the 3V pin and pin 14 (Analog pin 0), and look at the test dialog on windows, the X axis jumps from the far left to the far right and stays there regardless of the position of the potentiometer. If I then disconnect the potentiometer, the X axis remains at the far right position.

If I add a serial print statement to see the actual values, on power up without the potentiometer it reads 6, when I connect that the value varies between 1017 and 1023 depending on position. If I disconnect the potentiometer, the value goes to 1023 and stays there.

1) What am I missing or doing wrong to not get a full range of values?
2) Why does the Teensy read a high value when the pin is disconnected instead of a low value?

/* Basic USB Joystick Example
Teensy becomes a USB joystick

You must select Joystick from the "Tools > USB Type" menu

Pushbuttons should be connected to digital pins 0 and 1.
Wire each button between the digital pin and ground.
Potentiometers should be connected to analog inputs 0 to 1.

This example code is in the public domain.
*/

void setup() {
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
}

void loop() {
// read analog inputs and set X-Y position
Joystick.X(analogRead(0));
Joystick.Y(analogRead(1));

// read the digital inputs and set the buttons
Joystick.button(1, digitalRead(0));
Joystick.button(2, digitalRead(1));

// a brief delay, so this runs 20 times per second
delay(50);
}
 
How exactly do you connect your potentiometer? It should have three pins: the one in the middle has to be connected to the analog input pin of the teensy. The other potentiometer pins: one to ground, the other to 3V.
 
If I disconnect the potentiometer, the value goes to 1023 and stays there.
Of course. This line in setup pinMode(0, INPUT_PULLUP); causes that.
INPUT_PULLUP tells the Teensy to initialise itself with a pull up to 3V.
Hence if no connection you get 1023/1024.
That is configuring the input as a digital input.
For an analogue input you should just use ,INPUT.
 
That pinMode call is for pin 0 (one of the button pins), not analog pin 0 (which is pin 14).
Teensyduino 1.59 will automatically disable the pin keeper anyway for analog reads.
 
Back
Top