pierpa86
Posts: 15
Joined: Sat Apr 16, 2016 8:45 am

Rasp pi3 and MCP3008 + Joystick

Sat Apr 16, 2016 8:54 am

Hi at all man, i try to follow this tutorial " http://www.raspberrypi-spy.co.uk/2014/0 ... n-mcp3008/ " but i make a mistake and i have used 5v and not 3.3V, can i have broke the MCP3008?
another question = I have to use the same GPIO pin ? or can I use what I want ?
another question = the program at botton page (mcp3008_joystick.py) can find the correct GPIO port ? or i need to specific the GPIO port in the program ?

i'm a noob now with a lot of question pls help me :)

pierpa86
Posts: 15
Joined: Sat Apr 16, 2016 8:45 am

Re: Rasp pi3 and MCP3008 + Joystick

Mon Apr 18, 2016 8:02 am

no one ? :lol:

User avatar
LetHopeItsSnowing
Posts: 357
Joined: Sat May 26, 2012 6:40 am
Location: UK
Contact: Website

Re: Rasp pi3 and MCP3008 + Joystick

Mon Apr 18, 2016 12:52 pm

You may have broken you mcp3008, but I suspect not, im pretty sure they are 5 volt safe.

I dont really understand your questions, but I have tried my best to answer.
another question = I have to use the same GPIO pin ? or can I use what I want ?
The MCP3008 communicates over SPI (its a protocol) and you will have to use the SPI pins on the raspberry pi. You cant change them.

You can however connect the joystick up to different channels on the MCP3008.
another question = the program at botton page (mcp3008_joystick.py) can find the correct GPIO port ? or i need to specific the GPIO port in the program ?
You dont need to specify the GPIO port in the program as it uses SPI to communicate with the MCP3008.
"am I getting slower, or is stuff more complicated; either way I now have to write it down - stuffaboutcode.com"

pierpa86
Posts: 15
Joined: Sat Apr 16, 2016 8:45 am

Re: Rasp pi3 and MCP3008 + Joystick

Mon Apr 25, 2016 7:40 pm

thz a lot man, you have solved my problem :D my error is the choise of GPIO pin, i see now the right pin for Spi interface, but now the question is, if i already using the Spi pin for the touch panel i can use the same for the analog convert ? two device for the same pin ?

DirkS
Posts: 9936
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Rasp pi3 and MCP3008 + Joystick

Mon Apr 25, 2016 8:20 pm

pierpa86 wrote:thz a lot man, you have solved my problem :D my error is the choise of GPIO pin, i see now the right pin for Spi interface, but now the question is, if i already using the Spi pin for the touch panel i can use the same for the analog convert ? two device for the same pin ?
On a Pi with a 40pin header (that's from the B+ up) you can use SPI1 and use just some of the physical pins from 27 - 40 (see http://pinout.xyz/pinout/spi)
f you add

Code: Select all

dtoverlay=spi1-1cs,cs0_pin=16
to /boot/config.txt you should have a new SPI device (/dev/spidev1.0)

Wiring:
GPIO16 - CS
GPIO19 - MISO
GPIO20 - MOSI
GPIO21 - CLK

(see your /boot/overlays/README for some more info and possibilities)
Tested this yesterday with an ENC28J60 SPI network module on SPI1 and it worked fine.

pierpa86
Posts: 15
Joined: Sat Apr 16, 2016 8:45 am

Re: Rasp pi3 and MCP3008 + Joystick

Sun May 08, 2016 8:58 pm

thz for all that's work i can read value and are correct but now the question is what driver i can use for use with retropie as controller ?

pierpa86
Posts: 15
Joined: Sat Apr 16, 2016 8:45 am

Re: Rasp pi3 and MCP3008 + Joystick

Fri May 13, 2016 9:47 am

solved, i coded a driver in python using uinput, next time i make a guide for this problem for all ppl like me. thz for all :)

PinerdF
Posts: 21
Joined: Wed Sep 30, 2015 8:23 am

Re: Rasp pi3 and MCP3008 + Joystick

Mon Sep 05, 2016 9:18 am

hey, I did successfully follow the mcp3008 Tutorial of how to access a analog Joystick and I'm able to read out the digital data of the analog joystick like shown in the tutorial.

Now I'm trying to use this analog joystick as retropie input controller. I read about using additional ADCs like teensy 2.0 or arduino pro micro, but as I can see the digital data on the raspberry pi already I think it should be possible to somehow convert that data to be accepted by retropie.

Could you share your python driver for this issue?

pierpa86
Posts: 15
Joined: Sat Apr 16, 2016 8:45 am

Re: Rasp pi3 and MCP3008 + Joystick

Wed Jan 18, 2017 10:18 pm

Enjoy, this is my code, virtual GamePad with Analog Joystick X and Y axis and 12 buttons:

- from MCP3008 Channel 0 to 5 used for Buttons
- MCP3008 Channel 6 and 7 used for Joystick axis X - Y
- GPIO pin 21, 20, 16, 26, 19, 13 (with GRD pin) other 6 buttons

:mrgreen:

Code: Select all

#MCP3008 Analog X Y BTN 0-5
#GPIO0 BTN 6-11

#!/usr/bin/python

import RPi.GPIO as GPIO 
import spidev
import time
import os
import uinput


#Open GPIO0, GPIO1, GPIO2, GPIO3, GPIO4 : Set GPIO Channel here
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# 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

device = uinput.Device([uinput.BTN_0, uinput.BTN_1, uinput.BTN_2,
                        uinput.BTN_3, uinput.BTN_4, uinput.BTN_5,
					    uinput.BTN_6, uinput.BTN_7, uinput.BTN_8,
						uinput.BTN_9, uinput.BTN_BASE, uinput.BTN_BASE2,
                        uinput.ABS_X+(170,800,0,0),
                        uinput.ABS_Y+(170,800,0,0),
                      ])

#MCP3008 Channel					  
joy_x=6
joy_y=7
joy_buttonE=0
joy_buttonB=1
joy_buttonC=2
joy_buttonD=3
joy_buttonA=4
joy_buttonF=5



while True:

	#GPIO - BTN_6
    input_state = GPIO.input(21)
    if input_state == False:
      device.emit(uinput.BTN_6, 1)
    else:
      device.emit(uinput.BTN_6,0)
	  
	#GPIO - BTN_7
    input_state = GPIO.input(20)
    if input_state == False:
      device.emit(uinput.BTN_7, 1)
    else:
      device.emit(uinput.BTN_7,0)
	  
	#GPIO - BTN_8
    input_state = GPIO.input(16)
    if input_state == False:
      device.emit(uinput.BTN_8, 1)
    else:
      device.emit(uinput.BTN_8,0)
	  
	#GPIO - BTN_9
    input_state = GPIO.input(26)
    if input_state == False:
      device.emit(uinput.BTN_9, 1)
    else:
      device.emit(uinput.BTN_9,0)
	  
	#GPIO - BTN_BASE
    input_state = GPIO.input(19)
    if input_state == False:
      device.emit(uinput.BTN_BASE, 1)
    else:
      device.emit(uinput.BTN_BASE,0)
	  
	#GPIO - BTN_BASE2
    input_state = GPIO.input(13)
    if input_state == False:
      device.emit(uinput.BTN_BASE2, 1)
    else:
      device.emit(uinput.BTN_BASE2,0)
	  
	#MCP3008 - X
    joy_x_value=ReadChannel(joy_x) 
    device.emit(uinput.ABS_X,joy_x_value,syn=False)
	
	#MCP3008 - Y
    joy_y_value=ReadChannel(joy_y)  
    device.emit(uinput.ABS_Y,joy_y_value)
	
	#MCP3008 - BTN_0
    joy_buttonA_value=ReadChannel(joy_buttonA)
    if joy_buttonA_value<10:
      device.emit(uinput.BTN_0,1)
    else:
      device.emit(uinput.BTN_0,0)
	
	#MCP3008 - BTN_1
    joy_buttonB_value=ReadChannel(joy_buttonB)

    if joy_buttonB_value<10:
      device.emit(uinput.BTN_1,1)
    else:
      device.emit(uinput.BTN_1,0)

    joy_buttonC_value=ReadChannel(joy_buttonC)
	
	#MCP3008 - BTN_2
    if joy_buttonC_value<10:
      device.emit(uinput.BTN_2,1)
    else:
      device.emit(uinput.BTN_2,0)

    joy_buttonD_value=ReadChannel(joy_buttonD)
	
	#MCP3008 - BTN_3
    if joy_buttonD_value<10:
      device.emit(uinput.BTN_3,1)
    else:
      device.emit(uinput.BTN_3,0)

    joy_buttonE_value=ReadChannel(joy_buttonE)
	
	#MCP3008 - BTN_4
    if joy_buttonE_value<10:
      device.emit(uinput.BTN_4,1)
    else:
      device.emit(uinput.BTN_4,0)
	
	#MCP3008 - BTN_5
    joy_buttonF_value=ReadChannel(joy_buttonF)

    if joy_buttonF_value<10:
      device.emit(uinput.BTN_5,1)
    else:
      device.emit(uinput.BTN_5,0)
	  
time.sleep(0.2)

Return to “Troubleshooting”