Troubles with power supply I think?

Heya I'm new to this whole Arduino thing. And I'm making a Automatic Bin with a Ultrasonic Sensor and Servo motor. However what I'm having a problem with is just the power supply. I have no idea why it is slow. It works perfectly connected to USB from keyboard but when I use a 6v battery connected to VIN pin and GND pin. It is really slow and has a flashing led light. I don't know if its anything got to do with my code or its just a battery problem. I used a 9v battery before which was connected to the DC jack but that also does not work. I just hope i did not overcomplicate things and overlooked a simple problem that i can easily fix.

Components:
Arduino Uno R3
HC-SR04 Ultrasonic Sensor
Servo Motor Tower Pro SG5010

Code:

#include <Servo.h>
// Connected to servo (SG-5010)
Servo servo1;

int trigPin = 9;
int echoPin = 8;

long distance;
long duration;
 
void setup() 
{
servo1.attach(7); 

 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
}
 
void loop() {
  ultra();
  servo1.write(0);

  if(distance <= 10){

  servo1.write(90);
  delay(100);
  servo1.write(180);
  }
  delay(50);
}
// Connected to Ultrasonic Sensor (HC-SR04)
void ultra(){

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = (duration/2) / 29.1; if (distance < 80);
  
  delay(500);

}

Pin Layout:

The other Pin layout with the 9v battery is just identically the same but the Sensor's VCC Pin connected to the VIN Pin and the 9V battery connected to the barrel DC Jack.

Power the servo directly from the batteries, not from the 5V pin of the Arduino.

Forget 9V battery, useless for most arduino circuits and all circuits with motors.

1 Like

So I connect the power wire and GND wire to the battery or just the power wire?.

That is what I would expect, you are starving the Arduino. You need about 7V on Vin for it to operate properly. There is a voltage drop in the regulator. The sensor should be connected to the 5V pin. The Vin input at 7V will be to much.

I have the sensor and motor both connected to 5v pin. do i need to remove the servo motor from 5v pin? Or do i just replace the batterys to 7.5 volts?

Servo external. Supply grounding.

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