X and Y position of the joystick are output as analog voltage on the output pins.

This joystick has its own potentiometer for the X-axis and for the Y-axis:

In the idle state, the potentiometer is in the middle, so that resistance1 and resistance, and thus also the applied voltage, are identical.

If, for example, the position of the X-axis is now changed, the respective resistors change depending on the current position.

Depending on how the resistors are distributed among each other, this results in a corresponding voltage value that can be measured between the resistors and thus determine the position of the axis.

Pin assignment

Code example Arduino

Pin assignment Arduino

Arduino Sensor
5 V +V
Ground GND
Pin A1 VRy
Pin A0 VRx
Pin 3 Knob

The program reads the current values of the input pins, converts them to a voltage (0-1023 -> 0 V-5 V) and outputs it in the serial output.

// Declaration and initialization of the input pins
int JoyStick_X = A0; // X-axis signal
int JoyStick_Y = A1; // Y-axis signal
int Button = 3; // Button
 
void setup ()
{
  pinMode (JoyStick_X, INPUT);
  pinMode (JoyStick_Y, INPUT);
  pinMode (Button, INPUT);
   
  // Since the button pulls the signal to ground when pressed,
  // here we switch on the pullup resistor
  digitalWrite(Button, HIGH);  
   
  Serial.begin (9600); // Serial output with 9600 bps
}
 
// The program reads the current values of the input pins
// and outputs them to the serial output
void loop ()
{
  float x, y;
  int button;
   
  //Actual values are read, converted to the voltage value....
  x = analogRead (JoyStick_X) * (5.0 / 1023.0); 
  y = analogRead (JoyStick_Y) * (5.0 / 1023.0);
  Button = digitalRead (Button);
   
  //... and output at this position
  Serial.print ("X axis:"); Serial.print (x, 4); Serial.print ("V, ");
  Serial.print ("Y axis:"); Serial.print (y, 4); Serial.print ("V, ");
  Serial.print ("Button:");
 
  if(button==1)
  {
      Serial.println (" not pressed");
  }
  else
  {
      Serial.println (" pressed");
  }
  delay (200);
}

Sample program download

KY023-Arduino.zip

Code example Raspberry Pi

Pin assignment Raspberry Pi

Raspberry Pi Sensor
GPIO 24 [Pin 18] button
3.3 V [Pin 1] +V
Ground [pin 6] GND
KY-053 A1 VRy
KY-053 A0 VRx
Sensor KY-053
VRy A1
VRx A0
+V 3.3V [Pin 1]
GND Ground [Pin 6]
Raspberry Pi KY-053
GPIO 3 [Pin 5] SCL
GPIO 2 [Pin 3] SDA

Analog sensor, therefore the following must be considered: The Raspberry Pi has, in contrast to the Arduino, no analog inputs or there is no ADC (analog digital converter) integrated in the chip of the Raspberry Pi. This limits the Raspberry Pi, if you want to use sensors, which do not output digital values, but a continuously changing value (example: potentiometer -> different position = different voltage value).

To avoid this problem, our sensor kit X40 contains the KY-053, a module with a 16-bit ADC, which you can use on the Raspberry to expand it with 4 analog inputs. This module is connected to the Raspberry Pi via I2C, takes over the analog measurement and transfers the value digitally to the Raspberry Pi.

So we recommend to connect the KY-053 module with the mentioned ADC in between for analog sensors of this set. You can find more information on the KY-053 Analog Digital Converter information page.

The program uses the corresponding ADS1x15 and I2C Python libraries from Adafruit to control the ADS1115 ADC. These have been published at the following link https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15 under the MIT license. The required libraries are not included in the download package below.

The program reads the current values of the input pins and outputs them to the console as a value in [mV].

Please note that you need to enable I2C on your Raspberry Pi before using this example.

#!/usr/bin/python
# coding=utf-8

import time
import board
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

Button_PIN = 24
GPIO.setup(Button_PIN, GPIO.IN, pull_up_down = GPIO.PUD_UP)

delayTime = 0.2
# Create the I2C bus
i2c = busio.I2C(board.SCL, board.SDA)

# Create the ADC object using the I2C bus
ads = ADS.ADS1115(i2c)

# Create single-ended input on channels
chan0 = AnalogIn(ads, ADS.P0)
chan1 = AnalogIn(ads, ADS.P1)
chan2 = AnalogIn(ads, ADS.P2)
chan3 = AnalogIn(ads, ADS.P3)


while True:
    #current values are recorded
    x = '%.2f' % chan0.voltage
    y = '%.2f' % chan1.voltage
 
    # Output to console
    if GPIO.input(Button_PIN) == True:
        print ("X-axis:", x, "V, ", "Y-axis:", y, "V, Button: not pressed")
    else:
        print ("X-axis:", x, "V, ", "Y-axis:", y, "V, button: pressed")
    print ("---------------------------------------")
 
    # reset + delay
    button_pressed = False
    time.sleep(delayTime)

Sample program download

KY023-RPi.zip

To start with the command:

sudo python3 KY023-RPi.py

Code example Micro:Bit

Pinout Micro:Bit:

Micro:Bit Sensor
3V +V
Ground GND
Pin 2 VRx
Pin 1 VRy
Pin 0 Button

Sample program download

microbit-KY-023.zip