Running off seperate 6V power supply

I am probably showing my ignorance asking this, but I’m building a line following robot with the Arduino (Uno) receiving analogue inputs from a Pololu QTR reflectance sensor (2 of them) to control two continuous rotation servo’s.

I’ve got the following sketch :

#include <Servo.h>


int sensor2;
int sensor3;

Servo myservoRH;
Servo myservoLH;

void setup()
{
  myservoRH.attach(9);
  myservoLH.attach(10);
}

void loop()
{

  sensor2 = analogRead(2);
  sensor3 = analogRead(3);
  
  if (analogRead(3)>500) myservoRH.write(80);
  else if (analogRead(3)<501) myservoRH.write(90);
  if (analogRead(2)>500) myservoLH.write(100);
  else if (analogRead(2)<501) myservoLH.write(90);
}

Before anyone says it, I am aware there’s less lengthy alternatives and I’ve been experimenting with them too, though I’d like to get this one working first.

when myservo.write(90), no rotation. When myservo.write(<90) or myservo write(>90), rotation in opposite directions respectively. Closer to 0/180, the higher the speed.

Anyway.

This sketch works a treat when attached to the PC via the USB, but doesn’t want to work when running off 6V (4AA cells) alone (the servos twitch and then nothing). The LED’s on the arduino do not illuminate, so Arduino is not running (?). I am aware servo’s take a lot of current but I would have thought 4AA cells were enough to supply the whole circuit?

Is there something I need to change on my sketch, or is this a circuitry issue?

Many thanks.

The most common cause of your difficulties is powering the servos from the arduino. Are you?
Don’t.

This is an example of how it could be done. Note that 9v batteries like the one used here us usually a poor choice, but this diagram does show a workable solution.

ServoDiagram.jpg

I’ve attached to this post a diagram of the current circuit. It’s almost identical to your diagram except the choice of digital pins and the fact there’s 6 sensors.

Does the Arduino require a 9V supply on top of what I’ve got?

I don't see any source for Arduino 5V power, try connecting a well regulated 5V, 1 Amp to the 5V pin (+) and GND (-) or 7 to 12VDC to the barrel jack.
NOTE: The QTR needs 100 mA so a 9V "transistor" batt. may not last long.

Ah thanks a lot that improves things significantly :slight_smile: