Ultrasonic sensor

We have used an ultrasonic distance sensor to measure the distance between the mobile robot and all of the objects in a range of 180 degrees in front of the robot. The ultrasonic sensor is used with a servo mechanism as described next that helps scan a bigger area. The sensor used is shown in the following screenshot:

As you can see, it has two circles that we call the trig circle and the echo circle respectively. The trig circle throws the ultrasonic beam and, as soon as it hits in any solid object, it returns and is captured from the echo circle. We will not go through the details and the geometry of this process. However, it is important to understand that there is a duration in which the ultrasonic sound travels from the sensors and returns. This duration is divided by a number and we can get the distance of the object. Next, you will find the code that we need for all this process using an Arduino controller.

At first, we have to define the pins that the trig and echo pin is connected to. Let's assume that we have connected the echo pin to the Arduino digital pin 7 and the trig pin to the Arduino pin 8:

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin

In the setup() function, we have to define what kind of pins they are. So, we define the trig pin as an output pin and the echo pin as our input. The reason for this is simple. The trig pin, as explained earlier, sends an ultrasonic sound from the sensor to the environment; so, it is an output, where the echo received (or not) the sound, so we can call it our input:

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

In the loop, you can create a function named getDistance() and add the following implementation. We have to create a pulse with two second LOW and then HIGH for 10 seconds. With that pulse, we can grab the duration and if we divide that with 58.2 we can get the distance from the ultrasonic sensor to the closest object found or null if there is nothing in front:

digitalWrite(trigPin, LOW); 
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

Lastly, in your function, you can either have a global variable with the name of distance or return the distance with the following line of code:

return distance; 

Now, you should have a function that it is working perfectly with your sonar and gets the distance. Next, you will find the full code to test your connections. You might need to change the pins defined in the initial lines:

#define echoPin 24 // Echo Pin
#define trigPin 26 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

if (distance >= maximumRange || distance <= minimumRange){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
Serial.println("-1");
digitalWrite(LEDPin, HIGH);
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}

//Delay 50ms before next reading.
delay(50);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.35.72