Example Code for Arduino-Calibration and Print Waveform
Last revision 2026/01/15
This article guides you through using an Arduino to calibrate and print waveforms with EMG sensors, providing detailed instructions on hardware preparation, software setup, and calibration steps for effective EMG signal processing.
Hardware Preparation
- SEN0240 Gravity: Anlaog EMG Sensor x 1
- DFR0216-2 DFRduino UNO R3 with IO Expansion Shield and USB Cable A-B x1
- FIT0897 Gravity: Analog Sensor Cable for Arduino(Included with SEN0240) x1
Software Preparation
Wiring Diagram

Sample Code
/*
* Copyright 2017, OYMotion Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*/
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "EMGFilters.h"
#define TIMING_DEBUG 1
#define SensorInputPin A0 // input pin number
EMGFilters myFilter;
// discrete filters must works with fixed sample frequence
// our emg filter only support "SAMPLE_FREQ_500HZ" or "SAMPLE_FREQ_1000HZ"
// other sampleRate inputs will bypass all the EMG_FILTER
int sampleRate = SAMPLE_FREQ_1000HZ;
// For countries where power transmission is at 50 Hz
// For countries where power transmission is at 60 Hz, need to change to
// "NOTCH_FREQ_60HZ"
// our emg filter only support 50Hz and 60Hz input
// other inputs will bypass all the EMG_FILTER
int humFreq = NOTCH_FREQ_50HZ;
// Calibration:
// put on the sensors, and release your muscles;
// wait a few seconds, and select the max value as the threshold;
// any value under threshold will be set to zero
static int Threshold = 0;
unsigned long timeStamp;
unsigned long timeBudget;
void setup() {
/* add setup code here */
myFilter.init(sampleRate, humFreq, true, true, true);
// open serial
Serial.begin(115200);
// setup for time cost measure
// using micros()
timeBudget = 1e6 / sampleRate;
// micros will overflow and auto return to zero every 70 minutes
}
void loop() {
/* add main program code here */
// In order to make sure the ADC sample frequence on arduino,
// the time cost should be measured each loop
/*------------start here-------------------*/
timeStamp = micros();
int Value = analogRead(SensorInputPin);
// filter processing
int DataAfterFilter = myFilter.update(Value);
int envlope = sq(DataAfterFilter);
// any value under threshold will be set to zero
envlope = (envlope > Threshold) ? envlope : 0;
timeStamp = micros() - timeStamp;
if (TIMING_DEBUG) {
// Serial.print("Read Data: "); Serial.println(Value);
// Serial.print("Filtered Data: ");Serial.println(DataAfterFilter);
Serial.print("Squared Data: ");
Serial.println(envlope);
// Serial.print("Filters cost time: "); Serial.println(timeStamp);
// the filter cost average around 520 us
}
/*------------end here---------------------*/
// if less than timeBudget, then you still have (timeBudget - timeStamp) to
// do your work
delayMicroseconds(500);
// if more than timeBudget, the sample rate need to reduce to
// SAMPLE_FREQ_500HZ
}
Caution
-
The supply voltage range is 3.3~5.5V; The supply current should not be less than 20mA; The ripple current and disturbance current should be as low as possible. Stabilized DC voltage is recommended.
-
The effective spectrum range is 20Hz~500Hz, and the ADC converter which has higher than 8-bit resolution and 1 KHz frequency are recommended to take samples and digitized to keep original information.
-
Placing the metal dry electrode should consistent with the direction of muscle.
-
The product is not a professional medical device and cannot diagnose and cure disease as an assistant device.
Calibration Step
⚠️Calibration is recommended for every time when you use it, for the EMG signal is different in different position, even on the same person.
-
Modify the variable
Thresholdin the sample code to 0, namely:static int Threshold = 0; -
Upload the sample code to Arduino control board and open the serial monitor of Arduino IDE to check the printed value.
-
Relax the arm muscle and check the printed value of serial port. Calm down and relax for a while to check and record the maximum value printed by the serial monitor. If the value is too large, such as more than 1000, you can try fine-tune position of the dry electrode.

-
Modify the variable
Thresholdin the sample code to the recorded maximum value, and reload the sample code to Arduino control board. -
Open the serial plotter of arduino IDE and the myoelectricity waveform could be seen.
Result
After calibration, the waveform is generally a line at 0 when the muscle is relaxed, as shown below. Occasional peak waveforms are normal.

Obviously myoelectricity waveform could been seen in the process of making a fist, shown as below.

Was this article helpful?
