Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rasp Pi Analog Joystick
#1
I am currently trying to connect my raspberry pi 3 to an analog joystick controller using an ADC but when I run the code and move the joystick, it always returns a position of 0. Any help would be greatly appreciated.

#!usr/bin/python
import spidev
import os
import time

#Define Axis Channels
swt_channel = 0
vrx_channel = 1
vry_channel = 2

#Time Delay
delay = 0.5

#Spi oeffnen
spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz=1000000

#Function for reading the MCP3008 channel between 0 and 7
def readChannel(channel):
    val = spi.xfer2([1,(8+channel)<<4,0])
    data = ((val[1]&3) <<8) + val[2]
    return data

#endless loop
while True:
    #Determine position
    vrx_pos = readChannel(vrx_channel)
    vry_pos = readChannel(vry_channel)

    #SW determine
    swt_val = readChannel(swt_channel)

    #output
    print("VRx : {} VRy : {} SW : {}".format(vrx_pos,vry_pos,swt_val))

    # wait
    time.sleep(delay)
Reply
#2
I found this video: https://www.youtube.com/watch?v=fX225p-Sh58 which interfaces an analog joystick
to pi using the ADS7830 ADC board. Code is in python, but does not use the spidev package
You didn't specify what you are using as an interface (ADC) however the code should be similar.

Seems to be quite thorough.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Converting PyGame 2 axis joystick float to 360 angle archieab 1 3,356 Sep-26-2018, 05:40 PM
Last Post: archieab
  [PyGame] Using joystick module from PyGame archieab 6 16,253 Sep-25-2018, 06:13 PM
Last Post: archieab
  [PyGame] Joystick Input microphone_head 2 9,430 Sep-16-2018, 06:02 AM
Last Post: microphone_head
  [PyGame] Limiting a Joystick value -1 to +1, to a value between 0 an 127 with 64 being center? japreja 2 4,479 Dec-10-2017, 06:20 AM
Last Post: japreja

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020