B4R Code Snippet Reading KY-023 dual-axis XY joystick

SubName: Reading an KY-023 joystick
Description: You can use this simple code to read the position of a KY-023 dual-axis joystick.

At its centre point the joystick readings are around 512, moving the joystick up/down/left/right will change the resistance values either higher until it reaches 1023 or lower until it reaches 0, basically X and Y ranges from 0 to 1023 on the analog pins. The centre push switch is connected to any digital pin and when pressed the digital reading will change from 1 to 0. This is a great joystick for using with a menu system on a large display etc.

B4X user Sorex put me onto this Joystick a few weeks ago, so I ordered 3 of them, today I decided to test this joystick, the code and results are below.

Reading | ############################################Picked up _JAVA_OPTIONS: -Xmx512m -Xms512m
********************* PROGRAM STARTING ****************
AppStart
X = 513
Y = 517
Button = 1
X = 512
Y = 518
Button = 1
X = 512
Y = 517
Button = 1
X = 512
Y = 518
Button = 1
X = 512
Y = 517
Button = 1
X = 512
Y = 518
Button = 1
X = 513
Y = 517
Button = 1
X = 512
Y = 518
Button = 1
X = 1023
Y = 518
Button = 1
X = 1023
Y = 517
Button = 1
X = 1023
Y = 517
Button = 1
X = 1023
Y = 518
Button = 1
X = 1023
Y = 518
Button = 1
X = 1023
Y = 518
Button = 1
X = 1023
Y = 517
Button = 1
X = 1023
Y = 518
Button = 1
X = 557
Y = 517
Button = 1
X = 512
Y = 518
Button = 1
X = 512
Y = 517
Button = 1
X = 512
Y = 517
Button = 0
X = 512
Y = 518
Button = 0
X = 512
Y = 517
Button = 0
X = 455
Y = 518
Button = 0
X = 0
Y = 517
Button = 0
X = 0
Y = 510
Button = 0
X = 0
Y = 517
Button = 0
X = 0
Y = 517
Button = 0
X = 0
Y = 517
Button = 0
X = 0
Y = 517
Button = 0
X = 0
Y = 517
Button = 1
X = 79
Y = 518
Button = 1
X = 512
Y = 517
Button = 1
X = 512
Y = 516
Button = 1
X = 512
Y = 517
Button = 1
X = 512
Y = 518
Button = 1
X = 512
Y = 517
Button = 1
X = 512
Y = 172
Button = 1
X = 512
Y = 0
Button = 1
X = 511
Y = 0
Button = 1
X = 512
Y = 0
Button = 1
X = 512
Y = 0
Button = 1
X = 512
Y = 0
Button = 0
X = 512
Y = 0
Button = 0
X = 512
Y = 0
Button = 0
X = 512
Y = 517
Button = 0
X = 512
Y = 518
Button = 0
X = 512
Y = 1023
Button = 1
X = 512
Y = 1023
Button = 1
X = 512
Y = 1023
Button = 1
X = 512
Y = 1023
Button = 1
X = 512
Y = 1023
Button = 1
X = 512
Y = 1023
Button = 1
X = 512
Y = 1023
Button = 1
X = 511
Y = 1023
Button = 1
X = 512
Y = 518
Button = 1
X = 512
Y = 523
Button = 1
X = 512
Y = 516
Button = 1
X = 512
Y = 517
Button = 1
X = 512
Y = 517
Button = 1
X = 512
Y = 517
Button = 1
X = 512
Y = 518
Button = 1
X = 512
Y = 517
Button = 1
X = 512
Y = 517
Button = 0
X = 512
Y = 515
Button = 0
X = 512
Y = 518
Button = 0
X = 512
Y = 517
Button = 0
X = 512
Y = 518
Button = 0
X = 512
Y = 515
Button = 0
X = 512
Y = 517
Button = 0
X = 512
Y = 518
Button = 0
X = 512
Y = 517
Button = 1
X = 512
Y = 517
Button = 1
X = 512
Y = 517
Button = 1
X = 0
Y = 439
Button = 1
X = 0
Y = 1023
Button = 1
X = 318
Y = 1023
Button = 1
X = 1023
Y = 1023
Button = 1
X = 1023
Y = 517
Button = 1
X = 1023
Y = 0
Button = 1
X = 512
Y = 0
Button = 1
X = 0
Y = 0
Button = 1
X = 0
Y = 1023
Button = 1
X = 433
Y = 1023
Button = 1
X = 1023
Y = 1023
Button = 1
X = 1023
Y = 948
Button = 1
X = 1023
Y = 111
Button = 1
X = 1023
Y = 0
Button = 1
X = 403
Y = 0
Button = 1
X = 0
Y = 0
Button = 1
X = 0
Y = 517
Button = 1
X = 0
Y = 1023
Button = 1
X = 513
Y = 1023
Button = 1
X = 1023
Y = 523
Button = 1
X = 512
Y = 517
Button = 1
X = 512
Y = 518
Button = 1
X = 512
Y = 518
Button = 1
X = 512
Y = 518
Button = 1
X = 512
Y = 517
Button = 1
X = 512
Y = 517
Button = 1
X = 512
Y = 517
Button = 1
B4X:
'WIRE LEGEND for KY-023
'GND = GND
'+5V = 5V
'VRy = A0
'VRx = A1
'SW = D7
'*************************
'***     BOARD TYPE    ***
'***    Arduino All    ***
'*************************

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
 
    Public Serial1 As Serial
    Private Switch, VRx, VRy As Pin
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
 
    Switch.Initialize(7, Switch.MODE_INPUT_PULLUP)
    VRx.Initialize(VRx.A0, VRx.AnalogRead)
    VRy.Initialize(VRy.A1, VRy.AnalogRead)
 
    'AddLooper is only being used for example purposes
    AddLooper("ReadJoystick")
End Sub

Sub ReadJoystick
    Log("X = ", VRx.AnalogRead)
    Log("Y = ", VRy.AnalogRead)
    Log("Button = ", Switch.DigitalRead)
    Delay(50)
End Sub

Tags: Joystick, KY-023, Arduino, ESP

Actual joystick module
s-l1600.jpg


How to connect
Untitled-2.png


Enjoy...
 
Last edited:
Top