Example Code for Arduino-Joystick Control

Last revision 2026/01/15

Modify the Sample code for the Joystick Module. Read the X-axis (analog), Y-axis (analog), and Z-axis (digital) values of the joystick module and output them via the serial port.

Hardware Preparation

Software Preparation

Wiring Diagram

oystick Module For Arduino Diagram

Wiring Instructions
Pin X
S - A1
Pin Y
S - A0
Pin Z
D - D3

Other Preparation Work

Libary installation

Connection Instructions:

  • X-Axis -> Analog pin 0
  • Y-Axis -> Analog pin 1
  • Z-Axis -> Digital pin 3

Sample Code

// #
// # Editor     : Lauren from DFRobot
// # Date       : 17.01.2012

// # Product name: Joystick Module
// # Product SKU : DFR0061
// # Version     : 1.0

// # Description:
// # Modify the Sample code for the Joystick Module

// # Connection:
// #        X-Axis  -> Analog pin 0
// #        Y-Axis  -> Analog pin 1
// #        Z-Axis  -> Digital pin 3
// #


int JoyStick_X = 0; //x
int JoyStick_Y = 1; //y
int JoyStick_Z = 3; //key

void setup()
{
  pinMode(JoyStick_Z, INPUT);
  Serial.begin(9600); // 9600 bps
}
void loop()
{
  int x,y,z;
  x=analogRead(JoyStick_X);
  y=analogRead(JoyStick_Y);
  z=digitalRead(JoyStick_Z);
  Serial.print(x ,DEC);
  Serial.print(",");
  Serial.print(y ,DEC);
  Serial.print(",");
  Serial.println(z ,DEC);
  delay(100);
}

Was this article helpful?

TOP