Servo Just Flickering instead of Sweeping

I am creating a radar using ultrasonic sensor and servo motor. The servo motor works fine when using the sweep example. But when I use the below code, the servo just flickers, i.e) moves very very small rotation every second. It takes about 30 seconds to rotate about 160 degrees. Is the problem with my code or my arduino or servo?

Components Used:

  1. Arduino Uno/Mega
  2. Servo Motor(MicroServo 9g)
  3. HC-SR04 Ultrasonic Sensor

.
.

Here's The Code I Used:

// Includes the Servo library
#include <Servo.h>. 
// Defines Tirg and Echo pins of the Ultrasonic Sensor
const int trigPin = 10;
const int echoPin = 11;
// Variables for the duration and the distance
long duration;
int distance;
Servo myServo; // Creates a servo object for controlling the servo motor
void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600);
  myServo.attach(12); // Defines on which pin is the servo motor attached
}
void loop() {
  // rotates the servo motor from 15 to 165 degrees
  for(int i=15;i<=165;i++){  
  myServo.write(i);
  distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
  
  Serial.print(i); // Sends the current degree into the Serial Port
  Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  Serial.print(distance); // Sends the distance value into the Serial Port
  Serial.print("."); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing
  }
  // Repeats the previous lines from 165 to 15 degrees
  for(int i=165;i>15;i--){  
  myServo.write(i);
  distance = calculateDistance();
  Serial.print(i);
  Serial.print(",");
  Serial.print(distance);
  Serial.print(".");
  }
}
// Function for calculating the distance measured by the Ultrasonic sensor
int calculateDistance(){ 
  
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH); 
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
  distance= duration*0.034/2;
  return distance;
}

.
.

Here is the circuit connection:

.
.

Thanks in Advance!

How much time elapses between each servo move in your sketch ?

How much time elapses between each servo move in the Sweep sketch ?

Never power a servo motor from the Arduino 5V pin !

  • Connect the power wires on the Servo to an external power supply that satisfies the SG90 maximum current draw of 600mA and...
  • Connect the Ground wire of the power supply to the Ground rail of the Arduino.

Since you did not set a shorter time-out period on your pulseln() function call, you get the standard 1 second delay while waiting for the echo. Change to a time-out length that will work for your project and the servo will be able to go a bit faster.

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