Servomotor causes fluctuating values

Hi,

I'm working on a project with four potentiometers, two LDRs, and two continuous servomotors. Everything is connected to and powered by the Arduino.

I've begun to notice that when I power the servomotor and tell it to start spinning, the values output by the potentiometers and the LDR start to fluctuate exponentially. For example, I'll have my LDR reading 30 consistently, then it'll randomly spike to 100 for a second; the potentiometers will go from 0 to 1023 at random without being moved at all. This does not happen when the servomotor isn't spinning.

What could possibly be the cause of this? And what could be an alternative to solve this?

Thanks.

Please post your code using code tags and an illustration of your circuit.
How are you powering the servos? Not with the Arduino +5V pin, right?

The Arduino 5V and GND are going to a breadboard, and the servomotor is connected there.

This is my code for the servomotor and the LDR. My LDR goes into analog pin 4 and the servo signal is connected to digital pin 6; I output the results to the serial port so I can use it in a Processing sketch later.

#include <Servo.h>
Servo servo1;

void setup(){
  Serial.begin(9600);
  servo1.attach(6);
  servo1.write(100);
}

void loop(){
  String printst = String(0);
  int val = analogRead(0);

  // LDR
  val = analogRead(4);
  printst = String("A,") + String(4) + String(",") + String(val);
  Serial.println(printst);

  delay(100);
}

This is roughly how I have things connected.

The servo is powered by a separate battery or other power source, right?
The LDRs have resistors configured as a voltage divider to the A4 pin?

Also, your code doesn't compile. val needs to be declared somewhere as some data type, such as int val = analogRead(4);

My bad, I forgot to add that as I was writing over the relevant parts of the code, but it is defined there now! I edited the post to also include an image now.

Also, the atypical way you're printing to the serial monitor: seems like a clunky way to interface with Processing. You can just do Serial.println("whatever"); and Processing will look for "whatever" with no issues if that's what you're trying to do.

Need all the code. Don't worry, I won't sell it to the CIA :wink:
I suspect your problem is that you are powering the servo from the Arduino. It should be only signal and ground to Arduino, +5V and ground from a separate power supply and connect the grounds together.

2 Likes

The Arduino 5V pin cannot be used to power servos or other heavy loads. Use a separate power supply, and connect the grounds.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.