How to setup 2 Axis analog joystick with Orange Pi Zero

We will be now setting up Joystick with Orange Pi zero, if you are interested in how to setup joystick with Arduino check this link.  Since Orange Pi Zero does not have an Analog input you need to use an external analog to digital convertor. In this case I have used MCP3208. Below are the list of hardware you will need to set this up.

Hardware required:

and A MCP3208 Chip and lots of connecting wires

I have writting an article on how to setup mcp3208 with orange Pi.

Circuit Connection:

After setting up the connection it looks like this

Software installation

I have used Armbian image as the base operating system and installed the required python packages in a python virtual environment.

apt-get install python-dev python-pip
virtualenv localpy
source localpy/bin/activate
pip install spidev
git clone
cd orangepi_PC_gpio_pyH3
python install

If you are interested about gpio then refer to my article on how to
setup GPIO for Orange Pi Zero.
Now when everything is done then copy this code to your orange Pi Zero
and run.

import spidev
import time
from pyA20.gpio import gpio
from pyA20.gpio import connector
from pyA20.gpio import port

class MCP3208:
        def __init__(self, spi_channel=0):
                self.spi_channel = spi_channel
                self.conn = spidev.SpiDev(1, spi_channel)
                self.conn.max_speed_hz = 1000000 # 1MHz

        def __del__( self ):

        def close(self):
                if self.conn != None:
                        self.conn = None

        def bitstring(self, n):
                s = bin(n)[2:]
                return '0'*(8-len(s)) + s

	def ReadInput(self,Sensor):
		adc = self.conn.xfer2([1,(8+Sensor)<<4,0])
		data = ((adc[1]&3) << 8) + adc[2]
		return data

        def read(self, adc_channel=0):
                # build command
                cmd  = 128 # start bit
                cmd +=  64 # single end / diff
                if adc_channel % 2 == 1:
                        cmd += 8
                if (adc_channel/2) % 2 == 1:
                        cmd += 16
                if (adc_channel/4) % 2 == 1:
                        cmd += 32

                # send & receive data
                reply_bytes = self.conn.xfer2([cmd, 0, 0, 0])

                reply_bitstring = ''.join(self.bitstring(n) for n in reply_bytes)
#                print reply_bitstring

                # see also... (page.20)
                reply = reply_bitstring[5:17]
                return int(reply, 2)

if __name__ == '__main__':
        spi = MCP3208(0)
	button = port.PA12
	gpio.setcfg(button, gpio.INPUT)
	gpio.pullup(button, gpio.PULLUP)
        a1 = 0
        a2 = 0
        while True:
                a1 = spi.ReadInput(1)
                a2 = spi.ReadInput(2)
		state = gpio.input(button) 
		print "X Axis=%04d, Y Axis=%04d, Switch :%d" % (a1, a2,state)

Check the output here

One thought on “How to setup 2 Axis analog joystick with Orange Pi Zero

Leave a Reply

Your email address will not be published. Required fields are marked *