KY-023 Joystick Modul (XY-Achsen)

Technical data / Short description

X and Y positions of the joystick will be outputted as analog voltages at the output pin.

In this joystick, the x-axis and the y-axis have their own potentiometer. Together they build a voltage devider like the one in the next picture.

 In the non-operating mode, the potentiometer is in the middle so that risistor1=resistor2, so that the voltage will equally split to both resistors - e.g. Measurement of +V=5V -> 2,5V. If now one of the axis changes like the x-axis for example, the values of the resistors will change - e.g. value of resistor 1 will raise than the value of resistor 2 will fall or the value of resistor 1 will fall and the value of resistor 2 will raise. According to the division of the resistor values, you can measure a specific voltage value between the resistors and locate the position of the axis.

Code example Arduino

This program measures the value at the input pins, converts them into a voltage (0-1023 -> 0V-5V) and output these at the serial ouput.

```// Declaration and initialization of the input pin
int JoyStick_X = A0; // X-axis-signal
int JoyStick_Y = A1; // Y-axis-signal
int Button = 3;

void setup ()
{
pinMode (JoyStick_X, INPUT);
pinMode (JoyStick_Y, INPUT);
pinMode (Button, INPUT);

// pushing the button leads to
// power up 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 at the serial output
void loop ()
{
float x, y;
int Knopf;

// Current values will be read and converted to the right voltage
x = analogRead (JoyStick_X) * (5.0 / 1023.0);
y = analogRead (JoyStick_Y) * (5.0 / 1023.0);
Knopf = digitalRead (Button);

//... and outputted here
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(Knopf==1)
{
Serial.println ("not pushed");
}
else
{
Serial.println ("pushed");
}
delay (200);
}
```

Connections Arduino:

 Button = [Pin 3] Y-Position = [Pin A1] X-Position = [Pin A0] Sensor +V = [Pin 5V] Sensor GND = [Pin GND]

Code example Raspberry Pi

!! Attention !! Analog sensor  !! Attention !!

Unlike the Arduino, the Raspberry Pi dont provide an ADC ( analog digital converter ) on its chip. This gives the Raspberry Pi limits if you want to use a non digital sensor [ voltage value exceeded -> digital ON | voltage value deceeded -> digital OFF | example : button pushed [ON] button released [OFF]], but rather its a continous changeable value ( example: potentiometer -> other position = other voltage value).

To evade this, use our Sensorkit X40 with the KY-053 module which provides a 16 Bit ADC, which can be used  with the Raspberry Pi, to upgrade it with 4 additional analog input pins. This will be connected via I2C from the Raspberry Pi, it takes the analog measurment and gives the digital value to the Raspberry Pi.

So we recommend for analog sensors from this set, to interpose the KY-053 module with the ADC. For more information please look at the infosite

!! Attention !! Analog Sensor  !! Attention !!

The program reads the current values of the input pins and output it as [mV] at the terminal.

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

#############################################################################################################
### Copyright by Joy-IT
### Published under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
### Commercial use only after permission is requested and granted
###
### KY-023 Joystick Module - Raspberry Pi Python Code Example
###
#############################################################################################################

# This code is using the ADS1115 and the I2C python library for the Raspberry Pi
# These is published by BSD Licence under the following link
from time import sleep

# Additional needed modules will be imported and configured
import time, signal, sys, os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# Used variables will be initialized
delayTime = 0.2

ADS1115 = 0x01  # 16-bit

# Amplification (Gain) will be picked
gain = 4096  # +/- 4.096V
# gain = 2048  # +/- 2.048V
# gain = 1024  # +/- 1.024V
# gain = 512   # +/- 0.512V
# gain = 256   # +/- 0.256V

# Sampling rate of the ADC (SampleRate) will be picked
# sps = 8    # 8 Samples per second
# sps = 16   # 16 Samples per second
# sps = 32   # 32 Samples per second
sps = 64   # 64 Samples per second
# sps = 128  # 128 Samples per second
# sps = 250  # 250 Samples per second
# sps = 475  # 475 Samples per second
# sps = 860  # 860 Samples per second

# ADC-Channel (1-4) will be picked
x_adc_channel = 0    # Channel 0 for the x-axis
y_adc_channel = 1    # Channel 1 for the y-axis
# adc_channel = 2    # Channel 2
# adc_channel = 3    # Channel 3

# Here the ADC will be initialized - the ADC which is used by the KY-053 is an ADS1115 chipset.

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

#############################################################################################################

# ########
# main program loop
# ########
# The program reads the current values of the input pins
# and outputs the values at the terminal

try:
while True:
# Current values will be recorded

# Output at the terminal
if GPIO.input(Button_PIN) == True:
print "X-axis:", x,"mV, ","Y-axis:", y,"mV, Button: not pushed"
else:
print "X-axis:", x, "mV, ", "Y-axis:", y, "mV, Button: pushed"
print "---------------------------------------"

# Reset + Delay
button_pressed = False
time.sleep(delayTime)

except KeyboardInterrupt:
GPIO.cleanup()

```

Connections Raspberry Pi:

Sensor KY-023

 Button = GPIO24 [Pin 18 (RPi)] Y-position = Analog 1 [Pin A1 (ADS1115 - KY-053)] X-position = Analog 0 [Pin A0 (ADS1115 - KY-053)] +V = 3,3V [Pin 1 (RPi)] GND = GND [Pin 6 (RPi)]

```sudo python KY-023_RPi_JoystickModule.py