Ultrasonic Sensor
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.
import RPi.GPIO as GPIO
import time
Initialize the Trigger and Echo pins (ex. TRIGGER = 16 , ECHO = 18).
class UltrasonicInterface():
def __init__(self,trig,echo):
self.GPIO_TRIGGER = trig
self.GPIO_ECHO = echo
Assign Raspberry pi pins to with the assigned number.
def setup_ultrasonic(self):
GPIO.setup(self.GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(self.GPIO_ECHO, GPIO.IN)
Get distance from ultrasonic sensors.
How to get data from the sensors?
Create an Ultrasonic Interface object and pass in the TRIG and ECHO pins as arguments.
Resources:
[1]. HC-SR04 Ultrasonic Range Sensor on the Raspberry Pi