Site Tools


Sidebar

Content

pub:ps2_joystick

PS2 joystick

what's PS2 joystick:

The PS2 Joystick Module is an input device. It consists of a station and the control knobonside. It functions by sending angle or direction signals to the device controlled. The button on the module can also be recognized by the microcontroller. The module supports two-channel analog output, namely, xand y-axis offset, and one-channel digital output which indicates whether the user has pressed the button at z-axis or not. The Joystick Module can be used to easily control the object to move in a three-dimensional space. For example, it can be applied to control crane, truck, electronic games, robots, etc.


X Analog output(X)
Y Analog output(Y)
Z Digital key output
+ VCC
- GND



microbit

Components
1 * microbit
1 * microbit expansion board
1 * USB cable
1 * PS2 joystick
- Several Jumper wires

connection diagram:


Code: Makecode:


MU Python:

#-----------------------------------------------------------
# File name   : Ps2_joystick.py
# Description : Read the ps2 joystick value in repl.
# Author      : jason
# E-mail      : jason@adeept.com
# Website     : www.adeept.com
# Date        : 2019/01/14
#-----------------------------------------------------------
from microbit import *
 
while True:
    x = pin0.read_analog()
    y = pin1.read_analog()
    z = pin5.read_digital()
    print(x,y,z)
    sleep(20)

Click “flash” and download the code onto the micro:bit. Effect Picture:



Arduino

Components
- 1 * Adeept Arduino UNO R3 Board
- 1 * Joystick Module
- 1 * USB Cable
- 1 * 5-Pin Wires

Step 1: Build the circuit:


Step 2: Code

/***********************************************************
File name: _27_JoystickModule.ino
Description: We measure the information of joystick module, 
             and displayed in the serial monitor. 
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2019/01/14 
***********************************************************/
int joystickXPin = 0;           // the joystick X pin connected to analog pin 0
int joystickYPin = 1;           // the joystick Y pin connected to analog pin 1
int joystickZPin = 2;           // the joystick S pin connected to digital pin 2
void setup() 
{                
  pinMode(joystickXPin, INPUT); //Set analog 0 port mode, the INPUT for the input
  pinMode(joystickYPin, INPUT); //Set analog 1 port mode, the INPUT for the input
  pinMode(joystickZPin, INPUT_PULLUP); //Set digital 2 port mode, the INPUT for the input
  Serial.begin(9600);          //opens serial port, sets data rate to 9600 bps
}
void loop() 
{ 
 Serial.print("X:");             //send "X:" to the serial monitor
 Serial.print(analogRead(0));    //send X data to the serial monitor
 Serial.print("  Y:");           //send "  Y:" to the serial monitor
 Serial.print(analogRead(1));    //send Y data to the serial monitor
 Serial.print("  Z:");           //send "  Z:" to the serial monitor
 Serial.println(digitalRead(2)); //send Z data to the serial monitor
 delay(50);                      //delay 0.05 s
}

Step 3: Compile and download the sketch to the UNO R3 board.



Open the Serial Monitor in Arduino IDE. When the PIR module detects human movement, “Invasion!” will be displayed on the window; otherwise, “No invasion”.



Raspberry pi

Components
- 1 * Raspberry Pi
- 1 * GPIO Extension Board
- 1 * 40-Pin GPIO Cable
- 1 * Breadboard
- 1 * PS2 Joystick Module
- 1 * ADC0832 Module
- 1 * 3-Pin Wires
- 2 * 5-Pin Wires

Build the circuit:


Code:
C code:

#include <wiringPi.h>
#include <stdio.h>
 
typedef unsigned char uchar;
typedef unsigned int uint;
 
#define     ADC_CS    0
#define     ADC_DIO   1
#define     ADC_CLK   2
#define  JoyStick_Z   3
 
uchar get_ADC_Result(uchar xyVal)
{
	//10:CH0
	//11:CH1
	uchar i;
	uchar dat1=0, dat2=0;
 
	digitalWrite(ADC_CS, 0);
 
	digitalWrite(ADC_CLK,0);
	digitalWrite(ADC_DIO,1);	delayMicroseconds(2);
	digitalWrite(ADC_CLK,1);	delayMicroseconds(2);
	digitalWrite(ADC_CLK,0);
 
	digitalWrite(ADC_DIO,1);    delayMicroseconds(2); //CH0 10
	digitalWrite(ADC_CLK,1);	delayMicroseconds(2);
	digitalWrite(ADC_CLK,0);
 
	if(xyVal == 'y'){
		digitalWrite(ADC_DIO,0);	delayMicroseconds(2); //CH0 0
	}
	if(xyVal == 'x'){
		digitalWrite(ADC_DIO,1);	delayMicroseconds(2); //CH1 1
	}
	digitalWrite(ADC_CLK,1);	
	digitalWrite(ADC_DIO,1);    delayMicroseconds(2);
	digitalWrite(ADC_CLK,0);	
	digitalWrite(ADC_DIO,1);    delayMicroseconds(2);
 
	for(i=0;i<8;i++)
	{
		digitalWrite(ADC_CLK,1);	delayMicroseconds(2);
		digitalWrite(ADC_CLK,0);    delayMicroseconds(2);
 
		pinMode(ADC_DIO, INPUT);
		dat1=dat1<<1 | digitalRead(ADC_DIO);
	}
 
	for(i=0;i<8;i++)
	{
		dat2 = dat2 | ((uchar)(digitalRead(ADC_DIO))<<i);
		digitalWrite(ADC_CLK,1); 	delayMicroseconds(2);
		digitalWrite(ADC_CLK,0);    delayMicroseconds(2);
	}
 
	digitalWrite(ADC_CS,1);
 
	pinMode(ADC_DIO, OUTPUT);
 
	return(dat1==dat2) ? dat1 : 0;
}
 
int main(void)
{
	uchar tmp;
	uchar xVal = 0, yVal = 0, zVal = 0;
 
	if(wiringPiSetup() == -1){
		printf("setup wiringPi failed !\n");
		return -1; 
	}
 
	pinMode(ADC_CS,  OUTPUT);
	pinMode(ADC_CLK, OUTPUT);
	pinMode(JoyStick_Z, INPUT);
 
	pullUpDnControl(JoyStick_Z, PUD_UP);
 
	while(1){
		pinMode(ADC_DIO, OUTPUT);
 
		xVal = get_ADC_Result('x');
		if(xVal == 0){
			tmp = 1; //up
			xVal = 127;
		}
		if(xVal == 255){
			tmp = 2; //down
			xVal = 127;
		}
 
		yVal = get_ADC_Result('y');
		if(yVal == 0){
			tmp = 3;
			yVal = 127;
		}
		if(yVal == 255){
			tmp = 4;
			yVal = 127;
		}
 
		zVal = digitalRead(JoyStick_Z);
 
		if(zVal == 0){
			printf("Button is pressed !\n");
		}
 
		switch(tmp){
			case 1: printf("...up\n"); 
					break;
			case 2: printf("down...\n"); 
					break;
			case 3: printf("...right\n"); 
					break;
			case 4: printf("left...\n"); 
					break;
			default:
					break;
		}
 
		delay(100);
	}
 
	return 0;
}

Python code:

#!/usr/bin/env python
#------------------------------------------------------
#
#		This is a program for JoystickPS2 Module.
#
#		This program depend on ADC0832 ADC chip. Follow 
#	the instruction book to connect the module and 
#	ADC0832 to your Raspberry Pi.
#
#------------------------------------------------------
import ADC0832_tmp
import RPi.GPIO as GPIO
import time
 
btn = 15	# Define button pin
 
def setup():
	ADC0832_tmp.setup()				# Setup ADC0832
	GPIO.setmode(GPIO.BOARD)	# Numbers GPIOs by physical location
	GPIO.setup(btn, GPIO.IN, pull_up_down=GPIO.PUD_UP)	# Setup button pin as input an pull it up
	global state
	state = ['up', 'down', 'left', 'right']	
 
def getResult():	#get joystick result
	if ADC0832_tmp.getResult1() == 0:
		return 1		#up
	if ADC0832_tmp.getResult1() == 255:
		return 2		#down
 
	if ADC0832_tmp.getResult() == 0:
		return 3		#left
	if ADC0832_tmp.getResult() == 255:
		return 4		#right
 
	if GPIO.input(btn) == 0:
		print 'Button is pressed!'		# Button pressed
 
def loop():
	while True:
		tmp = getResult()
		if tmp != None:
			print state[tmp - 1]
 
def destory():
	GPIO.cleanup()				# Release resource
 
if __name__ == '__main__':		# Program start from here
	setup()
	try:
		loop()
	except KeyboardInterrupt:  	# When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
		destory()

For C language users:

Step 2: Edit and save the code with vim or nano.

(code path: /home/Adeept_Sensor_Kit_for_RPi_C_Code/34_PS2Joystick/joystick.c)

Step 3: Compile

$ sudo gcc joystick.c -o joystick -lwiringPi

Step 4: Run

$ sudo ./joystick

For Python users:

Step 2: Edit and save the code with vim or nano.

(code path: /home/Adeept_Sensor_Kit_for_RPi_Python_Code/34_PS2Joystick.py)

Step 3: Run

$ sudo python 34_PS2Joystick.py

Press or pull the knob and you will see the value of current status displayed on the terminal.


Link for code download:http://www.adeept.com/learn/ Download the kit information as needed.
Video link:http://www.adeept.com/video/

pub/ps2_joystick.txt · Last modified: 2019/01/15 10:06 by Adeept