Skip to content

Instantly share code, notes, and snippets.

@raspberrytipsnl
Created February 4, 2019 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raspberrytipsnl/9536226c3bf84a7f10af0c263b87a5d7 to your computer and use it in GitHub Desktop.
Save raspberrytipsnl/9536226c3bf84a7f10af0c263b87a5d7 to your computer and use it in GitHub Desktop.
Analog Joystick / MCP3008 / Raspberry Pi
#!/usr/bin/python
import spidev
import time
import os
spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz=1000000
def spiChannel(channel):
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data
while True:
button = spiChannel(0)
xas = spiChannel(1)
yas = spiChannel(2)
print("X-as: {} Y-as: {} Button: {}".format(xas,yas,button))
time.sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment