The BenchBot is equipped with 8 ultrasonic sensors that cover a range of 360 degrees of the system. These sensors are used in the feedback of the BenchBot, as an obstacle avoidance algorithm.
The bot currently is equipped with HCSRF04 ultrasonic sensors, that have a range from 2 cm to 300 cm. The sensor has a resolution of 0.3 cm and works on +5V DC. The data-sheet for the ultrasonic sensors can be found here.
A simple voltage divider circuit can be used to work with the ultrasonic sensors. Hence, a mathematically the equation can be defined as:
After several tests, we found that the ideal values of R2 and R1 should be chosen as per the ratio below.
Here are some resistor values that works well with the BenchBot design.
Resistor pair values | |||
---|---|---|---|
R2 | 2 K Ω | 470 Ω | 1.5 K Ω |
R1 | 1 K Ω | 370 Ω | 680 Ω |
How to use the sensor with raspberry pi?
Initialize the Raspberry pi and required libraries.
Code Block |
---|
import RPi.GPIO as GPIO import time |
Initialize the Trigger and Echo pins (ex. TRIGGER = 16 , ECHO = 18).
Code Block |
---|
class UltrasonicInterface(): def __init__(self,trig,echo): self.GPIO_TRIGGER = trig self.GPIO_ECHO = echo |
Assign Raspberry pi pins to with the assigned number.
Code Block |
---|
def setup_ultrasonic(self): GPIO.setup(self.GPIO_TRIGGER, GPIO.OUT) GPIO.setup(self.GPIO_ECHO, GPIO.IN) |
Get distance from ultrasonic sensors.
Code Block |
---|
def distance(self): GPIO.setmode(GPIO.BCM) # setting GPIO pins self.setup_ultrasonic() # set Trigger to HIGH GPIO.output(self.GPIO_TRIGGER, True) # set a small delay and change Trigger to LOW/False time.sleep(0.00001) GPIO.output(self.GPIO_TRIGGER, False) # get the start time start_time = time.time() stop_time = time.time() while GPIO.input(self.GPIO_ECHO) == 0: start_time = time.time() # get stop time while GPIO.input(self.GPIO_ECHO) == 1: stop_time = time.time() # time difference between start and arrival time_occured = stop_time - start_time # sound speed (34300 cm/s) distance = (time_occured * 34300) / 2 GPIO.cleanup() return distance |
How to get data from the sensors?
Create an Ultrasonic Interface object and pass in the TRIG and ECHO pins as arguments.
Code Block |
---|
ultrasonic_object = UltrasonicInterface(16,18) dist = ultrasonic_object.distance() |
Resources: