Arduino Ultrasonic servo rover distance meter
Ultrasonic sensors are widely using sensors for various applications, especially for robotic projects such as navigation robots, obstacle avoidance vehicles, etc.
An ultrasonic sensor can very precisely measure the distance to an object up to maximum length of 4 meters.
Refer: Ultrasonic sensor interface with Arduino.
A fixed ultrasonic sensor has an operating angle of 30 degrees only. So we should have to use different techniques to scan wider areas. One of the methods to overcome this area limitation is by using multiple ultrasonic sensors at a time. But in this method more numbers of sensors are required to scan wider areas; for example, 6 numbers of sensors will be required to scan an angle of 180 degrees.
So another technique to expand the range of scanning area is mounting the ultrasonic sensor on a servo arm. Using this method the ultrasonic sensor can scan large areas up to 210 degrees (using micro servos with 180-degree movement). That is the entire area where the servo arm points with a single ultrasonic sensor.
Refer Servo motor Arduino interface.
Arduino servo ultrasonic sensor
Here an ultrasonic sensor is mounted on the servo shaft and the servo motor is fixed to the robot chassis. The Servo mounting brackets for HCSR-04 ultrasonic sensors are available or a similar holder can be assembled by ourselves.
The servo motor has programmed to sweep the shaft in to and fro motion. The servo moves the arm in a range of 180 degrees clockwise and anti-clockwise direction. With this sweeping movement of the servo, the ultrasonic sensor placed on the servo arm scans the entire area comes in front of the sensor. Here then the measured instantaneous distance values are printed on the serial monitor. In an ideal case, the ultrasonic sensor will return a value for each degree of the servo. That is a complete clockwise and anticlockwise movement of the servo shaft returns a total of 360 values.
As mentioned above, in this project the sensor measures the presence of an object at an angle of 210 degrees. The reason for that is an ideal ultrasonic sensor has an operation angle of 30 degrees. So at ‘0’ and ‘180’ degrees the ultrasonic module covers an additional 15 degrees from the angle of servo shaft. Thus a total area of 210 degrees can be covered.
Actually, the project arrangement in this tutorial is part of an automatic navigation robot. So, here shows only the working of an ultrasonic module mounted on a servo. The code used in the project was to just demonstrate the working of the ultrasonic + servo arrangement. For robotic applications, the code can be modified to use the values in various functions. Also, the sweeping movement of the servo motor can be changed depending on the applications.
Servo – Ultrasonic rover code
#include <Servo.h> Servo servo; const int trig = 2, echo = 3 ; int pos = 0, distcm = 0; void setup() { Serial.begin(9600); pinMode(trig, OUTPUT); pinMode(echo, INPUT); servo.attach(9); } void loop() { for (pos = 0; pos <= 180; pos += 1) { servo.write(pos); delay(50); Serial.println(radar()); } for (pos = 180; pos >= 0; pos -= 1) { servo.write(pos); delay(50); Serial.println(radar()); } } long radar(void) { digitalWrite(trig, HIGH); delayMicroseconds(15); digitalWrite(trig, LOW); long dur = pulseIn(echo, HIGH); distcm = dur / 58; return distcm; }
Hi admin. how to stop a servo motor when the ultrasonic detect an object?
Add this code inside the for loops. The movement of servo stops by exiting from the for loop if measured distance is less than or equal to set distance value; that is minimum distance value to the object to decide the object is detected.