1

I've built a simple joystick for my RasPi 3 Model B. When making it, I tried to minimize the number of its GPIO pins, and ended up with a design that uses 3 pins.

However, I'm afraid I'm about to put lot of strain on my Pi here. For example, the buttons on my joystick look like this:

schematic

simulate this circuit – Schematic created using CircuitLab

(No, I didn't solder it on the headers, I just picked these two pins for the sake of argument.)

When reading the state of the buttons, the driver would first put GPIO20 to read mode, then put GPIO21 to write mode and write a 1. Then, digitalReading GPIO20 would tell us if the left button is pressed. For the right button, we can do the same thing in the other direction.

What I'm concerned about is that this program would toggle the mode of GPIO20 and 21 between Read and Write about a hundred times per second. I have never seen a protocol that uses the same pin for reading and writing, which makes me think it is bad practice, and possibly, at this frequency, even harmful to the CPU.

Is my joystick indeed dangerous? If I were to use it regularly, as a mouse, would it do a considerably greater damage to the Pi than, say, a normal USB mouse?

EDIT: As noted in the answers, this kind of wiring is an unreasonable effort to save a ground wire. I chose this approach mainly because it can handle n*(n-1) switches with n pins, by covering all the possible directions from one pin to another with a diode and a switch. My joystick has 6 switches (two buttons and a 4-direction stick), thus requiring 3 pins (as opposed to the 7 pins called for by the 'normal' approach). I didn't post the full schematic because I tried to keep it simple.

BaSzAt
  • 113
  • 4

3 Answers3

4

While the suggested schematics may (or may not) work it is indeed a lot of effort to save the ground wire (just as joan's answers points out). Don't think of GND as a third pin. It's not a GPIO pin, it's the common ground. It also does not "use up", i.e. you can use it multiple times over. Which is part of your linked picture at raspberrypi.org. Both switches share the "same" ground (as could ten switches or fifty).

If the number of available GPIO pins is indeed a problem best to look into established techniques to "multiply" them, but note that there's always a tradeoff. Ideas include multiplexing, shift registers, or port expanders, e.g. connected via one of the serial interfaces (I2C or SPI). Another approach is a switch matrix, see third link.

See:

Ghanima
  • 15,958
  • 17
  • 65
  • 125
2

Seems to be a lot of effort to save a ground wire.

The GPIO may be toggled millions of times a second.

Thinking about it the lines will be floating so you'll need to enable the internal pull-downs on each used GPIO.

joan
  • 71,852
  • 5
  • 76
  • 108
-1

I like your thinking, but it has been commonly done in keyboard matrixes. One diode per switch. One gpio for each column and one for each row. The gpio pins do not have to switch modes from input to output.

ADDITION after down vote: I found a better explanation here. http://www.openmusiclabs.com/learning/digital/input-matrix-scanning/

http://www.openmusiclabs.com/wp/wp-content/uploads/2011/10/switch_matrix_schem_sm.jpg

PaulF8080
  • 307
  • 1
  • 7