Controlling Video Games Using Muscle Signals (EMG)

by Upside Down Labs in Circuits > Sensors

91 Views, 1 Favorites, 0 Comments

Controlling Video Games Using Muscle Signals (EMG)

EMG 2-ch game setup.gif

In this tutorial, you'll learn to control video games in your laptop with 2-channel muscle signals (EMG) detected from your arm. The code maps 3 gestures into specific keystrokes to control the game like bending your hand left triggers the "left" keystroke, bending it right triggers "right," and spreading or flexing your fingers triggers "up." These gestures provide intuitive control for engaging gameplay.

But before moving forward, let's understand a brief about Electromyography (EMG):

What is Electromyography (EMG)?

Electromyography is a technique that measures muscle response or electrical activity in response to a nerve’s stimulation of the muscle. We can use this electrical activity to detect neuromuscular abnormalities or create solutions for some crazy real-world problems like making artificial limb for amputees.

Supplies

DIY Neuroscience Kit - Pro (Front).jpg
DIY Neuroscience Kit - Pro (Contents).jpg

DIY Neuroscience Kit Pro (Upside Down Labs Store | Amazon India | Robu India | Tindie | DigiKey)

The kit includes:

  1. BioAmp EXG Pill (Assembled)
  2. Arduino Uno with USB Cable
  3. BioAmp Cable (100cm)
  4. 3 x Jumper Cables
  5. NuPrep Skin Preparation Gel (25g)
  6. Wet Wipes x 10
  7. Repositionable Gel Electrodes x 3
  8. Boxy Gel Electrodes x 100
  9. Gel Electrodes (4 variants, 6pcs each)
  10. Muscle BioAmp Band (EMG Band)
  11. Brain BioAmp Band (EEG Band)
  12. Heart BioAmp Band (ECG)
  13. Electrode Gel (30ml)
  14. Servo Claw
  15. Muscle BioAmp Shield Kit
  16. Muscle BioAmp Shield v0.3 (Assembled)
  17. BioAmp Cable (100cm)
  18. Muscle BioAmp Band (EMG Band)
  19. BioAmp AUX Cable
  20. Gel Electrodes (4 variants, 6pcs each)
  21. STEMMA Cables x 6
  22. 9V Snap Cable

SOFTWARE:

Arduino IDE v1.8.X (Legacy IDE)

Visual Studio Code

Python

About DIY Neuroscience Kit Pro:

It is like your Neuroscience Lab in a Box which lets you explore the world of neuroscience by recording various biopotential signals of your body (EEG, EMG, ECG, EOG) and making amazing HCI and BCI projects.

Stack Muscle BioAmp Shield on Arduino Uno

stacking.gif

Stack the Muscle BioAmp Shield on top of Arduino Uno properly.

Configure BioAmp EXG Pill for EMG/ECG

assembly-step2.png

BioAmp EXG Pill is by default configured for recording EEG or EOG but if you want to record good quality ECG or EMG, then it is recommended to configure it by making a solder joint as shown in the image.

Note: Even without making the solder joint the BioAmp EXG Pill is capable of recording ECG or EMG but the signals would be more accurate if you configure it.

Connecting BioAmp EXG Pill

shield-pill connection.gif

Connect the BioAmp EXG Pill to the A2 port of Muscle BioAmp Shield via 3-pin STEMMA cable which has JST PH 2.0mm connector on one end and 3 female jumpers on the other end.

Connecting Electrode Cables

connecting-bioamp-cables.gif

Connect one BioAmp cable to BioAmp EXG Pill and another BioAmp cable to Muscle BioAmp Shield by inserting the cable ends into the respective JST PH connectors as shown above.

Skin Preparation

skin-prep.gif

We’ll create a 2-channel EMG acquisition system and to do so, we’ll be using both the sensors to record EMG signals from your arms in a way that Channel 1 is recorded from finger extensor muscle and Channel 2 from brachioradialis muscle. Thus, prepare the skin accordingly.

Apply Nuprep Skin Preparation Gel on the skin surface where electrodes would be placed to remove dead skin cells and clean the dirt from skin. After rubbing the skin surface thoroughly, clean it with an alcohol wipe or a wet wipe.

Electrode Placements

electrode-placement-ch1.gif
electrode-placement-ch2.gif

To use the gel electrodes, follows the steps given below:

  1. Snap the BioAmp Cable connected to BioAmp EXG Pill to gel electrodes.
  2. Peel the plastic backing from electrodes.
  3. Place the IN+ and IN- cables on the brachioradialis muscle of your arm and REF (reference) at the bony part of your elbow.
  4. Now snap the BioAmp Cable connected to Muscle BioAmp Shield to gel electrodes.
  5. Peel the plastic backing from electrodes.
  6. Place the IN+ and IN- cables on the finger extensor muscle of your arm and REF (reference) at the bony part of your elbow.

Uploading the Code

Connect Arduino Uno to your laptop using the USB cable (Type A to Type B). Download the GitHub repository given below:

Muscle BioAmp Arduino Firmware: upsidedownlabs/Muscle-BioAmp-Arduino-Firmware

Go to the folder 12_, open the arduino sketch 12_in Arduino IDE.

Go to tools from the menu bar, select board option then select Arduino UNO. In the same menu, select the COM port on which your Arduino Uno is connected. To find out the right COM port, disconnect your Arduino UNO board and reopen the menu. The entry that disappears should be the right COM port. Now upload the code.

IMPORTANT: Make sure your laptop is not connected to a charger and sit 5m away from any AC appliances for best signal acquisition.

Testing the Connections

Go to the menu bar, click on Tools, and select Serial Monitor, or simply click the icon in the top-right corner. At this point, the output values should display 0.

Here’s what each output value represents:

  1. 0: No significant activity, no keystroke is triggered.
  2. 1: Bend left gesture, triggering the "left" keystroke.
  3. 2: Bend right gesture, triggering the "right" keystroke.
  4. 3: Finger flex gesture, triggering the "up" keystroke.

Additionally, the 3 value can be triggered along with 1 or 2 by flexing your fingers while performing either the bend left or bend right gesture.

Running Python Script

Open Visual Studio Code, click on File < Open folder to open the folder 12_

Open the terminal, and ensure that the path is configured to the location of the requirement.txt file.

To install all the modules that are required to run the Python script, write the given command in the terminal:

pip install -r requirements.txt


Run the Python script EMG_Scroll.py by writing the given command in the terminal:

python EMG_2ChGameController.py


Note: The python script will automatically detect the COM port and successfully run the command in the terminal but in case of any error related to COM port then you can manually change the COM port in the python script on line 14 as per the COM port you selected in Arduino IDE.

Playing Video Games

In the terminal, you’ll see the Move Now prompt. Bending your hand left or right will trigger the left or right keystrokes respectively. Similarly, flexing or spreading your fingers will trigger the up keystroke, which can also be activated while performing the left or right gesture.

Note: What’s happening in the background? Whenever an EMG signal is detected, it acts as a trigger to emulate LEFT, RGHT or UP key on the keyboard.

Configuration of Keystrokes

Follow the steps below to change the keystrokes as per your requirements and create amazing applications using this project.

  1. Identify the key mappings by locating the lines in the code where the keys are pressed, e.g., keyboard.press ('left'), keyboard.press ('right'), and keyboard.press ('up') as well as where the respective keys are released. These lines determine the key actions triggered by gestures.
  2. Modify the key values by replacing 'left', 'right', or 'up' with the desired keystrokes. For example:
  3. Change 'left' to 'a' to press the A key instead.
  4. Change 'right' to 'd' to press the D key instead.
  5. Change 'up' to 'w' to press the W key instead.
  6. Save the changes

Conclusion

This was just a demonstration to show you how both the sensors (BioAmp EXG Pill & Muscle BioAmp Shield) can be used together to create a 2-channel EXG acquisition system and play games. In this project, we used BioAmp EXG Pill to record EMG signals, but it can also be used to record other biopotential signals as well like ECG, EOG, or EEG.

The possibilities are endless. What are you gonna make with DIY Neuroscience Kit - Pro?

Let us know your feedback in the comments and feel free to ask any questions.

You can also mail us at support@upsidedownlabs.tech for any kind of support while you are making this project.