Circuit works in simulation and not in real hardware....button is needed to press multiple times to register a pulse..jerking of mg995

Title: MG995 Servos Start Automatically, Button Debouncing Issues & Jerking Motors – Works in Tinkercad but Not in Real Setup

Hello Arduino Community,

We’ve built a project to drive two MG995 servo motors using an Arduino Uno. The system is supposed to work in two modes:

  1. Synchronous Mode: Both servos rotate together.

  2. Asynchronous Mode: Servo 1 rotates first, followed by Servo 2, and vice versa.

We tested our logic in Tinkercad simulation, and everything works perfectly there. However, in the real hardware setup, we are facing three major problems:


  1. Motors Start Immediately on Power-Up

Expected Behavior: Servos should remain at rest (0° position) when Arduino is powered on. They should only start moving when the Start button is pressed.

Actual Behavior: As soon as the Arduino is powered, the servos begin moving, even without pressing the Start button.

  1. Buttons (Stop/Delay Mode) are Unresponsive

We are using INPUT_PULLUP for Start, Stop, and Delay Mode buttons.

Problem: These buttons sometimes require 5 to 10 presses to register an action.

This doesn’t happen in Tinkercad, only in real-world usage.

We suspect a debouncing issue, but need confirmation and advice.

  1. Motors Show Jerky Movements

We're using two MG995 servos, each powered by a separate regulated 6.5V supply.

Despite this, the servos jerk erratically during operation, and don’t run smoothly as in the simulation.

All GNDs (servo power and Arduino GND) are properly connected.


Code Summary:

Buttons are set with INPUT_PULLUP.

Servos are driven using the Servo.h library.

Logic checks for button presses and uses flags (motorRunning and delayMode) to control motion.

We use delay(300) after button presses for debouncing, but it doesn’t seem reliable in hardware.

Code:

#include <Servo.h>

const int START_BUTTON_PIN = 7;
const int STOP_BUTTON_PIN = 6;
const int DELAY_BUTTON_PIN = 5;
const int SERVO_PIN_1 = 10;
const int SERVO_PIN_2 = 11;

Servo servo1, servo2;

bool motorRunning = false;
bool delayMode = false;

void setup() {
Serial.begin(9600);
pinMode(START_BUTTON_PIN, INPUT_PULLUP);
pinMode(STOP_BUTTON_PIN, INPUT_PULLUP);
pinMode(DELAY_BUTTON_PIN, INPUT_PULLUP);
servo1.attach(SERVO_PIN_1);
servo2.attach(SERVO_PIN_2);
servo1.write(0);
servo2.write(0);
}

void loop() {
if (digitalRead(START_BUTTON_PIN) == LOW) {
Serial.println("Start button pressed");
motorRunning = true;
delay(300);
}

if (digitalRead(STOP_BUTTON_PIN) == LOW) {
Serial.println("Stop button pressed");
motorRunning = false;
delayMode = false;
delay(300);
}

if (digitalRead(DELAY_BUTTON_PIN) == LOW) {
Serial.println("Delay mode activated");
delayMode = true;
delay(300);
}

while (motorRunning) {
if (delayMode) {
servo1.write(90); delay(500);
servo2.write(90); delay(500);
servo1.write(0); delay(500);
servo2.write(0); delay(500);
} else {
servo1.write(90); servo2.write(90); delay(1000);
servo1.write(0); servo2.write(0); delay(1000);
}

if (digitalRead(STOP_BUTTON_PIN) == LOW) {
  Serial.println("Stop button pressed");
  motorRunning = false;
  delayMode = false;
  delay(300);
}

}
}


My Questions:

  1. Why do the motors start immediately when powered on, even though motorRunning is initialized to false?

  2. Why do the buttons require multiple presses to register a state change?

Should I use software/hardware debouncing (e.g., millis() or capacitors)?

  1. Why are the MG995 motors jerking, even with a regulated 6.5V supply and separate GNDs?

Is this a power issue, servo library timing issue,

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

if (delayMode) {
servo1.write(90); delay(500);
servo2.write(90); delay(500);
servo1.write(0); delay(500);
servo2.write(0); delay(500);
} else {
servo1.write(90); servo2.write(90); delay(1000);
servo1.write(0); servo2.write(0); delay(1000);
}

For what period will the sketch be unable to read inputs whilst this code runs ?

Your wiring diagram does not match your description. So I don't have any idea what you have actually done.

The servo power supply must be able to provide at least 3 Amperes for two MG995 servos, and the voltage should be between 4.8 and 6V.

If you want your projects to succeed, study the manufacturer's specifications and obey the ratings.

Your while is full of long delays. If you start the servo, your code is hanging there incapable of getting trigger from the buttons.

What is the task of the program in real life?

To stimulate Soleus muscle

Like it requires 5 to 6 times presses to actuate the button

Hello chiraggg09

I did a quick review of your programme.
All calls to the delay() function block the expected real-time behaviour of the program.

   Line 29: delay(300);
   Line 36: delay(300);
   Line 42: delay(300);
   Line 47: servo1.write(90); delay(500);
   Line 48: servo2.write(90); delay(500);
   Line 49: servo1.write(0); delay(500);
   Line 50: servo2.write(0); delay(500);
   Line 52: servo1.write(90); servo2.write(90); delay(1000);
   Line 53: servo1.write(0); servo2.write(0); delay(1000);
   Line 60: delay(300);

What is to be done?

Divide the project into sub-projects:

  1. button manager, including debouncing
  2. servo manager
  3. timer module

A timer is required for debouncing and for timing the servos.

All in all, I recommend a redesign of the project.

1 Like