Skip to content

Example 31: PS2 Joystick ​

Experiment Overview ​

A PS2 joystick module provides two-axis analog output (X/Y) and one button output (Z-axis press), suitable for game control, pan-tilt control, robotic arm manipulation, and similar scenarios.

Hardware Wiring ​

Board LabelGPIO PinConnected Device
VRxGPIO34X-axis analog output
VRyGPIO35Y-axis analog output
SWGPIO4Button (LOW when pressed)

X/Y axes output ~1.65V (ADC ~2048) at center; 0V or 3.3V at full deflection.

JSON Configuration Example ​

json
{
  "peripherals": [
    {
      "id": "joystick_x",
      "name": "Joystick X-Axis",
      "type": 15,
      "enabled": false,
      "pins": [34],
      "params": {}
    },
    {
      "id": "joystick_y",
      "name": "Joystick Y-Axis",
      "type": 15,
      "enabled": false,
      "pins": [35],
      "params": {}
    },
    {
      "id": "joystick_btn",
      "name": "Joystick Button",
      "type": 13,
      "enabled": false,
      "pins": [4],
      "params": {}
    }
  ]
}

Peripheral Execution Linkage ​

Scenario: Joystick Servo Control (Timer Trigger + Script) ​

Feature: Control servo angle (0-180°) via joystick X-axis

Web UI Configuration Steps ​

Step 1: Ensure Servo Peripheral is Configured

  • Peripheral ID: servo_01
  • Type: Servo

Step 2: Create Rule

  1. Click Peripheral Execution in the left menu
  2. Click New Rule button
  3. Fill in basic configuration:
    • Rule Name: Joystick Servo Control
    • Report Data: ✅ Enabled
    • Enable: ✅ Enabled

Step 3: Configure Trigger

  1. Click Add Trigger button

  2. Fill in trigger configuration:

    FieldValueDescription
    Trigger TypeSelect Timer TriggerPeriodic sampling
    Timer ModeSelect Fixed IntervalMillisecond interval
    Interval100100ms (fast response)

Step 4: Configure Action (Script Conversion)

  1. Click Add Action button

  2. Fill in:

    • Action Type: Select Command Script
    • Action Parameter:
    var x=analogRead('joystick_x');
    var angle=Math.round(x*180/4095);
    servoWrite('servo_01',angle);
  3. Click Save button

PS2 Joystick Value Reference ​

StatusADC ValueDescription
Center~2048Middle position (dead zone ±100)
Left/Up Extreme0Minimum value
Right/Down Extreme4095Maximum value

Notes ​

  1. Dead Zone: Joystick center ADC value is ~2000-2100 (not exactly 2048); set ±100 dead zone
  2. Pin Selection: Must use ADC-capable pins (GPIO32-39)
  3. Sampling Rate: 50-100ms sampling interval recommended for control applications
  4. Calibration: Record center value on first use as offset compensation baseline

FastBee | fastbee.cn