"Potentiometer" with only 2 switches? - Page 1
Forum section: electronics / beginners

author: Mpegger
author_rank: Contributor
timestamp: February 28, 2022, 04:42:34 am
content: Greetings. This is my first time posting here, as I am in need of help trying to wire up a hat switch to my arduino.Let me just preface this with I have no real electrical or programming knowledge, other then what I've tinkered with over the years. I can follow a schematic and solder up a circuit board, but other then simple LED circuits, I don't have any other knowledge to build any other type of circuit myself. Same deal with programming, I can follow along with an example program, but I wouldn't be able to come up with a program of my own if I don't have plenty of example code to follow along with. That being said...I have a old RC car transmitter style Playstation 1 controller that I want to wire up with an Arduino Pro Micro, so that I can use it with my PC as a game controller. It has 2 axis (steering and a throttle/brake combo), 12 buttons, and a hat switch. Unfortunetly, the Arduino Pro Micro that I am using for this project only has 12 inputs, along with 4 analog inputs. I would not be able to use all 12 buttons and the hat switch, and would have to give up at least 2 buttons and using the remaining free 2 analog inputs for regular buttons.But I was wondering if there was a way for the hat switch to emulate 2 more analog axes via resistors. I know when wiring a potentiometer to a analog input on the Arduino, you would normally, for example, wire up ground to pin 1, 5V to pin 3, and pin 2 to one of the analog inputs on the Arduino. This way, when turned CCW, the resistance increases which lowers the voltage sensed by the Arduino, and when turned CW, the resistance decreases which increases the voltage sensed by the Arduino on the analog pin, and the Arduino will translate this sensed voltage to a value between 0-1024. So it sould be simple to just wire up some resistor(s) to emulate this function; push left on the hat switch, the resistance should be high, push right, resistance sould be low, and when the hat switch is centered (neither button is pressed), the resistance sould be inbetween whatever values I used for the left and right buttons. Simple enough I thought, but I can't think of any way to wire this up so that one switch would be a higher resistance then when the hat is "centered" (neither button being pressed).Example: If I think in terms of a 10k potentiometer, I would use a 5k resistor wired from 5V to the A0 analog input to represent the hat switch being centered. For the right switch, I would just wire the 5V to one side of the switch, the other side of the switch to the A0 input, so that when the right button on the hat switch is pressed, it'll be a direct path for 5V to A0 input, which would represent near zero resistance on that analog axis. But for the left switch, I can't think of any way to wire it to double the 5k resistance to 10K. I've even tried to play around with diodes in the mix, as I've read about using diodes as switches in my searches the last 2 days trying to figure something out, but I'm still unable to come up with anything that'll work.Is it impossible with just a switch, resistor(s), and diode(s) to build such a circuit that would increase the reisistance?

author: ataradov
author_rank: Super Contributor
timestamp: February 28, 2022, 04:59:46 am
content: I'm not sure I fully understand your setup. Is the hat just two completely free buttons? Then just use 2 resistors in series and buttons across those resistors. When no button is pressed you will read 1/2 of VDD (5V in this case or 1/2 of ADC full scale). When one button is pressed it will short one of the resistors to ground, resulting in a low reading. And when the other one is pressed, it will short the center tap to the VDD, resulting in a full scale reading.You can get a bit fancy and put a series resistor from the middle tap, so that neither ground or 5V gets directly connected to the MCU pin. The values are still going to be well distinguishable.You can apply similar idea to other buttons too. You can easily get away with 4-5 buttons on the same ADC pin and be able to read any combination of them.

author: Mpegger
author_rank: Contributor
timestamp: February 28, 2022, 05:51:19 am
content: Yes, the "hat" control on this joystick is just 4 switches, 1 switch for each direction, much like a arcade joystick. In my examples I was giving, I was just refering to 1 "axis" of the hat switch, so only reffered to 2 switches, as I planned on wiring the "axis" to 1 analog input on the Arduino. So Left + Right switches would be wired to A2, and Up + Down switches would be wired to A3 in my setup.The original electronics of this joystick used some kind of matrix encoding, as there are a bunch of diodes I salvaged from the boards, connected between various buttons on the boards. Even looking at the traces (all the boards in the controller are single sided), I can't wrap my head around how they used so few wires with all these buttons.Took me a couple of minutes, but I think I understand your example. I'll try it out tommorrow (today) morning and see if I can wire it up correctly on a breadboard.

author: ataradov
author_rank: Super Contributor
timestamp: February 28, 2022, 06:00:36 am
content: You can wire all 4 switches to one ADC input. Read about R-2R ladders. For 4 independent buttons you will get at most 16 different voltage levels, which are easy to distinguish. Of course your 4 buttons are not fully independent, so it is even easier.Here is a random article that describes this sort of arrangement -http://www.ignorantofthings.com/2018/07/the-perfect-multi-button-input-resistor.html

author: Ian.M
author_rank: Super Contributor
timestamp: February 28, 2022, 06:19:18 am
content: The existing board used a key matrix (hence the diodes) and you should do the same, as then the whole matrix including the hat switch will only take nine digital pins.Connect all your switches in a matrix with a Schottky diode in series with each so you don't get the fourth corner phantom button press problem if three buttons at the corners of a square in the matrix are down at the same time. The hat switch probably has five N.O. contact pairs, one each for each major direction + one for 'click', with a single common pin for one side of all the contact pairs. If you use a 5x4 matrix, it will be one complete row on its own. Set the five column pins as inputs with pullup, and scan the matrix by taking a single row pin low at a time. As you only have 12 other buttons there will be three spare spaces in the matrix where you could add extra switches. If you are using active low scanning, the diodes *MUST* have their cathodes towards the scanning (row) lines.https://learn.sparkfun.com/tutorials/button-pad-hookup-guide/allWhat you now do with the two spare analog pins on your Arduino and three digital pins is up to you. . .OTOH the controller already had a PCB in it to run it, and assuming that's still working or is fixable, it may be a lot simpler to use it as-is with the Arduino acting as a Playstation => USB HID controller protocol converter. The's even an Arduino library for Playstation 1 & 2 controllers that will do all the 'heavy lifting' for you:https://github.com/SukkoPera/PsxNewLib