Hi all,
I'm new to the forum and to arduino so go easy on me lol
I'll make this as clear as possible, so scope of my project is to move a servo motor using an EMG sensor. So that it rotates to 170 degrees when hand is closed and back to 0 when hand is open.
Here's what I'm working with, arduino uno, mg90s servo, v3 emg sensor, x2 9v batteries.
I followed this tutorial exactly,
Now the problem is that the values in serial monitor don't seem to change much, if at all when flexing a muscle operating the sensor. Instead the servo moves back and forward in an erratic twitching fashion.
I'm sure the wiring is OK as the tutorial has a lot of praise in the comment section and nobody seemed to have my issue, typical lol the only difference is the tutorial uses a Nano and I'm using an uno, but does that make any difference?
I know the board and servo are OK as I loaded up the servo sweep from the IDE and it works fine. So I'm thinking maybe it's the EMG sensor itself unless you guys can spot something i might of missed? I've included the code, only thing I tried with the code was I put 3 where it says (servo_pin) and I put 0 where it says (Emg_pin) I did change the threshold as a last attempt to get it working but it didn't work.
Things I've tried,
- New usb cable,
- Gave the servo it's own power supply after trying the 5v supply on the uno same result. Yes I grounded the supply to the uno.
- Got medical grade electrodes as I thought the ones included may have been the issue.
I hope you guys can help here's the code, thanks for taking the time to read
#include <Servo.h>
//Threshold for servo motor control with muscle sensor.
//You can set a threshold according to the maximum and minimum values of the muscle sensor.
#define THRESHOLD 250//Pin number where the sensor is connected. (Analog 0)
#define EMG_PIN 0//Pin number where the servo motor is connected. (Digital PWM 3)
#define SERVO_PIN 3//Define Servo motor
Servo SERVO_1;/-------------------------------- void setup ------------------------------------------------/
void setup(){
//BAUDRATE set to 115200, remember it to set monitor serial properly.
//Used this Baud Rate and "NL&CR" option to visualize the values correctly.
Serial.begin(115200);//Set servo motor to digital pin 3
SERVO_1.attach(SERVO_PIN);
}/-------------------------------- void loop ------------------------------------------------/
void loop(){
//The "Value" variable reads the value from the analog pin to which the sensor is connected.
int value = analogRead(EMG_PIN);//If the sensor value is GREATER than the THRESHOLD, the servo motor will turn to 170 degrees.
if(value > THRESHOLD){
SERVO_1.write(170);
}//If the sensor is LESS than the THRESHOLD, the servo motor will turn to 10 degrees.
else{
SERVO_1.write(10);
}//You can use serial monitor to set THRESHOLD properly, comparing the values shown when you open and close your hand.
Serial.println(value);
}