The joystick is one component but uses two analog sensors and gives you two values. One x value and one y value. The value for each direction is zero if the joystick is in the middle, -1 on one side and 1 on the other side.
Materials
- 1 Arduino Uno board
- 1 Basic Education shield
- 1 TinkerKit joystick
- 2 TinkerKit wire
- 3 jumper wire
Instructions
- Attach the shield onto the top of the Arduino board.
- Connect one of the TinkerKit wires to the joystick where it says “Y”.
- Connect 1 red jumper wire to the red wire in the tinkerKit wire and to 5V.
- Connect 1 black jumper wire to the black wire in the TinkerKit wire and to GND.
- Connect the last jumper wire to the TinkerKit wire and to analog pin A0.
- Connect the second TinkerKit wire to the joystick where it says “X” and to the analog TinkerKit connector, A1.
- Connect the board to the computer and upload example ‘Joystick’.
Code
Find the code in Example>EducationShield>Help>Joystick example
You should now open the serial monitor and move the joystick around. Both the X and Y value should be 0 when the joystick is in the middle. Values should change between -1 and 1 when you move the joystick to the different sides.
Commands
Joystick( xPin, yPin )
– creates a Joystick object.- Parameters
- (
int
)xPin
– the analog pin to which the joysticks x pin is connected. -
(
int
)yPin
– the analog pin to which the joysticks y pin is connected.
- (
- Parameters
getX()
– returns the x value.- Return
-
(
int
) – value of the joystick x reading. Between 0 and 1023.
-
- Return
getY()
– returns the y value.-
Return
- (
int
)value
of the joystick y reading. Between 0 and 1023.
- (
-
Troubleshooting
- First make sure that the connections between the joystick, wires and board are correct.
- Make sure you have connected the joystick to the same pins as you stated in your code.
- There are three small LEDs on the back of the joystick that lights up when the joystick is connected (These lights will light up even if you only connect one of the connectors). One to show that it is getting power, one for X and one for Y. If you are sure that it is connected correctly and the LEDs still do not light up, the joystick is probably broken.