Example Code for Raspberry Pi – JoyStick

Last revision 2026/01/20

This article provides example code and detailed instructions for integrating and using the JoyStick with Raspberry Pi, showcasing its features, setup process, and the necessary dependencies for smooth operation.

Introduction

The JoyStick produced by DFRobot is made with original high-quality metal PS2 rocker potentiometer. With (X, Y) 2 axis analog output and (Z) 1 button digital output, it can maintain good contact and mechanical properties no matter how you torture it. The 3 signals are respectively connected to the Arduino sensor expansion board through the 3P line, occupying only 3 ports for its control.

Features of its new version:

  • Operating Voltage: 3.3 V/5 V, suitable for more 3.3 V controllers.
  • Standard Size: The distance between two mounting holes with a diameter of 3mm is several times that of 5mm
  • Easy to Identify: The two analog output interfaces are marked with S, while the digital interface is marked with D
  • High-quality connectors, resistant to repeated plugging and unplugging
  • Immersion gold technology, not only improving the quality of PCB board, but also being with golden fonts.

Precautions

The layout of the new version of the analog sensor port has the following two improvements. Please refer to the blog How to change the layout of the data cable connector. When using this sensor on the IO expansion board, you may need to adjust the layout of the connector. For your convenience, we will make more improvements, so stay tuned.

Wiring Diagram

  • Power the Raspberry Pi on and install the Raspberry Pi expansion board correctly

  • Connect the X-axis output port of the JoyStick to the analog port 0 on the expansion board, the Y-axis output port to the analog port 1, and the Z-axis output to the digital port 8

Sample Code

  • Install Python dependency libraries and git, and you need to get your Raspberry Pi connect to internet for this step(skip if installed). In the terminal, type the following instructions and press ‘Enter’
    sudo apt-get install build-essential python-dev python-smbus git
  • Download drive library and program. In the terminal, type the following instructions and press ‘Enter’
    cd \~

git clone https://github.com/DFRobotdl/37\_Pcs\_Sensor.git

  • Find 7_Joystick_Module_V2.py in \37_Pcs_Sensor\8_Joystick_Module_V2, open and run it with Thonny Pyth IDE, you can see the printed X, Y, Z axis data. The value will change if you flip or press the joystick.


Was this article helpful?

TOP