Sweeping Obstacle Detector

The objective of this instructable is to make a setup in which a Ultrasonic sensor is mounted on a servo motor.
I've considered a micro servo (90g), and a red LED.
The connections need to be made as seen in the Fritzig diagram.
Attaching the Ultrasonic sensor and the servo arm should be done using double sided tape and insulation tape.
(Do not consider the joystick, i forgot to remove it)

Parts used:
1) Breadboard
2) Arduino UNO Board
3) Red LED
4) Ultrasound Sensor
5) Tower Pro (9grams) servo
6) Piezoelectric buzzer
7) 220 ohm resistor

The code is as follows:

#define trigPin 7
#define echoPin 6
#define led 13
#define buzzer 11

int ServoHorizontalPin = 3;
int HorizontalPotPin = A0;
int HorizontalPotValue = 0;
int ServoH_Min = 0;
int ServoH_Max = 180;
int pos = 0;
int sound;

Servo HorizontalServo;
int HorizontalServoPosition;

void setup()
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);

void loop()
for (pos = 0; pos <= 180; pos += 2)
for (pos = 180; pos >= 0; pos -= 2)

void alarm()
long duration, distance;
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

if (distance < 20)
digitalWrite(led, HIGH);
analogWrite(11, 20);

analogWrite(11, 0);

Please do try and give me feedback.
Any improvisations are happily accepted.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Sensors Contest 2016

Participated in the
Sensors Contest 2016

2 People Made This Project!


  • Made with Math Contest

    Made with Math Contest
  • Multi-Discipline Contest

    Multi-Discipline Contest
  • Robotics Contest

    Robotics Contest

8 Discussions


3 years ago

Could you please explain how do this function in detail? Does this map out the distances for each indexed radial location? What is the function of the buzzer? Are the distances stored in a data set and then compared on the next sweep to determine a change? Please elaborate on this project some more. What would use use this for?

3 replies

Reply 3 years ago

It keeps sweeping from 0 degrees to 180 degrees. The sensor is set to detect any obstacle at about 20cm (7.87inch). It not map out. The buzzer makes a tone when it detects an obstacle. You can change the range of detection by simply changing the value used in the initial IF Function. Replace the 20 with other values to vary the range. The distance comparison is not implemented, though i shall try it out. It could be used to detect obstacles on a small RC car or Robot. You could tweak the sweeping values to make it go faster or slower.


Reply 3 years ago

Sounds cool, I was thinking that if it would map out a space then you can compare the mapped data to the current data to detect where an object is located.


Reply 3 years ago

I wasnt able to map it, but to find distance to object, you could put a serial print function for "distance". We used it for the ultrasonic sensor, so just printing that value can tell the distance. Also serial printing "pos" will give us the approximate sector where the object is located.

I guess you would just measure the distance to the nearest obstacle, then turn the sensor slightly and measure again. Keep doing this until you know the distance to all the nearest objects around you. You could store this information in variables and have a record or the room around the robot.