Push-Up Counter Bluetooth® Application Using EMG Signals

 Objective

The importance of surface electromyography (EMG) in sports and occupational medicine as well as in ergonomic studies has risen over the past years. A myoelectric signal, also called a motor action potential, is an electrical impulse that produces contraction of muscle fibers in the body. These signals are sent to muscles that control voluntary movements. Electromyography signals help us measure the localized action potential of specific muscle movements and this helps to efficiently measure the strain at various parts of the body quickly and accurately. This project counts EMG pulses from a muscle flex and send this data over Bluetooth® to a Push-up Counter App on an Android™ device.

The project uses a Curiosity HPC Development Board with a PIC16F18875 microcontroller. The board is connected to the EMG sensor which reads the raw EMG data from muscle flexes and processes the signals to achieve a smoother signal suited for embedded control. We then count the number of muscle flexes and send the count, via UART protocol, to a Bluetooth RN4020 module. The Bluetooth module transmits this data wirelessly to another Bluetooth-enabled device, which in this case is an Android phone running an application called the Microchip Low Energy Data Profile (MLDP) Terminal.

This project is built on top of the EMG Signal Processing Module, created in the "EMG Signal Processing for Embedded Applications" page. The module provides the functionality required for processing the raw EMG signals output by the analog pin of the EMG sensor module. In addition to the development board and PIC16F18875 microcontroller, the project uses an EMG click board™, a three-connector sensor cable, electrode pads, a BLE2 click board™, and an Android device.

 Materials

Hardware Tools

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® X
Integrated Development Environment
MPLAB® XC8
C Compiler
swtool-28px.png
MPLAB® Code Configurator
Dynamic Code Generation

 Connection Diagram

Picture1a.png

HARDWARE SETUP

Picture2.png

HARDWARE CONNECTIONS

  • Connect the USB power to the board to turn it on.
  • Connect the 3.5 mm phone jack to the EMG sensor module and connect the electrodes to the cable.
  • Insert the EMG click module on the mikroBUS™ Slot 2 of the Curiosity board (refer to the "Connection Diagram").
  • Insert the BLE2 click module on the mikroBUS Slot 1 of the Curiosity board.
  • Launch the MLDP app on your Bluetooth-enabled Android device.

 Procedure

1

Installation and Setup

You need to have MPLAB® X IDE installed on your computer along with the MPLAB XC8 Compiler and the MPLAB Code Configurator (MCC). If you haven't used these tools before, the setup is described in more detail here: "Installation and Setup".

2

Load EMG Signal Processing Module

Since this project is built on top of the EMG Signal Processing Module, it is required to be loaded first. You can either download the Exercise Project Files or build your project from scratch with the help of the instructions given under the "Procedure" section of the "EMG Signal Processing Module for Embedded Applications" page.

3

Launch MPLAB Code Configurator

Open MCC under the Tools > Embedded menu of MPLAB X IDE as shown in the image below.

Picture3.png

4

Setup Project Resources

On the MCC screen, go to 'Project Resources'. Here, you can see the modules that have already been added from the EMG Processing module, which include:

  • System Module
  • Interrupt Module
  • Pin Module
  • ADCC
  • TMR6
  • EUSART
Picture4.png

The Bluetooth module we will add to our project in the next step comes with a UART module. Hence, we can remove the current EUSART module from the project. In the 'Project Resources' tab, click on the X next to the EUSART module to remove it.

Bluetooth Module Setup

Now, the Bluetooth module needs to be added to the project. On the MCC screen, you will see a list of peripherals under 'Device Resources'. Scroll to find Mikro-E Clicks > Wireless Connectivity > BLE2. Double-click on the module to add it to the project.

Picture5.png

After adding the BLE2, an EUSART peripheral and the UART Foundation Services Library are added to the project resources. The 'Project Resources' tab will look like this:

Picture6.png

Select the BLE2 from the 'Project Resources' tab. On the BLE2 'Information' tab, you can see a brief description of the BLE2 click. On the 'Configuration' tab, check the 'Generate Example' option. On the 'Advanced Settings' tab, check the 'UART' option on top and select 'EUSART' from the drop-down menu.

For more information on the operations of the Bluetooth module, refer to the RN4020 Overview.

BLE2Settings.png

UART Module Setup

The System Module, TMR6 module, and ADCC module do not need any changes. In the EUSART module, you will observe that the 'Baud Rate' has been set to '115200' and it cannot be changed. This is because the Bluetooth module uses this EUSART resource and has a fixed baud rate. Click on the 'Enable Continuous Receive' checkbox. The EUSART window will look like this:

Picture7.png

5

Configure Pin Manager

To assign pins on the microcontroller, open the 'Pin Manager' tab at the bottom of the screen. To select a pin, click on the corresponding blue open lock, turning it into a green locked symbol. Ensure the pins look as shown in the picture below:

Picture8.png

You will see that some pins have already been configured from adding the module in Step 2. The Bluetooth pins are to be configured next. Ensure that all other pin configurations are as shown in the picture above.

  • Choose the PDIP40 package from the drop-down menu.
  • Module ADCC: Click on Analog inputs (ANx) for (Port A – Pin 0) and (Port A – Pin 2).
  • Module BLE2: Click on (Port C – Pin 2) for Cmd_Mldp output.
  • Module BLE2: Click on (Port A – Pin 1) for Conn input.
  • Module BLE2: Click on (Port D – Pin 0) for Wake output.
  • Module EUSART: Similarly, click on (Port C – Pin 7) for RX and (Port C – Pin 6) for TX.
  • Pin Module: Click on (Port C – Pin 5) for GPIO Input and (Port A – Pin 7) for GPIO Output. Make sure to unselect (Port B – Pin 4) to make it blue and unlocked.
  • RESET: Click on (Port E – Pin 3) for MCLR (or leave it if it already shows green and locked).
  • TMR6: Ensure (Port B – Pin 7) is green for T6IN input.

6

Customize Names for Pins

Pin Module Setup

For easier understanding and programming, we can have custom names for the pins on the microcontroller that we intend to observe or control. Click on the 'Pin Module' in the 'Project Resources' tab on the left while MCC is open. Rename the pins according to the following diagram. To rename a pin, click on the 'Custom Name' for the pin and type in the new name. Uncheck the Analog box for GPIO.

Picture9.png

Now, you can see the names changed on the Pin Manager: Package View window as well.

Picture10.png

7

Generate Code

Click on the Generate button to have MCC create the software libraries for this project.

Picture11.png

The project will now have generated Header Files, Source Files, and a main.c file.

Picture12.png

8

Add Functionality

Picture13.png

The main.c file needs to be edited to include sending Bluetooth commands from the microcontroller to another Bluetooth-enabled device. Double-click on the main.c file to open it up in the editor window.

a

In the 'Included Files' section, the Bluetooth example header generated from MCC needs to be added. To do this, add the following line of code:

b

In the 'Global Variable Declaration' section, add the following line of code to declare a variable to count the number of push-ups:

Delete the flags sent_1 and sent_0 since we do not require them for monitoring the UART anymore.

c

After initializing the custom Timer Handler, we need to initialize the Bluetooth click. Add the following code in the main function of main.c :

You can set the name of your Bluetooth device here. The push-up counter is named “GymBuddy”.

d

In the 'Local Variable Declaration' section, we need a character array for the message sent over Bluetooth and a flag to indicate a muscle flex. You may also delete the variable mode since push-ups do not need any modes and are counted only for each muscle flex. Add the following code to the section:

e

In the while loop, the if loop for mode change can be deleted to only have an if loop that reads a switch press and starts the process. The code now looks like this:

f

After the result variable is calculated, functionality must be added to send commands to the slave microcontroller, as shown in the snippet below:

9

Programming the Device

Make sure your project has the programming tool selected and that your development board is connected to the power. Click on the Make and Program Device icon. This builds the project and launches the programmer. In the Output window, you should see a series of messages and if successful, it will end with a Programming/Verify Complete message.

10

Closing your Project

You can now close the project in MPLAB X IDE. The project is saved automatically when it is built, but if any changes to files or configuration were made, MPLAB X IDE will ask you to save them before the project is closed. The project can be closed under File > Close Project.

11

Installing MLPD Terminal Application

Download the attached APK file to your Android device and install it. This application is designed for communication with an RN4020 Bluetooth module. The app icon looks like the image below:

Picture18.png

 Results

  • When inserted on a powered-on Curiosity board, the BLE2 click lights up a blue LED for ‘WAKE’, indicating that the Bluetooth module is ready and awaiting a connection.
  • Open the MLDP app on your Android device. It should scan for all nearby devices.
  • Look for ‘GymBuddy’ and click on it to connect to our Bluetooth module. The connection should have been established now, and a green LED goes on for ‘CONN’ on the BLE2.
Picture19.png
  • You can now see the terminal screen with outgoing and incoming displays.
Picture20.png
  • Prepare the skin where you would like to attach the electrodes by wiping it clean and letting the area dry.
    • Your forearm is recommended for best results, although any muscle group would give you EMG signals.
  • Place the red electrode at the center of the muscle group and the blue electrode adjacent to it at the end of the muscle group.
  • Place the black electrode (reference electrode) where a bone is prominent (such as the side of your wrist).
  • Press the Start button (Switch2 – RC5) on Curiosity Board 1. The LED should be on.
  • Now, flex your muscle to see the Push-up Counter count the current number of push-ups on the MLDP terminal screen.
Picture21.png
  • Disconnect the device and the Bluetooth module on the MLDP app at the top right corner when done.
© 2018 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.