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.

