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

In this joystick, a separate potentiometer was installed for the X-axis, as well as for the Y-axis. These result in a voltage divider, like this one which is shown in the following picture.

In the idle state, the potentiometer is in the middle, so that resistor1=resistor2, with which also the applied voltage distributes itself equally on both - e.g. measured value At +V=5V -> 2.5V.

If now the position of the e.g. X-axis is changed, the respective resistors change depending on the current position - e.g. if it goes in one direction, resistor 1 becomes smaller and resistor 2 larger, if it goes in the other direction, resistor 1 becomes larger and resistor 2 smaller.

Depending on how the resistors are distributed among each other, this results in a respective voltage value that can be measured between the resistors (in the case of the potentiometer, so-called sliders) and thus the position of the axis can be determined.

Pin assignment

Code example Arduino

Pin assignment Arduino

Arduino Sensor
5V +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 -> 0V-5V) and outputs it on 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.3V [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.

In contrast to the Arduino, the Raspberry Pi has 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, where not digital values are output [voltage value exceeded -> digital ON | voltage value undercut -> digital OFF | example: button pressed [ON] button released [OFF]], but it should be a continuous variable value (example: potentiometer -> other position = other voltage value).

To avoid this problem, our sensor kit X40 has the KY-053, a module with 16 bit accurate 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 passes the value digitally to the Raspberry Pi.

Thus, we recommend to connect the KY-053 module with the said ADC in between for analog sensors of this set. More information can be found on the information page about the KY-053 Analog Digital Converter.

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].

#!/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.py

Code example Micro:Bit

Pinout Micro:Bit:

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

This is a MakeCode example for Micro:Bit which essentially does the same as the examples of the other two variants. However, this example is closer to the Raspberry Pi example than the Arduino example.

Sample program download

microbit-KY-023.zip