In trying to troubleshoot this issue I've added the lines contained in the sticky post in this forum and verified that there is signal going from the joystick to the MCP3008.
I've attached a picture of my wiring if that is helpful, I have a raspberry pi 2 and I am attempting to wire the MCP3008 according to this:
Here is the code I'm using to test the device:MCP3008 Pi Wire Colour
-------------- ---------------- -----------
Pin 1 (CH0) - Purple
Pin 2 (CH1) - Blue
Pin 3 (CH2) - Green
Pin 9 (DGND) Pin 6 (Ground) Blue
Pin 10 (CS) Pin 24 (GPIO8) Orange
Pin 11 (DIN) Pin 19 (GPIO10) Yellow
Pin 12 (DOUT) Pin 21 (GPIO9) Green
Pin 13 (CLK) Pin 23 (GPIO11) Blue
Pin 14 (AGND) Pin 6 (Ground) Green
Pin 15 (VREF) Pin 1 (3.3V) Yellow
Pin 16 (VDD) Pin 1 (3.3V) Orange.
Code: Select all
#!/usr/bin/python
#--------------------------------------
# This script reads data from a
# MCP3008 ADC device using the SPI bus.
#
# Analogue joystick version!
#
# Author : Matt Hawkins
# Date : 17/04/2014
#
# http://www.raspberrypi-spy.co.uk/
#
#--------------------------------------
import spidev
import time
import os
# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)
# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data
# Define sensor channels
# (channels 3 to 7 unused)
swt_channel = 0
vrx_channel = 1
vry_channel = 2
# Define delay between readings (s)
delay = 0.5
while True:
# Read the joystick position data
vrx_pos = ReadChannel(vrx_channel)
vry_pos = ReadChannel(vry_channel)
# Read switch state
swt_val = ReadChannel(swt_channel)
# Print out results
print "--------------------------------------------"
print("X : {} Y : {} Switch : {}".format(vrx_pos,vry_pos,swt_val))
# Wait before repeating loop
time.sleep(delay)