Gamepad

The questions below are due on Friday September 13, 2024; 05:00:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Safety

If you haven't done the this link and take the EHS00509w Electrical Safety Awareness course, you must do it before starting this lab. To do this, search for "509" under the Course Catalog Page, which should find the "Electrical Safety Awareness" course.

1) Reminder: Soldering 101

To start, take a look back at prelab section 2 which presents an introduction to soldering to remind yourself of the general process and review the safety information there.

There is also an unedited video of the whole process of building today's board here. Throughout the lab writeup, we'll provide links to the relevant parts of the video.

2) Safety First

Grab safety goggles and wear them whenever you or someone near you is soldering or clipping wires (i.e., the whole time we're in lab today).

If you want a vent fan, go ahead and set one of those up as well!

If you have long hair, it's probably a good idea to tie it up as well if you can (or just make sure to keep it away from the hot part of the iron).

3) Brief Overview

Today, we'll be making a pretty cool project, I think: a USB gamepad that can be used as a video game controller (Windows and Linux computers will recognize it as an XBox 360 controller):

This one was built on a green PCB, but we have a bunch of other colors available, too, so go ahead and pick whatever color speaks to you. They're all functionally the same; they just look different.

We'll be adding the components to the board, and the PCB connects them to each other. Before we move on to actually building the board, let's make sure we understand how things are connected up.

3.1) Buttons

The first component we'll look at is a button (momentary switch) like the ones we saw in last week's lab. In a circuit schematic, we'll often draw a momentary switch like so:

While the button is pressed down, its terminals are shorted together; but when we let go of the button, its terminals are again disconnected.

Each button is connected like this, with the 3.3 Volts being provided by the Teensy microcontroller:

The red "+" and "-" tags there represent a place where the Teensy is set up to measure voltages. By measuring those voltages, we can detect when a button is pressed/released.

When the switch is open, what is the voltage across the button (between the two labeled locations), in Volts?

When the switch is closed, what is the voltage across the button (between the two labeled locations), in Volts?

In the controller, the Teensy will measure these voltages to detect whether a given button is pressed or not.

4) Adding the Buttons to the PCB

Now that we understand how the buttons should work, let's go ahead and solder them to the PCB. We'll also go ahead and solder up the Teensy, which we can then use to test that the buttons are working correctly.

Now that we've seen how everything is connected up, let's move ahead with building the circuit.

4.1) Setting Up

Start by turning on the soldering iron (the switch on the right-hand side of the base) and adjusting its temperature to around 850. Hold the "Enter" button until the left-most number starts flashing. Then use the up-arrow button to adjust the number, and click enter to move on to the next digit. When all three digits are set, the display will change to show the iron's current temperature.

Also if the sponge on your iron isn't damp, take it to the sink in the center of the room to dampen it a bit. It shouldn't be dripping wet, but just a little damp. You can use that to clean excess solder off the tip of the iron.

Then grab yourself some solder from the tables at the front of the room (don't take the whole spool, just grab a small piece, you can always go back for more if you need). You don't need to use wire cutters or anything like that; you can just use your hands to tear off a length of solder.

4.2) Shoulder Buttons

The first things we'll add to the board are the four "shoulder" buttons on the top of the board (chosen to go first because they are physically the shortest components).

We'll actually start with the two that sit on the back of the board. Stick them into the board like so, putting the five pins of each through the five holes they fit into. Make sure the buttons themselves are on the side of the PCB with no text/logos on it (the pins should stick through the front side).

Now, flip the board over and let gravity hold things in place while you solder the pins.

Creating a solder joint involves a few steps:

  • Heat both the pin (the piece of metal sticking through) and the pad (the little circular metal contact on the PCB) for 2-3 seconds using the iron, making sure it's in contact with both.
  • Feed a small amount of solder onto the joint. It should "flow" smoothly onto the pad and make a shape like a little cone.
  • Continue heating for another second or so, then remove the iron.
  • Let the joint cool before moving things around (you should see it change color slightly when it's cooled enough to be mechanically stable).

Here is an example of a solder joint:

And here are some examples of bad solder joints (what to look out for as you're working):

If you do make a mistake, that's OK; we can usually fix it with a little bit of effort. This page from Adafruit discusses how to fix them, or feel free to ask us for help!

Note

If you want us to demonstrate and/or to take a look at your work as you're going, just let us know! We're happy to help.

Adding the back shoulder buttons is shown at this point in the demo video.

4.3) More Shoulder Buttons

Then do the other shoulder buttons (the ones that sit on the front of the board). Stick the pins through so they're sticking out the back, then flip the board over and solder the pins in.

Adding the front shoulder buttons is shown at this point in the demo video.

4.4) Other Buttons

Next, we'll add the other push-buttons, working in small groups. Start with the four on the right, then do the two in the middle, then do the four on the left.

For each little group, feed the buttons' leads into the associated holes on the board, flip it over, and solder one pin in place. Flipping the board with all of the buttons in can be a little bit hard, but you can use a piece of cardboard like we did in the demo video to help keep things in place. We have some cardboard on the tables at the front of the room if you want some. Just make sure you move it away from the iron when you start soldering.

Adding these buttons is shown at this point in the demo video.

You'll want to provide some downward force on the PCB while you're soldering so that the buttons sit flush with the surface of the board. Double-check afterwards. If the buttons aren't flush with the PCB, they can easily break off.

Once they're all sitting flush against the PCB, you can go in and solder the other pin on each button in place.

 
crying_cat emoji OH NO Button not flush with the PCB surface! crying_cat emoji smile_cat emoji Much better! smile_cat emoji

If yours aren't flush, that's OK; you can often fix it by melting the solder again and pushing the leads farther through the hole while it's melted.

4.5) Double-check!

Double-check all of your connections, and double-check that your buttons are sitting flat against the PCB.

4.6) Teensy Socket

Next, we'll add the a place for our Teensy to live. Do not solder the Teensy directly to the board. Instead, we'll make a socket for it by connecting up some headers like so (the black part should be on the side of the board with the words printed on it):

Are you going to solder the Teensy directly to the board?

Plug those in to the board and then plug the Teensy into them (this will help to keep them aligned with the board).

Then:

  • Flip the board over again.
  • Solder the four corner pins.
  • Remove the Teensy and set it to the side for now.
  • Solder the rest of the pins.

Adding the Teensy socket is shown at this point in the demo video, though I did things in a slightly different order than what we're suggesting here.

4.7) Test It!

Don't Get Stuck Here!

Give these instructions a shot, but if you get stuck installing things, let us know and we're happy to help. Don't let yourself spend a bunch of time here.

If you haven't already done so, try working through the instructions for installing Arduino/Teensy/Xinput before proceeding.

Now that you can plug the Teensy in, we can use it to test the connections we've made so far. Before we do that, though, we're going to add a pin to the Teensy to make measuring things a little bit easier (by giving us an easy place to connect our alligator clips for ground). Grab a single header pin, plug it into the breadboard, and solder it onto the middle hole in the back of the Teensy, like so:

Plug the Teensy into the sockets on your PCB (with its USB connector toward the top of the gamepad), then connect the Teensy to your computer via USB cable, which should provide power. Then we'll flash a small test program to it:

  • Download tester.ino, save it on your computer, and open it with Arduino/Teensyduino.
  • From the menu, under Tools\toBoard: ...,\toTeensyduino, select Teensy 4.0. This tells Arduino what kind of board we're programming.
  • Plug your Teensy into the computer via USB cable (we have extras if you need one) and double-tap the button within 0.5 seconds of plugging it in.
  • From the menu, under Tools\toUSB Type: ..., make sure Serial is selected.
  • Click the second button from the left, which looks like a rightward-pointing arrow (it should say "Upload" when you hover over it). This should compile things and send the result to the Teensy with no orange error messages.

If you get an error (usually on Mac) with something about "Teensy Loader", follow the instructions on this page to help fix it. Download that and open it; then there should be a little window that pops up with a picture of the Teensy in it. Once that's up, go back to Teensyduino and try flashing again.

With that done, let's use the scope to make some measurements to make sure things are changing as expected as we push the buttons. Connect the alligator clip of the scope to the vertical pin sticking out the back of the Teensy, and grab a short clip of wire in the jaws of the probe. You can then use this wire to make contact with the little holes labeled with the names of the various buttons (A, X, ...). When doing so, you should see the signal change as you press the corresponding button. Do these signals change as you predicted using theory above?

Test all of the buttons except LV, LH, LS, RV, RH, and RS (which come from the joysticks that we haven't added yet) and make sure they change as you expect when you push the buttons.

Checkoff 1:
Show your work so far to a staff member.

Once you've received your checkoff and are absolutely sure that things are working as expected, you can use wire cutters to trim off the excess leads on the buttons so that we don't have those long wires sticking out.

When clipping, use another finger to hold the lead so it doesn't go flying when you cut it; and WEAR GOGGLES WHEN DOING THIS; little metal clippies flying out and getting stuck in your eyes is not much fun. Clipping the leads happens at this point in the demo video.

Also unplug your Teensy from the socket on the board until we're ready to test again.

4.8) Joysticks

In the prelab, we learned about potentiometers: three-terminal devices that we can think of as containing two resistors that we can change by turning a knob or similar.

The joysticks in the controller each contain two potentiometers (one whose \alpha value changes from 0 to 1 as the stick moves left-to-right, and one for up-and-down). Go ahead and grab a joystick from the tables at the front of the room. They look like this, where the two little orange thingies on the sides are the potentiometers. Each pot has three terminals, which are connected like so:

Use your multimeter to measure: what is the value of R_P for these potentiometers, in Ohms?

In the controller, we'll use these joysticks as voltage dividers by dropping a voltage across the two internal resistors. The voltage source shown here will be provided by the Teensy, which provides a constant 3.3 Volts. Each pot within the joystick is connected like shown below, where the Teensy is set up to measure the voltage between "+" and "-" for each:

Solve for the voltage indicated above (in Volts). Enter your answer as a Python expression. Your expression can involve variables R_P and/or alpha, which represent the variables R_P and \alpha, respectively.

We'll set up the Teensy to measure this voltage for each direciton, which we can use to find the (x, y) position of the joystick.

4.9) Adding the Joysticks

So let's go ahead and add the joysticks to the board. We recommend doing them one at a time. For each, like all the other pieces, we'll feed the pins through the board so that the physical body of the joystick is on the side with the words and logos and whatnot:

BUT BE REALLY REALLY CAREFUL! It can be hard to get the pins to line up correctly, and we want to make sure we're not forcing anything (the joysticks themselves can break). A good place to start is to make sure all the pins are straight. And even then, it can take some time to get things to line up. When things are set up appropriately, you should be able to see all of the pins sticking out the back side of the board, like shown in the following image. There should be:

  • two sets of three pins, one for each potentiometer (six total)
  • one set of four small pins for the button
  • four large pins on the side (not electrically connected, just there for mechanical support)

for a total of 14 pins per joystick. All of these should be clearly visible before you start soldering, like so:

A good way to start is by holding the joystick flush with the board while you solder one of the pins; this should then keep it in place while you solder the rest.

Once you've got all the pins visible, go ahead and solder them. Be careful with these ones, since they are a little difficult and a little close together. Also, the physical mechanism of the joystick can make it a little bit tricky to keep things straight. Take your time and be careful, and generally don't attach the cover of the joystick while you're doing this (you can do that after the soldering is done).

Adding one joystick is shown at this point in the demo video and the other is here.

Once you have them in place, you can go ahead and put the caps on the joysticks.

5) Test

Now let's test that those are connected up properly. With the Teensy plugged in (and running the same code from when we tested the buttons), we can make some measurements.

Remember that the joysticks each have two potentiometers inside, and we've hooked them up as voltage dividers. As such, if we measure at LH and LV relative to ground (the vertical stick coming out of the Teensy), we should see changes as we move the left stick around (same for the right stick with RH and RV). To experiment, let's try connecting LH to be channel 1 and LV to be channel 2. Wiggle the stick around, and make sure you can explain what is going on.

Right now, the scope is displaying time on its horizontal axis, with each channel as a separate signal on the vertical axis. But that isn't particularly useful for visualizing this kind of device. So we're going to try a different kind of display: "X/Y" mode. Instead of separate plots of channel 1 and channel 2 versus time, this mode will cause the scope to make a single plot of channel 2 versus channel 1.

Set the scope to X/Y mode by first clicking "Acquire" (near the top middle of the panel of buttons). From the menu that pops up, set "Time Mode" (the top thing) to "XY". You should see the plot change.

Now, move the joystick around. Does the display on the scope change as you expect? It's OK if one or both of the axes are "backward" compared to what you might expect; we'll handle that correction in software on the Teensy (no need for you to do anything about it).

It turns out that this is precisely what goes on if you've ever used a real game controller. The analog sticks are connected as voltage dividers like this, and the voltages are read into a computer to determine the player's inputs. Yet another cool example of circuits serving as an interface to the physical world (in this case, enabling a neat kind of human-computer interaction).

For extra fun, under the "Display" menu, change "Persistance" to "Variable Persistance" to cause a trace of the dot to remain for a while after it is originally drawn. Can you use this to draw a little picture or something like that?

Repeat your experiment with the right joystick to make sure everything is working.

The joysticks also contain two additional buttons, which you can press by clicking the stick down into the board. Use the RS and LS test points to check that these are also working as expected.

Checkoff 2:
Demonstrate your results to a staff member.

6) Using the Controller

Before we finish up (which will make accessing these solder joints a bit harder), we're going to want to make sure everything is working. We'll do this by programming the Teensy and putting it through the paces a little bit.

6.1) Program the Teensy

The gamepad we've built is designed to look to your computer like an XBox 360 controller. Unfortunately, MacOS doesn't support XBox 360 controllers (even the real ones), so we've got two versions of the code, and which one you want depends on your operating system of choice (i.e., the one you'll plug this into to play games, if any; not necessarily the OS you're programming the Teensy from). Follow the instructions for the appropriate version for you. Either version can be flashed to the Teensy using any operating system, but they work slightly differently:

  • The "XInput" version works on Windows and GNU/Linux but not MacOS. This version makes the Teensy appear to your computer as an XBox 360 controller, so it should work without any additional configuration. This is the version yo want to use if possible, but because MacOS does not support XBox 360 controllers (even legit ones), it won't work there.

  • The "Joystick" version makes the Teensy appear to your computer as a generic joystick and should work on any operating system, but the mapping from buttons on the controller to the buttons to be used in a game won't be able to be determined automatically (and it may differ from game to game), so you might need to do some additional configuration within each game to get things to work.

Follow one of these sets of instructions depending on which version you want:

  • To install the XInput version:

    • Download gamepad_xinput.ino, save it on your computer, and open it with Arduino/Teensyduino.
    • From the menu, under Tools\toBoard: ..., select Teensy 4.0. This tells Arduino what kind of board we're programming.
    • Plug your Teensy into the computer via USB cable (we have extras if you need one) and double-tap the button within 0.5 seconds of plugging it in.
    • From the menu, under Tools\toUSB Type: ..., choose XInput.
    • Click the second button from the left, which looks like a rightward-pointing arrow (it should say "Upload" when you hover over it). This should compile things and send the result to the Teensy with no orange error messages.
  • To install the Joystick version:

    • Download gamepad_joystick.ino, save it on your computer, and open it with Arduino/Teensyduino.
    • From the menu, under Tools\toBoard: ...,\toTeensyduino, select Teensy 4.0. This tells Arduino what kind of board we're programming.
    • Plug your Teensy into the computer via USB cable (we have extras if you need one) and double-tap the button within 0.5 seconds of plugging it in.
    • From the menu, under Tools\toUSB Type: ..., choose Serial + Keyboard + Mouse + Joystick.
    • Click the second button from the left, which looks like a rightward-pointing arrow (it should say "Upload" when you hover over it). This should compile things and send the result to the Teensy with no orange error messages.

If you run into any errors, let us know, and we're happy to try to help.

6.2) Try It!

Now let's test and see whether everything is working. If you unplug and re-plug the USB cable connecting your Teensy to your computer and open up this page (use Chromium/Chrome for the best experience, or Firefox works too; but Safari won't work), you should see it respond when you move the joystick and/or push buttons. If not, let us know and we can help.

If you want a more realistic test, we also have two computers set up in the middle of the room with games loaded up and ready to try! And as we mentioned, the XInput version of the controller should work for roughly any game on a Windows or Linux machine, so feel free to use your own machine to test, too, if you want.

Checkoff 3:

Demonstrate your working game controller to a staff member.

When you're done with that:

  • Turn off your soldering iron
  • Click the "Default Settings" button on the scope
  • Under the "Wave Gen" menu in the scope, click "Settings" and then "Default Wave Gen"
  • Turn your scope off, too.

7) Finalizing the Gamepad

To finalize things, we'll remove the pin we added to the Teensy, and we'll also do away with the sharp edges on the back of the board and cover it with a piece of acrylic to make it a little less painful to hold.

7.1) Removing the Teensy Pin

Now that we're done with testing the Teensy, we can go ahead and remove the pin we added to the Teensy. Plug the pin into the breadboard, heat up the solder blob, and pull up on the Teensy while the solder is melted; the pin should slide right out:

7.2) Adding the Acrylic Backing

Use some double-sided tape (from the table near the printer in the middle of the room) to attach the acrylic to the back. The holes should line up with the solder joints. Grab yourself a little strip of tape and then attach pieces to the board.

You can stick the tape down like so (it doesn't have to be in exactly these spots):

then pull the backing off and stick down the acrylic to it:

Push down hard to make sure everything is attached.

Adding the acrylic backing is shown in this video.

8) Clean Up

Woo-hoo, we're all done! The gamepad (including the Teensy) is yours to keep. But if you borrowed a USB cable, please bring that back (we may need to ask you to acquire your own if you want to use this later on).

Before you leave, though, clean up by doing the following:

  • Turn off your iron (and vent fan if you had one).
  • Put the safety goggles back.