Servo motor and sensor code - problems in functionality

Hey guys,
What i’m trying to run is: arduino uno, connected USB to computer. Servo 6V motor is connected to breadboard and 6V battery, and on the breadboard I placed and connected an ultrasonic distance sensor.

I’m having some weird functioning of the implementation, which I would like to get some help on:

  1. At first the code was simpler, without the for loop at the setup, and more similar to the code in the sensor example which exists in the tutorials.

  2. At initial testing, the sensor worked perfect. I managed to place my hand away from it and set a distance of 10cm and then 20cm and in both tests it worked. [I have to mention - the pin was 7 all the time, not 9, so it can’t be a pin change…]

  3. After this, I tried to fix up the code a bit with some time delays and a for loop at the start of the code to “initialize” the motor and make sure it’s working. I want to move it a few times back and forth, not in the loop(), so that it won’t do this endlessly.

  4. What happens now is that first of all the sensor doesn’t work and can’t tell if something is in front of it. I know this might be the wiring, but i’m pretty sure it’s all good so maybe it’s something in the code or software.

  5. What also happens is that instead of running 180 to 0 a few times at the start, the motor goes about 90 degrees and back to 0 only once, sort of like it just “got some power” and moved a bit to check itself, but it’s not what i’m telling it to do in the code…

If anyone can help me figure out the code/software problems this might relate to it would be great :slight_smile:
I’m also just unplugging the USB every time I want to change stuff etc., don’t know if this is healthy for the board. The 6V battery I connected should run the servo 6V perfectly, but maybe you guys can see something in my setup that is ruining the circuit/parts.

Thanks again!

#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards
int i = 0;
const int trigPin = 13;
const int echoPin = 12;

void setup() 
  myservo.attach(7);  // attaches the servo on pin 7 to the servo object 
  Serial.begin(9600); //set up communication with PORT
  for (int x = 0; x < 2; x++) {
  myservo.write(180); //go from 180 to 0 for initial visual test of motor
void loop() 
  long duration, cm;
  pinMode(trigPin, OUTPUT); //set initial LOW, then 2microsec HIGH to start sensor
  digitalWrite(trigPin, LOW); //then back to LOW again to start measuring
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); //enough to reset the sensor
  digitalWrite(trigPin, LOW);

  pinMode(echoPin, INPUT); //set to recieve data from sensor
  duration = pulseIn(echoPin, HIGH); //gets length of time of bounce
  cm = microsecondsToCentimeters(duration); //convert time into distance  
  if (cm < 15) {
    Serial.println(cm); //display the distance from sensor
  else {
      Serial.println("Too Far");

long microsecondsToCentimeters(long microseconds) {
    // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;

What also happens is that instead of running 180 to 0 a few times at the start, the motor goes about 90 degrees and back to 0 only once, sort of like it just "got some power" and moved a bit to check itself, but it's not what i'm telling it to do in the code...

You are only giving the servo 15 milliseconds to get to 180 before telling it to move back to 0 then straight away back to 180. Make the delay longer after sending it to 180 and add another one after sending it back to 0.

By the way, how many times do you want it to run back and forth because at the moment it only does it twice.

I'm also just unplugging the USB every time I want to change stuff etc.

I don't understand this. How is the 6V supply wired to the servo and the Arduino ?

Hi UKHeliBob, thanks for replying.

To answer your questions:

Yes, I did want it to do it twice :)

The 6V is connected to the +/- on the breadboard, and the servo is connected to that through jumpers, and the ground is connected all the way to the board GND. So the power to the servo should come from the 6V and the power to the arduino from the USB. right? I think it's all good.

I will make a test with the extra time delay and update if it didn't get solved.


Update: The delay in the for() loop fixed the problem.