arduino sensor sweep

Arduino Sensor Servo Sweep Tutorial – Locating the nearest target

Arduino Sensor Servo Sweep

In robotics, you often need to locate certain objects. In most cases, this is achieved through the use of multiple various sensors. Minimizing the number of such sensors is often required and can be quite a challenge. In this tutorial we will be looking at a particular servo driven technique which accomplishes just that. We are talking about an Arduino Sensor Servo Sweep. In summary, this is the effect of correctly positioning a sensor through the use of a servo motor and reading the distance accordingly. With this distance, the user can determine the distance to the closest or furthest object depending on the circumstances.

I highly recommend you first view the separate tutorials for Servo Motor Control and Ultrasonic Sensor Distance Evaluation. We will be combining both concepts into the sensor sweep. Click the following links for each tutorial:
Arduino Servo Control: Servo Tutorial
Arduino Ultrasonic Sensor: Ultrasonic Tutorial

Required Hardware

arduino sensor servo tutorial
For this tutorial, you will require the following:

  • Arduino
  • Breadboard
  • Push Buttons
  • Servo Motor
  • Ultrasonic Sensor
  • Jumper Wires

Hardware Setup

The wiring is quite simple for this tutorial. The Ultrasonic Sensor Echo/Trig pins are on Arduino pins 10/8 respectively. The servo output is wired to pin 9. Lastly, the button is connected to pin 11 of the Arduino microcontroller.

Arduino Sensor Servo Software Implementation

Overall, the software for this tutorial is a combination of what was covered in the two tutorials mentioned above. I will walk you through the code and describe each functionality. as always, if you have any questions, post them on the forums.


#include
Servo servo1;
int pos1 = 0;
#define trig 10
#define echo 8
#define button 11
long duration, distance, ClosestTarget, AngleTarget;
void setup()
{
Serial.begin(9600);
servo1.attach(9);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(button, INPUT);
}

The above lines are the definition of the inputs/outputs as well as some of the registers we will be using in the program.


void loop()
{
if(digitalRead(button)==HIGH)
{
for(pos1 = 0; pos1 < 180; pos1 += 1) { servo1.write(pos1); delay(10); digitalWrite(trig, LOW); delayMicroseconds(2); digitalWrite(trig, HIGH); delayMicroseconds(10); digitalWrite(trig, LOW);

Inside the main loop function, we begin by checking the button. The rest of the function will only execute if the button was pressed. this functionality was added in order to limit the sweeping action of the servo. You can remove the button read, but that will leave your servo sweeping continuously unless you add a delay or another way of restricting it.

The following for loop is used for the 180 degree sweep of the servo motor. The "pos1" is written into the servo1 object which instructs the servo to move to the correct location.

After the servo move, the functionality of the Ultrasonic Sensor is implemented. The code above will bring the trigger to LOW. It will then send a HIGH pulse of 10 microseconds with a transition back to LOW level.


duration = pulseIn(echo, HIGH); // Wait for HIGH, timer on, timer ends on LOW
distance = duration / 58;
if(ClosestTarget>distance)
{
ClosestTarget=distance;
AngleTarget=pos1;
}
}
for(pos1 = 180; pos1>=1; pos1-=1)
{
servo1.write(pos1);
delay(10);
}

The above code captures the signal, translates the duration into a distances and verifies if the new distance is shorter than the previous one. If that is the case, it will store the new distance as well as the angle into the registers. This is done in order to find the closest target to our sensor system. This code can easily be modified in order to find the furthest target by reversing the compare check.

Finally, the servo is returned to its 0 position.


Serial.print("Closest Target = " );
Serial.print(ClosestTarget);
Serial.println(" ");
Serial.print("Target Angle = " );
Serial.print(AngleTarget);
Serial.println(" ");
ClosestTarget = 1000;
}
}

The final part of the code is used to send the information of the target to the serial monitor.

Complete Code


#include
Servo servo1;
int pos1 = 0;
#define trig 10
#define echo 8
#define button 11
long duration, distance, ClosestTarget, AngleTarget;
void setup()
{
Serial.begin(9600);
servo1.attach(9);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(button, INPUT);
}
void loop()
{
if(digitalRead(button)==HIGH)
{
for(pos1 = 0; pos1 < 180; pos1 += 1) { servo1.write(pos1); delay(10); digitalWrite(trig, LOW); delayMicroseconds(2); digitalWrite(trig, HIGH); delayMicroseconds(10); digitalWrite(trig, LOW); duration = pulseIn(echo, HIGH); // Wait for HIGH, timer on, timer ends on LOW distance = duration / 58; if(ClosestTarget>distance)
{
ClosestTarget=distance;
AngleTarget=pos1;
}
}
for(pos1 = 180; pos1>=1; pos1-=1)
{
servo1.write(pos1);
delay(10);
}
Serial.print("Closest Target = " );
Serial.print(ClosestTarget);
Serial.println(" ");
Serial.print("Target Angle = " );
Serial.print(AngleTarget);
Serial.println(" ");
ClosestTarget = 1000;
}
}

Thank you all for reading/watching. If you have any additional questions, please write them down on the forums: EEEnthusiast Forums
- EEEnthusiast