Introduction: Sweeping Obstacle Detector

Picture of 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
#include

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()
{
HorizontalServo.attach(ServoHorizontalPin);
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);
}

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

void alarm()
{
HorizontalServo.write(pos);
delay(25);
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(1);
digitalWrite(trigPin, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;

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

else
{
digitalWrite(led,LOW);
analogWrite(11, 0);
}
}

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

Comments

as-WIN-th made it! (author)2016-05-25

Find the attached program , tweak it and use it to store your Ultra sonic sensor values.

Now you can compare previous US sensor values to current values. Comparison will be easier if you reduce the scanning positions. (i.e) do not scan from 0 to 180 and 180 to 0 all the way, because you will have 180 previous values to compare with new ones.

Regarding attaching a joystick to this project you should bring

for (pos = 0; pos <= 180; pos += 2)
{
alarm();
}
for (pos = 180; pos >= 0; pos -= 2)
{
alarm();
}

this part of code into a conditional while loop

while (no values from X and Y co-ordinates)

{

for (pos = 0; pos <= 180; pos += 2)
{
alarm();
}
for (pos = 180; pos >= 0; pos -= 2)
{
alarm();
}

}

Once you interface your joy and able to read the values from joy on serial monitor you will get to know how to proceed with the code.

const int VRxPin= 0; //VRx pin connected to arduino pin A0
const int VRyPin= 1; //VRy pin connected to arduino in A1
const int SwButtonPin= 8; //SW pin connected to arduino pin D8

int pressed= -1; //this variable will determine whether joystick has been pressed down (selected)
int x= -1;//this variable will hold the X-coordinate value
int y= -1; //this variable will hold the Y-coordinate value

void readJoystick() {
pressed= digitalRead(SwButtonPin);//reads whether joystick has been pressed down (selected) or not
x= analogRead(VRxPin);//reads the X-coordinate value
y= analogRead(VRyPin);//reads the Y-coordinate value
}

void setup() {
pinMode(SwButtonPin, INPUT);//sets the SW switch as input
digitalWrite(SwButtonPin, HIGH);//sets the SW button HIGH
Serial.begin(115200);//sets the baud rate
}

void loop() {
readJoystick();//calls this function which reads the digital input button SW, the X-coordinate and the Y-coordinate
Serial.print("X: ");
Serial.println(x);//prints the X-coordinate
Serial.print("Y: ");
Serial.println(y);//prints the Y-coordinate
Serial.print (" Pressed: ");
Serial.println(pressed);//prints whether joystick knob has been pressed or not
delay(10);
}

Soul_seeker (author)as-WIN-th2016-05-25

Cool! Thanks.

as-WIN-th made it! (author)2016-05-25

Find the attached program , tweak it and use it to store your Ultra sonic sensor values.

Now you can compare previous US sensor values to current values. Comparison will be easier if you reduce the scanning positions. (i.e) do not scan from 0 to 180 and 180 to 0 all the way, because you will have 180 previous values to compare with new ones.

Regarding attaching a joystick to this project you should bring

for (pos = 0; pos <= 180; pos += 2)
{
alarm();
}
for (pos = 180; pos >= 0; pos -= 2)
{
alarm();
}

this part of code into a conditional while loop

while (no values from X and Y co-ordinates)

{

for (pos = 0; pos <= 180; pos += 2)
{
alarm();
}
for (pos = 180; pos >= 0; pos -= 2)
{
alarm();
}

}

Once you interface your joy and able to read the values from joy on serial monitor you will get to know how to proceed with the code.

const int VRxPin= 0; //VRx pin connected to arduino pin A0
const int VRyPin= 1; //VRy pin connected to arduino in A1
const int SwButtonPin= 8; //SW pin connected to arduino pin D8

int pressed= -1; //this variable will determine whether joystick has been pressed down (selected)
int x= -1;//this variable will hold the X-coordinate value
int y= -1; //this variable will hold the Y-coordinate value

void readJoystick() {
pressed= digitalRead(SwButtonPin);//reads whether joystick has been pressed down (selected) or not
x= analogRead(VRxPin);//reads the X-coordinate value
y= analogRead(VRyPin);//reads the Y-coordinate value
}

void setup() {
pinMode(SwButtonPin, INPUT);//sets the SW switch as input
digitalWrite(SwButtonPin, HIGH);//sets the SW button HIGH
Serial.begin(115200);//sets the baud rate
}

void loop() {
readJoystick();//calls this function which reads the digital input button SW, the X-coordinate and the Y-coordinate
Serial.print("X: ");
Serial.println(x);//prints the X-coordinate
Serial.print("Y: ");
Serial.println(y);//prints the Y-coordinate
Serial.print (" Pressed: ");
Serial.println(pressed);//prints whether joystick knob has been pressed or not
delay(10);
}

baudeagle (author)2016-05-21

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?

Soul_seeker (author)baudeagle2016-05-21

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.

baudeagle (author)Soul_seeker2016-05-22

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.

Soul_seeker (author)baudeagle2016-05-22

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.

DIY Hacks and How Tos (author)2016-05-21

Great robotic sensor. You could easily use this to map out obstacles in a whole area.

Could you explain the method to mapping ? New to arduino :P

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.

Cool! Thanks :D

About This Instructable

462views

27favorites

More by Soul_seeker:MPU6050 controlled servo armSweeping obstacle detector
Add instructable to: