By using this website, you agree to the use of cookies. We use them to optimize functionality and convenience of the site for our clients.
Elemyo | 24 February 2020

Overview the Elemyo-library for Arduino.

In this article, we will look at examples of sketches on Arduino for processing signals from Elemyo sensors.
Library for working with Arduino: here.
Before you get started, we strongly recommend you to read the detailed instructions for the sensor or module that you are using. All actual datasheets you can find in our datasheet library.
The content of the review:
1
Terminal
Learn more
2
EMG_Simple_Read
Learn more
3
Multiconnection
Learn more
4
EMG_Moving_Average
Learn more
5
Controll_by_EMG
Learn more
6
Servo_Discrete_Control
Learn more
7
Servo_Motion_Control
Learn more

1
TERMINAL
This example is intended for Arduino firmware for visualization and signal processing in Python and MatLab. It is enough to compile this example and flash Arduino, then run the sketch in Python (download the example) or MatLab (download the example). The example is compatible with the entire range of sensors. In Fig. 1 shows the MYO to Arduino Uno connection diagram used in the example. Other sensors you can connect in the same way.
Сигнал с ЭМГ датчика. ЭМГ сигнал, регистрируемый при активности мышц предплечья. Форма сигнала при единичном мышечном сокращении
Fig. 1: Connection scheme of the sensor to the Arduino Uno.

2
EMG_SIMPLE_READ
This example implements the simplest function: reading sensor readings on analog input A0 and writing the signal to Serial. If a sensor with adjustable gain is used, the gain can be set using the MyoSensor.gain (value) command, where one of the following gain values is used as the argument "value": "x1", "x2", "x3", "x4", "X5", "x8", "x10", "x16", "x32". The connection diagram is similar to Fig. 1.

3
MULTICONNECTION
This example demonstrates working with an array of sensors. In the example, the simultaneous connection of 6 sensors is analyzed (more or fewer sensors are connected by analogy).
Sensors are connected according to Fig. 2.
Пример ЭМГ сигнала при плохом контакте датчика с кожей. Миограмма с помехой 50 Гц
Fig. 2: Connection scheme of 6 sensors to Arduino Uno.
Sensor gain control is done through SPI. If you plan to control the gain of each sensor individually, then the CS pins of each sensor must be connected to individual Arduino pins (it can be either PWM or digital). If you plan to set the same gain for all sensors, then the CS pins of each sensor can be connected to one common Arduino pin (it can be either PWM or digital). SCLK pins are connected to pin 13, SI pins to pin 11.
After flashing Arduino with this example, you can start the plotter via a serial connection to draw signals from each sensor.

4
EMG_Moving_Average
This example demonstrates one of the signal smoothing methods. As an anti-aliasing method, the method of exponential moving average of arbitrary order is taken. Information on the moving average method and the exponential moving average method of arbitrary order is available here.
Пример ЭМГ сигнала при плохом контакте датчика с кожей. Миограмма с помехой 50 Гц
Fig. 3: The picture shows an example of signal smoothing.
The essence of the method:
Let us have some function f (t) (signal) that changes its value over time (for example, temperature, pressure, EMG, etc.). We are faced with the task of highlighting in real time the main trends in the behavior of a function against the background of short-term fluctuations. To do this, we need to average the values of the function. One of the ways of such averaging is the method of exponential moving average of arbitrary order. The idea of the method is that each new averaged value is obtained based on the current and previous values of the function. The method is constructed as follows:

EMAtn = a* EMAtn-1 + (1 - a)* EMAt-1n,

where EMAtn - this is the value of the exponential moving average of the n-th order at the current time t. EMAt-1n this is the previous value of the exponential moving average of the n-th order. EMAtn-1 – this is the current value of the exponential moving average of the (n-1) th order. a - smoothing coefficient.

As an example, we consider how signal smoothing occurs by the 3rd order exponential moving average method:

First, we calculate the value of the exponential moving average of the first order.

EMAt = a*f(t) + (1 - a)*EMA(t-1), where EMAt – first order exponential moving average. f(t) – function value at the current time, EMA(t-1) – first-order exponential moving average calculated over the previous time period.

Then, the second order:

DMAt = a*EMAt + (1 - a)*DMA(t-1), где DMAt – second order exponential moving average.

Then, the third order:

TMAt = a*DMA t + (1 - a)*TMA(t-1) – third order exponential moving average.

Initial values of EMA0, DMA0, TMA0 can be set equal to f(0).

Method implementation:
If we take the EMG signal directly as a function, the smoothing result will be direct, since the EMG signal is symmetrical with respect to the reference voltage. If the EMG signal is rectified with respect to the reference voltage (Fig. 4), then the smoothing curve will pass between the peak values and the value of the reference voltage, while the amplitude of the smoothed signal will be ¼ of the signal amplitude (Fig. 5-7).
Пример ЭМГ сигнала при плохом контакте датчика с кожей. Миограмма с помехой 50 Гц
Fig. 4: The graph shows an example of signal smoothing.
The program works as follows: reads the readings from the analog input A0 Arduino, if the value is less than the value of the reference voltage, then the sensor reading is symmetrically "transferred" relative to the value of the reference voltage (Fig. 4). Next, the calculation of EMAtn. The program is implemented EMAt3, but if necessary, it can be replaced by any other order.

An example of the algorithm is shown in Fig. 5-7.
Пример ЭМГ сигнала при плохом контакте датчика с кожей. Миограмма с помехой 50 Гц
Fig. 5: Three short muscle contractions.
Пример ЭМГ сигнала при плохом контакте датчика с кожей. Миограмма с помехой 50 Гц
Fig. 6: Gradual muscle contraction followed by relaxation.
Пример ЭМГ сигнала при плохом контакте датчика с кожей. Миограмма с помехой 50 Гц
Fig. 7: Rapid muscle tension followed by relaxation.
You can adjust the "depth" of smoothing using the coefficient a. The closer a is to 1, the mean curve is less sensitive to signal changes.
If the recorded EMG signal turns out to be small in amplitude, then set the increasing gain using the function MyoSensor.gain (value), where one of the following gain values is used as the argument "value": "x1", "x2", "x4", "X5", "x8", "x10", "x16", "x32".

5
CONTROL_BY_EMG
This example demonstrates how to use the EMG signal to set two control commands. The program works as follows: if the muscle is tensioned for 1 second or less, the program will display the value "Type 1", if more than a second, then the value "Type 2". This program can be used as the easiest way to control a robotic arm, motor or just an LED. For example: if we stretch the arm for less than 1 s, then the robotic arm will make a fist-type grip, if for more than 1 s, then the robotic arm will make a pinch grip. In the case of the LED: when the hand voltage is short, the LED will blink 1 time, in case of continuous hand voltage, the LED will blink twice.

The program constantly monitors the magnitude of the amplitude of the recorded signal. As soon as the amplitude value exceeds a certain threshold thresholdStart, the program understands that the muscle is tense and starts counting the time (EMG_TimeStart). As soon as the amplitude of the EMG signal becomes less than the threshold value thresholdEnd, the program detects that the muscle is no longer strained and calculates the time interval EMG_Time, during which the EMG was recorded. Depending on the value of EMG_Time, the value "Type 1" or "Type 1" is displayed.

For the program to work correctly, it is important to set the thresholdStart and thresholdEnd parameters that are individual for each user. To do this, first run the "Terminal" example above and display the EMG signal. thresholdStart set as half the value of the maximum amplitude, thresholdEnd as the doubled value of the interference value in the absence of an EMG signal. If the recorded EMG signal turns out to be small in amplitude, it sets the increasing gain using the MyoSensor.gain (value) function, where one of the following gain values is used as the argument "value": "x1", "x2", "x4", "X5", "x8", "x10", "x16", "x32".
Пример ЭМГ сигнала при плохом контакте датчика с кожей. Миограмма с помехой 50 Гц

6
Servo_Discrete_Control
This sketch allows you to define single muscle contractions. Can be used for simple robotic arm control system. Link to the sketch is here.

7
Servo_Motion_Control
This sketch allows you to control the servo motor continuously - the angle of rotation of the motor shaft is proportional to the degree of muscle tension. Link to the sketch is here.
Thanks for attention!
We hope you find this review helpful. If you have any questions - feel free to contact us at info@elemyo.com. We will answer you!
Last update: 05 JANUARY / 2023
Copyright: Elemyo