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.

  • As of now I see in the schematics two pins used for two switches. That is two independent signals connected to two independent inputs. What's the benefit over a traditional approach in connecting? – Ghanima May 27 '16 at 16:40
  • @Ghanima I'm not using a fixed "ground" or "input voltage" pin. (Thinking about it, that might be a bad practice in and of itself.) – BaSzAt May 27 '16 at 16:48
  • I still don't get it. Your schematics shows the use of two GPIO pins to read to pins in a complicate way. Two pins could be used to read two independent signals without all the fuzz. – Ghanima May 27 '16 at 17:07
  • @Ghanima I'm trying to find out what simple solution you are referring to. The most minimalistic approach I could find on raspberrypi.org takes 3 pins. I don't see how one could beat that without using diodes. (I'm fairly new to electronics, honestly.) – BaSzAt May 27 '16 at 17:32
  • 1
    If you want an example of a protocol that uses the same pin for reading and writing (sort of), take a look at worksheet #4 on the CamJam #2 kit. This is deals with reading an LDR by timing the charging of a capacitor. It does so by constantly flipping a GPIO to OUT (to discharge the capacitor), then IN (waiting for the GPIO to rise because the capacitor is "full"). And it works quite nicely - we use the same technique in GPIO Zero :) – Dave Jones May 30 '16 at 22:09
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:

  • That keyboard matrix really is a great idea. Though in my case, it would still need at least 5 pins. Maybe I should just do as you say and buy an extension board... – BaSzAt May 27 '16 at 19:27
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.

  • Thanks for the warning about the pull-downs. In your second line, did you mean that it is safe to toggle the GPIO at a 1 MHz frequency? Is there some source that explicitly states that? – BaSzAt May 27 '16 at 19:19
  • @BaSzAt I don't think I've seen a source, it's not the sort of information which is shown in datasheets, apart from indirectly by rise and fall times I suppose. However people regularly use SPI (provided by the GPIO) to talk to displays at 30-40 MHz. I've seen posts showing photos of 4 nanonsecond pulses (I believe there was a little under and over shoot). – joan May 27 '16 at 19:52
-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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.