• Arduino
  • Raspberry Pi
  • Raspberry Pi Pico
  • Micro:Bit

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 +5V
GND GND
Pin A1 VRy
Pin A0 VRx
Pin 3 Button

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

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 Raspberry Pi

Pin assignment Raspberry Pi

Raspberry Pi Sensor
GPIO 24 [Pin 18] Button
3,3 V [Pin 1] +5V
GND [Pin 6] GND
- VRy
- VRx
Sensor KY-053
VRy A1
VRx A0
Raspberry Pi KY-053
GPIO 3 [Pin 5] SCL
Gpio 2 [Pin 3] SDA
3,3 V [Pin 17] VDD
GND [Pin 14] GND

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

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 Micro:Bit

Pinout Micro:Bit:

Mico:Bit Sensor
Pin 0 Button
3,3 V +5V
GND GND
- VRy
- VRx
Sensor KY-053
VRy A1
VRx A0
Mico:Bit KY-053
Pin 19 SCL
Pin 20 SDA
3,3 V VDD
GND GND

Analog sensor, therefore the following must be observed: The Micro:Bit has analog inputs or there is an ADC (analog digital converter) integrated in the chip of the Micro:Bit. However, these are only limited to 10-bit and therefore offer only a rather low accuracy for analog measurements.

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 Micro:Bit to expand it by 4 analog inputs. This is connected to the Micro:Bit via I2C, takes over the analog measurement and transfers the value digitally to the Micro:Bit.

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

The program uses the corresponding library from us to control the ADS1115 ADC. This has been published under the following link pxt-ads1115 under the MIT-License.

Sample program download

microbit-KY-023.zip

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 Raspberry Pi Pico

Pin assignment Raspberry Pi Pico

Raspberry Pi Pico Sensor
GND GND
3 V +V
- VRx
- VRy
GPIO28 SW
Sensor KY-053
GND -
+V -
VRx A0
VRy A1
SW -
Raspberry Pi Pico KY-053
GPIO1 SCL
GPIO0 SDA
3 V VDD
GND GND

Analog sensor, therefore the following must be observed

The Raspberry Pi Pico has analog inputs for the internal ADC (analog digital converter) in the chip of the Raspberry Pi Pico, but this ADC has only a resolution of 12-bit.

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

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

The program uses the corresponding ADS1115-Micropython library from Joy-IT to control the ADS1115 ADC. This has been published under the following linkhttps://github.com/joy-it/ADS1115-Micropython under the MIT License. The required library is included in the download package below.

This is a sample program that outputs text serially when a signal change is detected at the sensor.

# Load libraries
from machine import Pin
import ADS1115
import utime

# Initialization of the ADC
ADS1115.init(0x48, 1, 4, False)
# Initialization of GPIO28 as input
SW = Pin(28,Pin.IN, Pin.PULL_UP)

# Endless loop for reading the ADC and switch
while True:
    xAxis = ADS1115.read(0)
    utime.sleep_ms(20)
    yAxis = ADS1115.read(1)
    switch = SW.value()

    # Serial output of the analog values
    print("X-axis: " + str(xAxis) + ", Y-axis: " + str(yAxis) + ", Switch " + str(switch))
    if switch == 0:
        print("Button is pressed.")
    print(" ")
    utime.sleep_ms(500)

Example program download

KY023-Pico.zip