How to connect Analog Joystick to Raspberry Pi - Page 1
Forum section: electronics / general

author: ironguy
author_rank: Newbie
timestamp: January 31, 2017, 11:39:52 am
content: I'm creating my own Arcade Console using Raspberry Pi and some buttons. The thing is that a button works (it is activated) when it is connected to ground, when the circuit is closed. I can not use the same approach with an analog joystick because it works different. How do I arrange it?PS: The software I'm using is RetroPie and Retrogame to handle the hardware.

author: Benta
author_rank: Super Contributor
timestamp: January 31, 2017, 04:35:12 pm
content: If you're talking analog joystick like they are known from RC planes, it's just two potentiometers at right angles to each other. One end the pots are connected to plus, the other end to ground, and the two slider outputs give you a voltage according to the position.You'll need to use the A/D converters on your board.

author: artag
author_rank: Super Contributor
timestamp: January 31, 2017, 04:51:38 pm
content: It's easy with an ADC, which the Pi doesn't have. But a very crude ADC will do. PC and Amiga joysticks used just a capacitor and a digital i/o pin. Set the i/o pin low to discharge the capacitor. Make the i/o pin an input, and allow the capacitor to charge through the joystick. When the voltage reaches the digital input threshold, the input pin changes state : note how long it's been since it was started. The time/voltage relationship is exponential and the input threshold poorly defined, so calculate the resistance and calibrate for the central rest position.Some small resistors are added to ensure the joystick set to zero ohms or the discharge of the capacitor into the i/o pin doesn't cause excessive current to flow.

author: ironguy
author_rank: Newbie
timestamp: January 31, 2017, 05:00:54 pm
content: @artag @Benta I'm using a playstation 2 Joystick, like this:https://cdn.sparkfun.com//assets/parts/2/4/2/7/09032-03-L.jpgHow do I do for when it passes the threshold the circuit is closed, I can only think in a rele but it's not very efficient

author: artag
author_rank: Super Contributor
timestamp: January 31, 2017, 05:24:21 pm
content: Here's a more detailed description :http://www.raspberrypi-spy.co.uk/2012/08/reading-analogue-sensors-with-one-gpio-pin/In that example, there's an LDR. For the joystick, use just 2 pins of the potentiometer (middle and 1 outer).

author: ironguy
author_rank: Newbie
timestamp: January 31, 2017, 05:33:06 pm
content: @artag I cannot modify Retrogame code (because I'm not that professional). I just need to convert the two potentiometers from the joystick (X and Y) into 4 digital signals (up down left and right) that output 0 volts (the circuit is closed and they are connected to the ground from Raspberry) whenever they are activated

author: Benta
author_rank: Super Contributor
timestamp: January 31, 2017, 05:56:56 pm
content: OK, I'll retire from this thread. I've never owned a PS 2 or even used one.

author: Ian.M
author_rank: Super Contributor
timestamp: January 31, 2017, 07:10:32 pm
content: So you need a hardware analog joystick interface that Retrogame supports. Without digging into the details of what is and isn't supported, probably the easiest option is a USB gameport adaptor, which makes analog or digital joysticks with a DA-15 PC gameport connector appear as HID joysticks. However the original PC gameport 'standard' used 100K linear potentiometers, wired as variable resistors in the joystick, and if the joystick you want to use has potentiometers too far from the 100K norm, you may not be able to get it to calibrate properly or even work at all.

author: ironguy
author_rank: Newbie
timestamp: January 31, 2017, 10:53:28 pm
content: The thing is that I'm building my own arcade and it's a project for learning how to solve this kind of problems. I'm trying to avoid shortcuts like buying a digital Joystick or any other way.

author: Ian.M
author_rank: Super Contributor
timestamp: January 31, 2017, 11:16:06 pm
content: I didn't say get a joystick, I said get a gameport adapter to give you the analog joystik axis inputs you need. If you don't want to go down that road, you'll either need to dig through the RetroPie and Retrogame documentation to see what analog interfaces are supported and buy or build one, or you'll need to do quite a lot of coding (with a very steep learning curve) to emulate an analog HID joystick (or one of the other supported types), either on the PI reading an add-on ADC or on a MCU with its own built in ADC.

author: artag
author_rank: Super Contributor
timestamp: January 31, 2017, 11:49:47 pm
content: So you want to use the analog joystick to provide simpler digital signals ?In that case, you need a comparator, such as an LM339. You can wire the potentiometers between 0 and 3V3 and have a couple of thresholds at about 1V and 2V. Each comparator has an analog slider on one input and one of the voltage references on the other. When the joystick passes a threshold, the output flips.You could probably hack something to work using just resistors, but it requires a fair bit of calculation and some fiddling about. The comparators are a better way to educate yourself about the electronics function.

author: Ian.M
author_rank: Super Contributor
timestamp: January 31, 2017, 11:56:30 pm
content: Using an analog joystick as a bang-bang digital control is guaranteed to produce a crappy UI. There's no 'feel' when the joystick passes each switching threshold so the only option for reliable fast control is to mash the stick to its limits. Its even viler if it isn't self-centering.

author: ironguy
author_rank: Newbie
timestamp: February 01, 2017, 09:45:16 am
content: Check this outhttps://learn.adafruit.com/cupcade-raspberry-pi-micro-mini-arcade-game-cabinet/interface-boardHere they use the same Joystick for the same system, it's a clue for guessing how to do it

author: Ian.M
author_rank: Super Contributor
timestamp: February 01, 2017, 10:22:30 am
content: If you are going for the digital bang-bang converter interface, the easiest option would be to ground each pot wiper and use a fixed pullup resistor of approximately 1/4 the pot track resistance on each end of the track. Route each track end to the +in of a separate comparator. You can use a quad comparator chip (e.g. LM339) to handle two axes. All the -in pins go to the wiper of a preset with its track wired between Vdd and Gnd, which adjusts the thresholds i.e. how far you have to push the stick to register a button press. You may wish to use separate presets for the horizontal and vertical -in pins so you can set the horizontal and vertical thresholds separately. The LM339 open collector outputs can be treated as-if they are switches wired between <whatever> (e.g. a PI I/O pin) and ground. When the pot is pushed towards its track end, it pulls the corresponding +in pin below the reference voltage, which makes the comparator switch on its output transistor, pulling its output pin low. As the outputs are open collector they can safely drive 3.3V logic (as long as any pullups are wired to +3.3V) even if the LM339 tself and the joystick is powered from a +5V supply.However I *still * reckon it will suck compared to a real analog joystick interface, even if you add a second LM339 and preset to set a second threshold for each direction to detect the stick being right over to operate an 'accelerator' button from any combo of its outputs wire-ORed together.

author: @rt
author_rank: Super Contributor
timestamp: September 09, 2017, 10:52:46 am
content: Old thread I know, but relevant.Ian, Have you tried this? My complaints as a consumer wouldn’t be the same as yours, and my overall experience not nearly so harsh.The first thing I would change is the analogue thumb sticks have too much physical travel.It would be nice to have a smaller collar, but the lack of feedback feeling the microswitches close didn’t enter my mind.I did this myself a year ago, and have been using it since , with a parallel input shift register, and resistors that make a divider in the same manner as yours,and present the logic low level at the shift register inputs at a chosen positions. Pots can also be used to trim the joystick dead zone.I’m planning on stealing yours though, as I couldn’t give definite resistor values for every shift register with potentially different logic level thresholdsif different people reproduced a microcontroller project. I plan to link back here, not pretending I came up with the comparator circuit.I think it's superior for mouse cursor movement (if that has to be the interface) where analogue input pins on the micro weren’t available.The Amiga can take a digital joystick mouse cursor input too, and it feels more natural with that as well.Quote from: Ian.M on February 01, 2017, 10:22:30 am

author: Ian.M
author_rank: Super Contributor
timestamp: September 09, 2017, 12:25:26 pm
content: I can't claim any originality for the the idea, but it does seem there are very few projects that use a LM339 and an analog stick with the wipers grounded. There do seem to be an excessive number of over-complex window comparator based circuits. Sometimes you have no choice - like when some F*idiot*sells a pot breakout board that ties the X axis and Y axis track ends together on the assumption that you will always want to use them as rail to rail potentiometers and will never need to add trim resistors at the track ends to limit the output range - or if you are interfacing to an unmodified preassembled joystick + housing that wires them that way.I don't play action games so haven't had occasion to build analog to digital joystick interface in a VERY long time. IIRC it was about 30 years ago, was for an Amiga and emulated a quadrature mouse. The details are extremely fuzzy but IIRC it used something like my suggested circuit above to get the directions and also had a job lot of diodes routing both ends of each axis to a VCO for each axis to control the 'mouse' movement rate.Anyone with either a lathe or a 3D printer should be able to come up with a usable limiting collar for their preferred stick. I would suggest including an nO ring groove or a rubber washer so there's some resilience when you mash the stick to its limit. Tactile feedback is harder - maybe include a solenoid as a 'thumper' and pulse briefly any time one of the LM339 outputs goes low.Re: linking here, great, but please post the schematic to this thread as well.*Adafruit - I'm looking at yoooou!

author: @rt
author_rank: Super Contributor
timestamp: September 09, 2017, 03:36:48 pm
content: Nothing like that Amiga mouse idea has come up in Amiga group in years.I think that would go down well. Even a joystick that included the mouse control. The enclosure for anything like that is hard.The real project for me is a microcontroller one. I just tested it out on the Amiga to see if it really does suck.Don’t get me wrong, for a game written with 8 way joystick in mind, the 8 way joystick is better, the analogue one.. well it doesn’t totally suckIf you’re talking about my first circuit, I wouldn’t promote it due to the different possible input IO characteristics people’s applications could have.Mainly logic low threshold voltage so far. For the LM339 circuit, I’m happy to share a schematic which is almost done.The joysticks come off the board with a jet lighter on the back as fast as possible.. rework blower, and hand iron both melt the joystick!

author: Ian.M
author_rank: Super Contributor
timestamp: September 09, 2017, 06:03:33 pm
content: Nowadays the obvious way to do the 'Joymouse' would be using a MCU. IIRC the original had a decent sized veroboard full of chips. First the analog signal to the VCOs had to be scaled and run through a squarer to give progressive acceleration, then the VCO outputs had to be gated by the LM339 outputs to get X and Y axis up and down pulsetrain signals, which then had to be applied to up/down counters and the outputs decoded to 2 bit Grey code. IIRC there would have been about as much logic again to support a 'T' adapter that could also be controlled by a real mouse.