Arduino Forum

Topics => Robotics => Topic started by: Linky132 on Mar 01, 2019, 11:42 am

Title: Stuttering Servo Motors
Post by: Linky132 on Mar 01, 2019, 11:42 am

I'm building a simple, wheeled, obstacle avoiding Arduino robot. It consists of a wooden base with an Arduino Uno R3 and an Arduino sensor shield v.4.0 mounted on it. It's powered by 4 AA batteries in a case. The servo motors are FS90R's (plugged into 6 and 8 on the shield) and there is a Sharp GP2Y0A21YK0F Reflective (IR) distance Sensor mounted on the front.

( (

Code: [Select]
#include <Servo.h>// Include servo library

Servo servoLeft; // Declare left servo
Servo servoRight; // Declare right servo

int IRpin = 0; // Analog pin for reading the IR sensor
float IRread; // Floating point number to hold converted voltage as distance

void setup() {
  servoLeft.attach(6); // Attach left signal to P6
  servoRight.attach(8); // Attach right signal to P8

void loop() {

  if (IRread < 299) // NO OBSTACLE

void getDistance() {
  IRread = analogRead(IRpin); // Reads the value of the sharp sensor

void forward() { // Create a forward subroutine

void dontGo() {

void leftTurn() {

The motors work fine without the IR distance sensor but when I add the IR distance sensor code the motors start and stop at very short time intervals with a stuttering motion. It seems as if the Arduino can't do two things at once.

I've tried using millis() as an alternative to delay() but the motors still stutter. Removing the delay() completely makes the motors stutter at really short intervals.

Any help or ideas would be much appreciated, please. Many thanks!
Title: Re: Stuttering Servo Motors
Post by: Snoopybg on Mar 01, 2019, 01:05 pm
If you print the IRread value, does it stay stably below 299 if there is no obstacle?
Title: Re: Stuttering Servo Motors
Post by: slipstick on Mar 01, 2019, 01:05 pm
Only write to the motors when you want to change what they're doing. So if they're already going forward don't keep on telling them to go forward just leave them. You can do something like set a "motorState" variable in forward() and the other motor routines and only call the routine again if it isn't already working.

And make sure you have enough power. NiMH rechargeable batteries are better than alkalines.

Title: Re: Stuttering Servo Motors
Post by: vinceherman on Mar 01, 2019, 02:44 pm
First post.  Proper use of code tags.  Karma++
Title: Re: Stuttering Servo Motors
Post by: MarkT on Mar 02, 2019, 09:59 am
You should use separate power for the servos to the Arduino, servos are current hungry and very bursty in
their current demands - this usually results in random-resetting problems unless the Arduino has a separate table supply.